Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tests #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
333 changes: 132 additions & 201 deletions tests/Sniffs/Exceptions/ExceptionDeclarationSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,201 +4,160 @@

namespace Consistence\Sniffs\Exceptions;

use Generator;

class ExceptionDeclarationSniffTest extends \Consistence\Sniffs\TestCase
{

public function testInvalidExceptionName(): void
{
$resultFile = $this->checkFile(__DIR__ . '/data/InvalidExceptionName.php', [
'exceptionsDirectoryName' => 'data',
]);

$this->assertSniffError(
$resultFile,
7,
ExceptionDeclarationSniff::CODE_NOT_ENDING_WITH_EXCEPTION,
'Exception class name "InvalidExceptionName" must end with "Exception".'
);
}

public function testValidClassName(): void
{
$resultFile = $this->checkFile(__DIR__ . '/data/ValidNameException.php', [
'exceptionsDirectoryName' => 'data',
]);

$this->assertNoSniffErrorInFile($resultFile);
}

public function testValidClassNameThatExtendsCustomException(): void
{
$resultFile = $this->checkFile(__DIR__ . '/data/ValidClassNameThatExtendsCustomException.php', [
'exceptionsDirectoryName' => 'data',
]);

$this->assertNoSniffErrorInFile($resultFile);
}

public function testAbstractExceptionWithValidNameException(): void
{
$resultFile = $this->checkFile(__DIR__ . '/data/AbstractExceptionWithValidNameException.php', [
'exceptionsDirectoryName' => 'data',
]);

$this->assertNoSniffErrorInFile($resultFile);
}

public function testAbstractClassWithInvalidExceptionName(): void
{
$resultFile = $this->checkFile(__DIR__ . '/data/AbstractExceptionWithInvalidName.php', [
'exceptionsDirectoryName' => 'data',
]);

$this->assertSniffError(
$resultFile,
7,
ExceptionDeclarationSniff::CODE_NOT_ENDING_WITH_EXCEPTION,
'Exception class name "AbstractExceptionWithInvalidName" must end with "Exception".'
);
}

public function testClassThatDoesNotExtendAnything(): void
{
$resultFile = $this->checkFile(__DIR__ . '/data/ClassThatDoesNotExtendAnything.php', [
'exceptionsDirectoryName' => 'data',
]);

$this->assertNoSniffErrorInFile($resultFile);
}

public function testClassThatExtendsRegularClass(): void
{
$resultFile = $this->checkFile(__DIR__ . '/data/ClassThatDoesNotExtendException.php', [
'exceptionsDirectoryName' => 'data',
]);

$this->assertNoSniffErrorInFile($resultFile);
}

public function testInterfaceThatDoesNotExtendAnything(): void
{
$resultFile = $this->checkFile(__DIR__ . '/data/InterfaceThatDoesNotExtendAnything.php', [
'exceptionsDirectoryName' => 'data',
]);

$this->assertNoSniffErrorInFile($resultFile);
}

public function testInterfaceThatDoesNotExtendAnythingException(): void
{
$resultFile = $this->checkFile(__DIR__ . '/data/InterfaceThatDoesNotExtendAnythingException.php', [
'exceptionsDirectoryName' => 'data',
]);

$this->assertNoSniffErrorInFile($resultFile);
}

public function testInterfaceThatExtendsException(): void
{
$resultFile = $this->checkFile(__DIR__ . '/data/InterfaceThatExtendsException.php', [
'exceptionsDirectoryName' => 'data',
]);

$this->assertNoSniffErrorInFile($resultFile);
}

public function testInterfaceThatExtendsExceptionIncorrectName(): void
{
$resultFile = $this->checkFile(__DIR__ . '/data/InterfaceThatExtendsExceptionIncorrectName.php', [
'exceptionsDirectoryName' => 'data',
]);

$this->assertSniffError(
$resultFile,
7,
ExceptionDeclarationSniff::CODE_NOT_ENDING_WITH_EXCEPTION,
'Exception class name "InterfaceThatExtendsExceptionIncorrectName" must end with "Exception".'
);
}

public function testExceptionWithConstructorWithoutParametersIsNotChainable(): void
{
$resultFile = $this->checkFile(__DIR__ . '/data/ConstructWithoutParametersException.php', [
'exceptionsDirectoryName' => 'data',
]);

$this->assertSniffError(
$resultFile,
10,
ExceptionDeclarationSniff::CODE_NOT_CHAINABLE,
'Exception is not chainable. It must have optional \Throwable as last constructor argument.'
);
}

public function testExceptionWithChainableConstructorIsChainable(): void
{
$resultFile = $this->checkFile(__DIR__ . '/data/ChainableConstructorException.php', [
'exceptionsDirectoryName' => 'data',
]);

$this->assertNoSniffErrorInFile($resultFile);
}

public function testExceptionWithCustomExceptionArgumentIsChainable(): void
{
$resultFile = $this->checkFile(__DIR__ . '/data/CustomExceptionArgumentChainableConstructorException.php', [
'exceptionsDirectoryName' => 'data',
]);

$this->assertNoSniffErrorInFile($resultFile);
/**
* @return mixed[][]|\Generator
*/
public function validExceptionDeclarationDataProvider(): Generator
{
yield 'valid class name' => [
'filePath' => __DIR__ . '/data/ValidNameException.php',
];
yield 'valid class name that extends custom exception' => [
'filePath' => __DIR__ . '/data/ValidClassNameThatExtendsCustomException.php',
];
yield 'abstract exception with valid name' => [
'filePath' => __DIR__ . '/data/AbstractExceptionWithValidNameException.php',
];
yield 'class that does not extend anything' => [
'filePath' => __DIR__ . '/data/ClassThatDoesNotExtendAnything.php',
];
yield 'class that extends regular class' => [
'filePath' => __DIR__ . '/data/ClassThatDoesNotExtendException.php',
];
yield 'interface that does not extend anything' => [
'filePath' => __DIR__ . '/data/InterfaceThatDoesNotExtendAnything.php',
];
yield 'interface that does not extend anything exception' => [
'filePath' => __DIR__ . '/data/InterfaceThatDoesNotExtendAnythingException.php',
];
yield 'interface that extends exception' => [
'filePath' => __DIR__ . '/data/InterfaceThatExtendsException.php',
];
yield 'exception with chainable constructor is chainable' => [
'filePath' => __DIR__ . '/data/ChainableConstructorException.php',
];
yield 'exception with custom exception argument is chainable' => [
'filePath' => __DIR__ . '/data/CustomExceptionArgumentChainableConstructorException.php',
];
yield 'exception with error argument is chainable' => [
'filePath' => __DIR__ . '/data/ErrorArgumentChainableConstructorException.php',
];
yield 'exception is placed in correct directory' => [
'filePath' => __DIR__ . '/data/ValidNameException.php',
];
}

public function testExceptionWithErrorArgumentIsChainable(): void
/**
* @dataProvider validExceptionDeclarationDataProvider
*
* @param string $filePath
*/
public function testValidExceptionDeclaration(string $filePath): void
{
$resultFile = $this->checkFile(__DIR__ . '/data/ErrorArgumentChainableConstructorException.php', [
$resultFile = $this->checkFile($filePath, [
'exceptionsDirectoryName' => 'data',
]);

$this->assertNoSniffErrorInFile($resultFile);
}

public function testExceptionWithNonchainableConstructorIsNotChainable(): void
{
$resultFile = $this->checkFile(__DIR__ . '/data/NonChainableConstructorException.php', [
'exceptionsDirectoryName' => 'data',
]);

$this->assertSniffError(
$resultFile,
10,
ExceptionDeclarationSniff::CODE_NOT_CHAINABLE,
'Exception is not chainable. It must have optional \Throwable as last constructor argument and has "string".'
);
/**
* @return mixed[][]|\Generator
*/
public function invalidExceptionDeclarationDataProvider(): Generator
{
yield 'invalid exception name' => [
'filePath' => __DIR__ . '/data/InvalidExceptionName.php',
'exceptionsDirectoryName' => 'data',
'line' => 7,
'code' => ExceptionDeclarationSniff::CODE_NOT_ENDING_WITH_EXCEPTION,
'message' => 'Exception class name "InvalidExceptionName" must end with "Exception".',
];
yield 'abstract class with invalid exception name' => [
'filePath' => __DIR__ . '/data/AbstractExceptionWithInvalidName.php',
'exceptionsDirectoryName' => 'data',
'line' => 7,
'code' => ExceptionDeclarationSniff::CODE_NOT_ENDING_WITH_EXCEPTION,
'message' => 'Exception class name "AbstractExceptionWithInvalidName" must end with "Exception".',
];
yield 'interface that extends exception incorrect name' => [
'filePath' => __DIR__ . '/data/InterfaceThatExtendsExceptionIncorrectName.php',
'exceptionsDirectoryName' => 'data',
'line' => 7,
'code' => ExceptionDeclarationSniff::CODE_NOT_ENDING_WITH_EXCEPTION,
'message' => 'Exception class name "InterfaceThatExtendsExceptionIncorrectName" must end with "Exception".',
];
yield 'exception with constructor without parameters is not chainable' => [
'filePath' => __DIR__ . '/data/ConstructWithoutParametersException.php',
'exceptionsDirectoryName' => 'data',
'line' => 10,
'code' => ExceptionDeclarationSniff::CODE_NOT_CHAINABLE,
'message' => 'Exception is not chainable. It must have optional \Throwable as last constructor argument.',
];
yield 'exception with non-chainable constructor is not chainable' => [
'filePath' => __DIR__ . '/data/NonChainableConstructorException.php',
'exceptionsDirectoryName' => 'data',
'line' => 10,
'code' => ExceptionDeclarationSniff::CODE_NOT_CHAINABLE,
'message' => 'Exception is not chainable. It must have optional \Throwable as last constructor argument and has "string".',
];
yield 'exception with constructor without parameter type hint is not chainable' => [
'filePath' => __DIR__ . '/data/NonChainableConstructorWithoutParameterTypehintException.php',
'exceptionsDirectoryName' => 'data',
'line' => 10,
'code' => ExceptionDeclarationSniff::CODE_NOT_CHAINABLE,
'message' => 'Exception is not chainable. It must have optional \Throwable as last constructor argument and has none.',
];
yield 'exception is placed in incorrect directory' => [
'filePath' => __DIR__ . '/data/ValidNameException.php',
'exceptionsDirectoryName' => 'exceptions',
'line' => 7,
'code' => ExceptionDeclarationSniff::CODE_INCORRECT_EXCEPTION_DIRECTORY,
'message' => 'Exception file "ValidNameException.php" must be placed in "exceptions" directory (is in "data").',
];
yield 'exception is placed in incorrect directory case sensitively' => [
'filePath' => __DIR__ . '/data/ValidNameException.php',
'exceptionsDirectoryName' => 'Data',
'line' => 7,
'code' => ExceptionDeclarationSniff::CODE_INCORRECT_EXCEPTION_DIRECTORY,
'message' => 'Exception file "ValidNameException.php" must be placed in "Data" directory (is in "data").',
];
}

public function testExceptionWithConstructorWithoutParameterTypeHintIsNotChainable(): void
/**
* @dataProvider invalidExceptionDeclarationDataProvider
*
* @param string $filePath
* @param string $exceptionsDirectoryName
* @param int $line
* @param string $code
* @param string $message
*/
public function testInvalidExceptionDeclaration(
string $filePath,
string $exceptionsDirectoryName,
int $line,
string $code,
string $message
): void
{
$resultFile = $this->checkFile(__DIR__ . '/data/NonChainableConstructorWithoutParameterTypehintException.php', [
'exceptionsDirectoryName' => 'data',
$resultFile = $this->checkFile($filePath, [
'exceptionsDirectoryName' => $exceptionsDirectoryName,
]);

$this->assertSniffError(
$resultFile,
10,
ExceptionDeclarationSniff::CODE_NOT_CHAINABLE,
'Exception is not chainable. It must have optional \Throwable as last constructor argument and has none.'
$line,
$code,
$message
);
}

public function testExceptionIsPlacedInCorrectDirectory(): void
{
$resultFile = $this->checkFile(__DIR__ . '/data/ValidNameException.php', [
'exceptionsDirectoryName' => 'data',
]);

$this->assertNoSniffErrorInFile($resultFile);
}

/**
* @requires OS WIN
*/
Expand All @@ -212,32 +171,4 @@ public function testExceptionIsPlacedInCorrectDirectoryOnWindows(): void
$this->assertNoSniffErrorInFile($resultFile);
}

public function testExceptionIsPlacedInIncorrectDirectory(): void
{
$resultFile = $this->checkFile(__DIR__ . '/data/ValidNameException.php', [
'exceptionsDirectoryName' => 'exceptions',
]);

$this->assertSniffError(
$resultFile,
7,
ExceptionDeclarationSniff::CODE_INCORRECT_EXCEPTION_DIRECTORY,
'Exception file "ValidNameException.php" must be placed in "exceptions" directory (is in "data").'
);
}

public function testExceptionIsPlacedInIncorrectDirectoryCaseSensitively(): void
{
$resultFile = $this->checkFile(__DIR__ . '/data/ValidNameException.php', [
'exceptionsDirectoryName' => 'Data',
]);

$this->assertSniffError(
$resultFile,
7,
ExceptionDeclarationSniff::CODE_INCORRECT_EXCEPTION_DIRECTORY,
'Exception file "ValidNameException.php" must be placed in "Data" directory (is in "data").'
);
}

}
Loading