|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Okapi\Aop\Tests\Integration\RegexPatterns; |
| 4 | + |
| 5 | +use InvalidArgumentException; |
| 6 | +use Okapi\Aop\Attributes\After; |
| 7 | +use Okapi\Aop\Attributes\Around; |
| 8 | +use Okapi\Aop\Attributes\Before; |
| 9 | +use Okapi\Wildcards\Regex; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use ReflectionMethod; |
| 12 | + |
| 13 | +class RegexPatternsTest extends TestCase |
| 14 | +{ |
| 15 | + public function testAllAdviceTypesAcceptRegexObjects(): void |
| 16 | + { |
| 17 | + $class = new Regex('~^App\\\\Controller$~'); |
| 18 | + $method = new Regex('~^[a-z][a-z0-9_]*$~i'); |
| 19 | + foreach ([new Before($class, $method), new Around($class, $method), new After($class, $method)] as $advice) { |
| 20 | + static::assertSame($class, $advice->class); |
| 21 | + static::assertSame($method, $advice->method); |
| 22 | + static::assertTrue($class->matches('App\\Controller')); |
| 23 | + static::assertTrue($method->matches('SAVE')); |
| 24 | + static::assertFalse($method->matches('__construct')); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + public function testRegexAndWildcardPatternsCanBeMixed(): void |
| 29 | + { |
| 30 | + $wildcardClass = new After(class: 'App\\*', method: new Regex('~^save$~')); |
| 31 | + static::assertNotNull($wildcardClass->class); |
| 32 | + static::assertTrue($wildcardClass->class->matches('App\\Controller')); |
| 33 | + $wildcardMethod = new After(class: new Regex('~Controller$~'), method: 'save*'); |
| 34 | + static::assertNotNull($wildcardMethod->method); |
| 35 | + static::assertTrue($wildcardMethod->method->matches('saveAll')); |
| 36 | + static::assertNotNull($wildcardMethod->class); |
| 37 | + static::assertTrue($wildcardMethod->class->matches('App\\Controller')); |
| 38 | + } |
| 39 | + |
| 40 | + public function testRegexLookingStringsRemainWildcards(): void |
| 41 | + { |
| 42 | + $advice = new After(class: 'App\\*', method: '/^save$/'); |
| 43 | + static::assertNotNull($advice->method); |
| 44 | + static::assertTrue($advice->method->matches('/^save$/')); |
| 45 | + static::assertFalse($advice->method->matches('save')); |
| 46 | + } |
| 47 | + |
| 48 | + public function testAbsentPatternsRemainAbsent(): void |
| 49 | + { |
| 50 | + foreach ([new After(), new After('', ''), new After('0', '0')] as $advice) { |
| 51 | + static::assertNull($advice->class); |
| 52 | + static::assertNull($advice->method); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public function testInvalidClassRegexFailsDuringConstruction(): void |
| 57 | + { |
| 58 | + $this->expectException(InvalidArgumentException::class); |
| 59 | + $this->expectExceptionMessage('Invalid class regex'); |
| 60 | + new After(class: new Regex('/[/'), method: '*'); |
| 61 | + } |
| 62 | + |
| 63 | + public function testInvalidMethodRegexFailsDuringConstruction(): void |
| 64 | + { |
| 65 | + $this->expectException(InvalidArgumentException::class); |
| 66 | + $this->expectExceptionMessage('Invalid method regex'); |
| 67 | + new After(class: '*', method: new Regex('missing delimiters')); |
| 68 | + } |
| 69 | + |
| 70 | + public function testRegexObjectsCanBeUsedInPhpAttributes(): void |
| 71 | + { |
| 72 | + $reflection = new ReflectionMethod(AttributeFixture::class, 'advice'); |
| 73 | + $attribute = $reflection->getAttributes(After::class)[0]->newInstance(); |
| 74 | + static::assertInstanceOf(After::class, $attribute); |
| 75 | + static::assertNotNull($attribute->class); |
| 76 | + static::assertNotNull($attribute->method); |
| 77 | + static::assertTrue($attribute->class->matches('App\\Controller')); |
| 78 | + static::assertTrue($attribute->method->matches('save')); |
| 79 | + static::assertFalse($attribute->method->matches('__construct')); |
| 80 | + } |
| 81 | +} |
0 commit comments