|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use PhpStaticAnalysis\Attributes\Throws; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | + |
| 8 | +class ThrowsTest extends TestCase |
| 9 | +{ |
| 10 | + public function testMethodThrows(): void |
| 11 | + { |
| 12 | + $this->assertEquals(['Exception'], $this->methodThrows()); |
| 13 | + } |
| 14 | + |
| 15 | + public function testInvalidTypeMethodThrows(): void |
| 16 | + { |
| 17 | + $errorThrown = false; |
| 18 | + try { |
| 19 | + $this->invalidTypeMethodThrows(); |
| 20 | + } catch (TypeError) { |
| 21 | + $errorThrown = true; |
| 22 | + } |
| 23 | + $this->assertTrue($errorThrown); |
| 24 | + } |
| 25 | + |
| 26 | + public function testSeveralMethodThrows(): void |
| 27 | + { |
| 28 | + $this->assertEquals([ |
| 29 | + 'Exception', |
| 30 | + 'Exception' |
| 31 | + ], $this->severalMethodThrowss()); |
| 32 | + } |
| 33 | + |
| 34 | + public function testMultipleMethodThrows(): void |
| 35 | + { |
| 36 | + $this->assertEquals([ |
| 37 | + 'Exception', |
| 38 | + 'Exception' |
| 39 | + ], $this->multipleMethodThrowss()); |
| 40 | + } |
| 41 | + |
| 42 | + public function testFunctionThrows(): void |
| 43 | + { |
| 44 | + $this->assertEquals(['Exception'], functionThrows()); |
| 45 | + } |
| 46 | + |
| 47 | + #[Throws(Exception::class)] |
| 48 | + private function methodThrows(): array |
| 49 | + { |
| 50 | + return $this->getThrows(__FUNCTION__); |
| 51 | + } |
| 52 | + |
| 53 | + #[Throws(0)] |
| 54 | + private function invalidTypeMethodThrows(): array |
| 55 | + { |
| 56 | + return $this->getThrows(__FUNCTION__); |
| 57 | + } |
| 58 | + |
| 59 | + #[Throws( |
| 60 | + Exception::class, |
| 61 | + Exception::class, |
| 62 | + )] |
| 63 | + private function severalMethodThrowss(): array |
| 64 | + { |
| 65 | + return $this->getThrows(__FUNCTION__); |
| 66 | + } |
| 67 | + |
| 68 | + #[Throws(Exception::class)] |
| 69 | + #[Throws(Exception::class)] |
| 70 | + private function multipleMethodThrowss(): array |
| 71 | + { |
| 72 | + return $this->getThrows(__FUNCTION__); |
| 73 | + } |
| 74 | + |
| 75 | + private function getThrows(string $functionName): array |
| 76 | + { |
| 77 | + $reflection = new ReflectionMethod($this, $functionName); |
| 78 | + return self::getThrowsFromReflection($reflection); |
| 79 | + } |
| 80 | + |
| 81 | + public static function getThrowsFromReflection( |
| 82 | + ReflectionMethod | ReflectionFunction $reflection |
| 83 | + ): array { |
| 84 | + $attributes = $reflection->getAttributes(); |
| 85 | + $throwss = []; |
| 86 | + foreach ($attributes as $attribute) { |
| 87 | + if ($attribute->getName() === Throws::class) { |
| 88 | + $attribute->newInstance(); |
| 89 | + $throwss = array_merge($throwss, $attribute->getArguments()); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return $throwss; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +#[Throws(Exception::class)] |
| 98 | +function functionThrows(): array |
| 99 | +{ |
| 100 | + $reflection = new ReflectionFunction(__FUNCTION__); |
| 101 | + return ThrowsTest::getThrowsFromReflection($reflection); |
| 102 | +} |
0 commit comments