|
2 | 2 |
|
3 | 3 | namespace SlevomatCodingStandard\Sniffs\Namespaces; |
4 | 4 |
|
5 | | -use PHP_CodeSniffer\Files\File; |
6 | 5 | use SlevomatCodingStandard\Sniffs\TestCase; |
7 | 6 | use Throwable; |
8 | 7 | use TypeError; |
9 | 8 |
|
10 | 9 | class FullyQualifiedExceptionsSniffTest extends TestCase |
11 | 10 | { |
12 | 11 |
|
13 | | - public function testNonFullyQualifiedExceptionInTypeHint(): void |
| 12 | + public function test(): void |
14 | 13 | { |
15 | | - self::assertSniffError( |
16 | | - $this->getFileReport(), |
17 | | - 3, |
18 | | - FullyQualifiedExceptionsSniff::CODE_NON_FULLY_QUALIFIED_EXCEPTION, |
19 | | - 'FooException' |
20 | | - ); |
21 | | - } |
| 14 | + $report = self::checkFile(__DIR__ . '/data/fullyQualifiedExceptionNames.php'); |
22 | 15 |
|
23 | | - public function testNonFullyQualifiedExceptionInThrow(): void |
24 | | - { |
25 | | - self::assertSniffError( |
26 | | - $this->getFileReport(), |
27 | | - 6, |
28 | | - FullyQualifiedExceptionsSniff::CODE_NON_FULLY_QUALIFIED_EXCEPTION, |
29 | | - 'FooException' |
30 | | - ); |
31 | | - } |
| 16 | + // Not fully qualified exception in type hint |
| 17 | + self::assertSniffError($report, 3, FullyQualifiedExceptionsSniff::CODE_NON_FULLY_QUALIFIED_EXCEPTION, 'FooException'); |
32 | 18 |
|
33 | | - public function testNonFullyQualifiedExceptionInCatch(): void |
34 | | - { |
35 | | - self::assertSniffError( |
36 | | - $this->getFileReport(), |
37 | | - 7, |
38 | | - FullyQualifiedExceptionsSniff::CODE_NON_FULLY_QUALIFIED_EXCEPTION, |
39 | | - 'BarException' |
40 | | - ); |
41 | | - self::assertSniffError( |
42 | | - $this->getFileReport(), |
43 | | - 9, |
44 | | - FullyQualifiedExceptionsSniff::CODE_NON_FULLY_QUALIFIED_EXCEPTION, |
45 | | - Throwable::class |
46 | | - ); |
47 | | - self::assertSniffError( |
48 | | - $this->getFileReport(), |
49 | | - 11, |
50 | | - FullyQualifiedExceptionsSniff::CODE_NON_FULLY_QUALIFIED_EXCEPTION, |
51 | | - TypeError::class |
52 | | - ); |
53 | | - } |
| 19 | + // Not fully qualified exception in throw |
| 20 | + self::assertSniffError($report, 6, FullyQualifiedExceptionsSniff::CODE_NON_FULLY_QUALIFIED_EXCEPTION, 'FooException'); |
54 | 21 |
|
55 | | - public function testFullyQualifiedExceptionInTypeHint(): void |
56 | | - { |
57 | | - self::assertNoSniffError($this->getFileReport(), 16); |
58 | | - } |
| 22 | + // Not fully qualified exception in catch |
| 23 | + self::assertSniffError($report, 7, FullyQualifiedExceptionsSniff::CODE_NON_FULLY_QUALIFIED_EXCEPTION, 'BarException'); |
| 24 | + self::assertSniffError($report, 9, FullyQualifiedExceptionsSniff::CODE_NON_FULLY_QUALIFIED_EXCEPTION, Throwable::class); |
| 25 | + self::assertSniffError($report, 11, FullyQualifiedExceptionsSniff::CODE_NON_FULLY_QUALIFIED_EXCEPTION, TypeError::class); |
59 | 26 |
|
60 | | - public function testFullyQualifiedExceptionInThrow(): void |
61 | | - { |
62 | | - self::assertNoSniffError($this->getFileReport(), 19); |
63 | | - } |
| 27 | + // Fully qualified exception in type hint |
| 28 | + self::assertNoSniffError($report, 16); |
64 | 29 |
|
65 | | - public function testFullyQualifiedExceptionInCatch(): void |
66 | | - { |
67 | | - self::assertNoSniffError($this->getFileReport(), 20); |
68 | | - self::assertNoSniffError($this->getFileReport(), 22); |
69 | | - self::assertNoSniffError($this->getFileReport(), 24); |
70 | | - self::assertNoSniffError($this->getFileReport(), 26); |
71 | | - self::assertNoSniffError($this->getFileReport(), 28); |
| 30 | + // Fully qualified exception in throw |
| 31 | + self::assertNoSniffError($report, 19); |
| 32 | + |
| 33 | + // Fully qualified exception in catch |
| 34 | + self::assertNoSniffError($report, 20); |
| 35 | + self::assertNoSniffError($report, 22); |
| 36 | + self::assertNoSniffError($report, 24); |
| 37 | + self::assertNoSniffError($report, 26); |
| 38 | + self::assertNoSniffError($report, 28); |
72 | 39 | } |
73 | 40 |
|
74 | 41 | public function testClassSuffixedErrorOrExceptionIsNotAnExceptionButReported(): void |
@@ -117,9 +84,32 @@ public function testFixableFullyQualified(): void |
117 | 84 | self::assertAllFixedInFile($report); |
118 | 85 | } |
119 | 86 |
|
120 | | - private function getFileReport(): File |
| 87 | + public function testWithSpecialExceptionNames(): void |
121 | 88 | { |
122 | | - return self::checkFile(__DIR__ . '/data/fullyQualifiedExceptionNames.php'); |
| 89 | + $report = self::checkFile( |
| 90 | + __DIR__ . '/data/fullyQualifiedSpecialExceptionNames.php', |
| 91 | + [ |
| 92 | + 'specialExceptionNames' => [ |
| 93 | + 'Foo\SomeError', |
| 94 | + 'Lorem\Ipsum\SomeOtherError', |
| 95 | + ], |
| 96 | + ] |
| 97 | + ); |
| 98 | + |
| 99 | + // Throwing used exception |
| 100 | + self::assertSniffError($report, 12, FullyQualifiedExceptionsSniff::CODE_NON_FULLY_QUALIFIED_EXCEPTION, 'FooError'); |
| 101 | + |
| 102 | + // Catching partially used exception |
| 103 | + self::assertSniffError($report, 13, FullyQualifiedExceptionsSniff::CODE_NON_FULLY_QUALIFIED_EXCEPTION, 'Foo\SomeException'); |
| 104 | + |
| 105 | + // Throwing exception from same namespace |
| 106 | + self::assertSniffError($report, 18, FullyQualifiedExceptionsSniff::CODE_NON_FULLY_QUALIFIED_EXCEPTION, 'SomeOtherError'); |
| 107 | + |
| 108 | + // Catching fully qualified exception |
| 109 | + self::assertNoSniffError($report, 15); |
| 110 | + |
| 111 | + // Catching fully qualified exception from same namespace |
| 112 | + self::assertNoSniffError($report, 17); |
123 | 113 | } |
124 | 114 |
|
125 | 115 | } |
0 commit comments