diff --git a/tests/Factory/SentryFactoryTest.php b/tests/Factory/SentryFactoryTest.php index d1af7dd..6047cdb 100644 --- a/tests/Factory/SentryFactoryTest.php +++ b/tests/Factory/SentryFactoryTest.php @@ -5,6 +5,7 @@ namespace Consistence\Sentry\Factory; use Consistence\Sentry\Metadata\SentryIdentificator; +use PHPUnit\Framework\Assert; class SentryFactoryTest extends \PHPUnit\Framework\TestCase { @@ -13,7 +14,7 @@ public function testGetSentry(): void { $factory = $this->createMock(SentryFactory::class); $factory - ->expects($this->once()) + ->expects(self::once()) ->method('getSentry'); $factory->getSentry(new SentryIdentificator('string')); @@ -24,15 +25,15 @@ public function testNoSentry(): void $sentryIdentificator = new SentryIdentificator('string'); $factory = $this->createMock(SentryFactory::class); $factory - ->expects($this->once()) + ->expects(self::once()) ->method('getSentry') - ->will($this->throwException(new \Consistence\Sentry\Factory\NoSentryForIdentificatorException($sentryIdentificator))); + ->will(self::throwException(new \Consistence\Sentry\Factory\NoSentryForIdentificatorException($sentryIdentificator))); try { $factory->getSentry($sentryIdentificator); - $this->fail(); + Assert::fail('Exception expected'); } catch (\Consistence\Sentry\Factory\NoSentryForIdentificatorException $e) { - $this->assertSame($sentryIdentificator, $e->getSentryIdentificator()); + Assert::assertSame($sentryIdentificator, $e->getSentryIdentificator()); } } diff --git a/tests/Factory/Simple/SimpleSentryFactoryTest.php b/tests/Factory/Simple/SimpleSentryFactoryTest.php index a11852c..916b697 100644 --- a/tests/Factory/Simple/SimpleSentryFactoryTest.php +++ b/tests/Factory/Simple/SimpleSentryFactoryTest.php @@ -9,37 +9,85 @@ use Consistence\Sentry\Type\CollectionType; use Consistence\Sentry\Type\Sentry; use Consistence\Sentry\Type\SimpleType; +use Generator; +use PHPUnit\Framework\Assert; class SimpleSentryFactoryTest extends \PHPUnit\Framework\TestCase { /** - * @return mixed[][] + * @return mixed[][]|\Generator */ - public function sentryIdentificatorToSentryProvider(): array + public function sentryIdentificatorToSentryDataProvider(): Generator { - return [ - [new SentryIdentificator('string'), new SimpleType()], - [new SentryIdentificator('int'), new SimpleType()], - [new SentryIdentificator('bool'), new SimpleType()], - [new SentryIdentificator('float'), new SimpleType()], - [new SentryIdentificator('integer'), new SimpleType()], - [new SentryIdentificator('boolean'), new SimpleType()], - [new SentryIdentificator('mixed'), new SimpleType()], - [new SentryIdentificator('DateTimeImmutable'), new SimpleType()], - [new SentryIdentificator('string[]'), new CollectionType()], - [new SentryIdentificator('int[]'), new CollectionType()], - [new SentryIdentificator('bool[]'), new CollectionType()], - [new SentryIdentificator('float[]'), new CollectionType()], - [new SentryIdentificator('integer[]'), new CollectionType()], - [new SentryIdentificator('boolean[]'), new CollectionType()], - [new SentryIdentificator('mixed[]'), new CollectionType()], - [new SentryIdentificator('DateTimeImmutable[]'), new CollectionType()], + yield 'string' => [ + 'sentryIdentificator' => new SentryIdentificator('string'), + 'sentry' => new SimpleType(), + ]; + yield 'int' => [ + 'sentryIdentificator' => new SentryIdentificator('int'), + 'sentry' => new SimpleType(), + ]; + yield 'bool' => [ + 'sentryIdentificator' => new SentryIdentificator('bool'), + 'sentry' => new SimpleType(), + ]; + yield 'float' => [ + 'sentryIdentificator' => new SentryIdentificator('float'), + 'sentry' => new SimpleType(), + ]; + yield 'integer' => [ + 'sentryIdentificator' => new SentryIdentificator('integer'), + 'sentry' => new SimpleType(), + ]; + yield 'boolean' => [ + 'sentryIdentificator' => new SentryIdentificator('boolean'), + 'sentry' => new SimpleType(), + ]; + yield 'mixed' => [ + 'sentryIdentificator' => new SentryIdentificator('mixed'), + 'sentry' => new SimpleType(), + ]; + yield 'object' => [ + 'sentryIdentificator' => new SentryIdentificator('DateTimeImmutable'), + 'sentry' => new SimpleType(), + ]; + yield 'string array' => [ + 'sentryIdentificator' => new SentryIdentificator('string[]'), + 'sentry' => new CollectionType(), + ]; + yield 'int array' => [ + 'sentryIdentificator' => new SentryIdentificator('int[]'), + 'sentry' => new CollectionType(), + ]; + yield 'bool array' => [ + 'sentryIdentificator' => new SentryIdentificator('bool[]'), + 'sentry' => new CollectionType(), + ]; + yield 'float array' => [ + 'sentryIdentificator' => new SentryIdentificator('float[]'), + 'sentry' => new CollectionType(), + ]; + yield 'integer array' => [ + 'sentryIdentificator' => new SentryIdentificator('integer[]'), + 'sentry' => new CollectionType(), + ]; + yield 'boolean array' => [ + 'sentryIdentificator' => new SentryIdentificator('boolean[]'), + 'sentry' => new CollectionType(), + ]; + yield 'mixed array' => [ + 'sentryIdentificator' => new SentryIdentificator('mixed[]'), + 'sentry' => new CollectionType(), + ]; + yield 'object array' => [ + 'sentryIdentificator' => new SentryIdentificator('DateTimeImmutable[]'), + 'sentry' => new CollectionType(), ]; } /** - * @dataProvider sentryIdentificatorToSentryProvider + * @dataProvider sentryIdentificatorToSentryDataProvider * * @param \Consistence\Sentry\Metadata\SentryIdentificator $sentryIdentificator * @param \Consistence\Sentry\Type\Sentry $sentry @@ -47,33 +95,46 @@ public function sentryIdentificatorToSentryProvider(): array public function testGetSentry(SentryIdentificator $sentryIdentificator, Sentry $sentry): void { $factory = new SimpleSentryFactory(new SentryIdentificatorParser()); - $this->assertTrue($factory->getSentry($sentryIdentificator) instanceof $sentry); + Assert::assertTrue($factory->getSentry($sentryIdentificator) instanceof $sentry); } public function testSameSentryInstance(): void { $factory = new SimpleSentryFactory(new SentryIdentificatorParser()); $sentry = $factory->getSentry(new SentryIdentificator('string')); - $this->assertSame($sentry, $factory->getSentry(new SentryIdentificator('string'))); + Assert::assertSame($sentry, $factory->getSentry(new SentryIdentificator('string'))); } - public function testNoSentry(): void + /** + * @return mixed[][]|\Generator + */ + public function getSentryWhenNoSentryForIdentificatorDataProvider(): Generator { - $factory = new SimpleSentryFactory(new SentryIdentificatorParser()); - - $this->expectException(\Consistence\Sentry\Factory\NoSentryForIdentificatorException::class); - - $factory->getSentry(new SentryIdentificator('')); + yield 'empty string' => [ + 'sentryIdentificator' => new SentryIdentificator(''), + ]; + yield 'nonexistent object' => [ + 'sentryIdentificator' => new SentryIdentificator('Foo\Bar'), + ]; } - public function testNonexistingObject(): void + /** + * @dataProvider getSentryWhenNoSentryForIdentificatorDataProvider + * + * @param \Consistence\Sentry\Metadata\SentryIdentificator $sentryIdentificator + */ + public function testGetSentryWhenNoSentryForIdentificator( + SentryIdentificator $sentryIdentificator + ): void { $factory = new SimpleSentryFactory(new SentryIdentificatorParser()); - $this->expectException(\Consistence\Sentry\Factory\NoSentryForIdentificatorException::class); - $this->expectExceptionMessage('Foo\Bar'); - - $factory->getSentry(new SentryIdentificator('Foo\Bar')); + try { + $factory->getSentry($sentryIdentificator); + Assert::fail('Exception expected'); + } catch (\Consistence\Sentry\Factory\NoSentryForIdentificatorException $e) { + Assert::assertSame($sentryIdentificator, $e->getSentryIdentificator()); + } } } diff --git a/tests/Generated/SentryAutoloaderTest.php b/tests/Generated/SentryAutoloaderTest.php index ac1eca4..29a5366 100644 --- a/tests/Generated/SentryAutoloaderTest.php +++ b/tests/Generated/SentryAutoloaderTest.php @@ -4,6 +4,7 @@ namespace Consistence\Sentry\Generated; +use PHPUnit\Framework\Assert; use org\bovigo\vfs\vfsStream; class SentryAutoloaderTest extends \PHPUnit\Framework\TestCase @@ -21,19 +22,19 @@ public function testRebuild(): void $generator = $this->createMock(SentryGenerator::class); $generator - ->expects($this->once()) + ->expects(self::once()) ->method('generateAll') - ->will($this->returnValue($classMap)); + ->will(self::returnValue($classMap)); $autoloader = new SentryAutoloader($generator, $targetPath); - $this->assertFalse($autoloader->isClassMapReady()); + Assert::assertFalse($autoloader->isClassMapReady()); $autoloader->rebuild(); - $this->assertTrue($autoloader->isClassMapReady()); + Assert::assertTrue($autoloader->isClassMapReady()); - $this->assertEquals($classMap, require $targetPath); + Assert::assertEquals($classMap, require $targetPath); } } diff --git a/tests/Generated/SentryGeneratorTest.php b/tests/Generated/SentryGeneratorTest.php index 3b1cc24..e95edaf 100644 --- a/tests/Generated/SentryGeneratorTest.php +++ b/tests/Generated/SentryGeneratorTest.php @@ -12,6 +12,7 @@ use Consistence\Sentry\Metadata\SentryMethod; use Consistence\Sentry\MetadataSource\MetadataSource; use Consistence\Sentry\Type\Sentry; +use PHPUnit\Framework\Assert; use ReflectionClass; use org\bovigo\vfs\vfsStream; use org\bovigo\vfs\visitor\vfsStreamStructureVisitor; @@ -33,40 +34,40 @@ public function testGenerateClass(): void $fooProperty = $this->createMock(PropertyMetadata::class); $fooProperty - ->expects($this->once()) + ->expects(self::once()) ->method('getSentryIdentificator') - ->will($this->returnValue($sentryIdentificator)); + ->will(self::returnValue($sentryIdentificator)); $fooProperty - ->expects($this->once()) + ->expects(self::once()) ->method('getSentryMethods') - ->will($this->returnValue([$fooMethod])); + ->will(self::returnValue([$fooMethod])); $classMetadata = $this->createMock(ClassMetadata::class); $classMetadata - ->expects($this->once()) + ->expects(self::once()) ->method('getProperties') - ->will($this->returnValue([$fooProperty])); + ->will(self::returnValue([$fooProperty])); $metadataSource = $this->createMock(MetadataSource::class); $metadataSource - ->expects($this->once()) + ->expects(self::once()) ->method('getMetadataForClass') ->with($classReflection) - ->will($this->returnValue($classMetadata)); + ->will(self::returnValue($classMetadata)); $sentry = $this->createMock(Sentry::class); $sentry - ->expects($this->once()) + ->expects(self::once()) ->method('generateMethod') - ->with($this->identicalTo($fooProperty), $this->identicalTo($fooMethod)) - ->will($this->returnValue('function test() {}')); + ->with(Assert::identicalTo($fooProperty), Assert::identicalTo($fooMethod)) + ->will(self::returnValue('function test() {}')); $sentryFactory = $this->createMock(SentryFactory::class); $sentryFactory - ->expects($this->once()) + ->expects(self::once()) ->method('getSentry') - ->with($this->identicalTo($sentryIdentificator)) - ->will($this->returnValue($sentry)); + ->with(Assert::identicalTo($sentryIdentificator)) + ->will(self::returnValue($sentry)); $generator = new SentryGenerator( $classFinder, @@ -76,7 +77,7 @@ public function testGenerateClass(): void ); $fileName = $generator->generateClass($classReflection); - $this->assertFileExists($fileName); + Assert::assertFileExists($fileName); } public function testGenerateClassSkipMethodNameCaseInsensitive(): void @@ -91,38 +92,38 @@ public function testGenerateClassSkipMethodNameCaseInsensitive(): void $fooMethod = $this->createMock(SentryMethod::class); $fooMethod - ->expects($this->once()) + ->expects(self::once()) ->method('getMethodName') - ->will($this->returnValue('GETskipPROPERTY')); + ->will(self::returnValue('GETskipPROPERTY')); $fooProperty = $this->createMock(PropertyMetadata::class); $fooProperty - ->expects($this->once()) + ->expects(self::once()) ->method('getSentryIdentificator') - ->will($this->returnValue($sentryIdentificator)); + ->will(self::returnValue($sentryIdentificator)); $fooProperty - ->expects($this->once()) + ->expects(self::once()) ->method('getSentryMethods') - ->will($this->returnValue([$fooMethod])); + ->will(self::returnValue([$fooMethod])); $classMetadata = $this->createMock(ClassMetadata::class); $classMetadata - ->expects($this->once()) + ->expects(self::once()) ->method('getProperties') - ->will($this->returnValue([$fooProperty])); + ->will(self::returnValue([$fooProperty])); $metadataSource = $this->createMock(MetadataSource::class); $metadataSource - ->expects($this->once()) + ->expects(self::once()) ->method('getMetadataForClass') ->with($classReflection) - ->will($this->returnValue($classMetadata)); + ->will(self::returnValue($classMetadata)); $sentryFactory = $this->createMock(SentryFactory::class); $sentryFactory - ->expects($this->once()) + ->expects(self::once()) ->method('getSentry') - ->with($this->identicalTo($sentryIdentificator)); + ->with(Assert::identicalTo($sentryIdentificator)); $generator = new SentryGenerator( $classFinder, @@ -133,9 +134,9 @@ public function testGenerateClassSkipMethodNameCaseInsensitive(): void try { $generator->generateClass($classReflection); - $this->fail(); + Assert::fail('Exception expected'); } catch (\Consistence\Sentry\Generated\NoMethodsToBeGeneratedException $e) { - $this->assertSame($classMetadata, $e->getClassMetadata()); + Assert::assertSame($classMetadata, $e->getClassMetadata()); } } @@ -149,20 +150,20 @@ public function testGenerateClassNoSentryMethods(): void $classMetadata = $this->createMock(ClassMetadata::class); $classMetadata - ->expects($this->once()) + ->expects(self::once()) ->method('getProperties') - ->will($this->returnValue([])); + ->will(self::returnValue([])); $metadataSource = $this->createMock(MetadataSource::class); $metadataSource - ->expects($this->once()) + ->expects(self::once()) ->method('getMetadataForClass') ->with($classReflection) - ->will($this->returnValue($classMetadata)); + ->will(self::returnValue($classMetadata)); $sentryFactory = $this->createMock(SentryFactory::class); $sentryFactory - ->expects($this->never()) + ->expects(self::never()) ->method('getSentry'); $generator = new SentryGenerator( @@ -174,9 +175,9 @@ public function testGenerateClassNoSentryMethods(): void try { $generator->generateClass($classReflection); - $this->fail(); + Assert::fail('Exception expected'); } catch (\Consistence\Sentry\Generated\NoMethodsToBeGeneratedException $e) { - $this->assertSame($classMetadata, $e->getClassMetadata()); + Assert::assertSame($classMetadata, $e->getClassMetadata()); } } @@ -188,9 +189,9 @@ public function testGenerateAll(): void $classFinder = $this->createMock(ClassFinder::class); $classFinder - ->expects($this->once()) + ->expects(self::once()) ->method('findByInterface') - ->will($this->returnValue([FooClass::class])); + ->will(self::returnValue([FooClass::class])); $sentryIdentificator = new SentryIdentificator('string'); @@ -198,40 +199,40 @@ public function testGenerateAll(): void $fooProperty = $this->createMock(PropertyMetadata::class); $fooProperty - ->expects($this->once()) + ->expects(self::once()) ->method('getSentryIdentificator') - ->will($this->returnValue($sentryIdentificator)); + ->will(self::returnValue($sentryIdentificator)); $fooProperty - ->expects($this->once()) + ->expects(self::once()) ->method('getSentryMethods') - ->will($this->returnValue([$fooMethod])); + ->will(self::returnValue([$fooMethod])); $classMetadata = $this->createMock(ClassMetadata::class); $classMetadata - ->expects($this->once()) + ->expects(self::once()) ->method('getProperties') - ->will($this->returnValue([$fooProperty])); + ->will(self::returnValue([$fooProperty])); $metadataSource = $this->createMock(MetadataSource::class); $metadataSource - ->expects($this->once()) + ->expects(self::once()) ->method('getMetadataForClass') ->with($classReflection) - ->will($this->returnValue($classMetadata)); + ->will(self::returnValue($classMetadata)); $sentry = $this->createMock(Sentry::class); $sentry - ->expects($this->once()) + ->expects(self::once()) ->method('generateMethod') - ->with($this->identicalTo($fooProperty), $this->identicalTo($fooMethod)) - ->will($this->returnValue('function test() {}')); + ->with(Assert::identicalTo($fooProperty), Assert::identicalTo($fooMethod)) + ->will(self::returnValue('function test() {}')); $sentryFactory = $this->createMock(SentryFactory::class); $sentryFactory - ->expects($this->once()) + ->expects(self::once()) ->method('getSentry') - ->with($this->identicalTo($sentryIdentificator)) - ->will($this->returnValue($sentry)); + ->with(Assert::identicalTo($sentryIdentificator)) + ->will(self::returnValue($sentry)); $generator = new SentryGenerator( $classFinder, @@ -241,11 +242,11 @@ public function testGenerateAll(): void ); $generated = $generator->generateAll(); - $this->assertCount(1, $generated); + Assert::assertCount(1, $generated); $structure = vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure(); - $this->assertCount(1, $structure['sentry']); - $this->assertTrue(isset($structure['sentry']['Consistence_Sentry_Generated_FooClass.php'])); + Assert::assertCount(1, $structure['sentry']); + Assert::assertTrue(isset($structure['sentry']['Consistence_Sentry_Generated_FooClass.php'])); } public function testGenerateAllNoMethods(): void @@ -256,26 +257,26 @@ public function testGenerateAllNoMethods(): void $classFinder = $this->createMock(ClassFinder::class); $classFinder - ->expects($this->once()) + ->expects(self::once()) ->method('findByInterface') - ->will($this->returnValue([FooClass::class])); + ->will(self::returnValue([FooClass::class])); $classMetadata = $this->createMock(ClassMetadata::class); $classMetadata - ->expects($this->once()) + ->expects(self::once()) ->method('getProperties') - ->will($this->returnValue([])); + ->will(self::returnValue([])); $metadataSource = $this->createMock(MetadataSource::class); $metadataSource - ->expects($this->once()) + ->expects(self::once()) ->method('getMetadataForClass') ->with($classReflection) - ->will($this->returnValue($classMetadata)); + ->will(self::returnValue($classMetadata)); $sentryFactory = $this->createMock(SentryFactory::class); $sentryFactory - ->expects($this->never()) + ->expects(self::never()) ->method('getSentry'); $generator = new SentryGenerator( @@ -286,10 +287,10 @@ public function testGenerateAllNoMethods(): void ); $generated = $generator->generateAll(); - $this->assertCount(0, $generated); + Assert::assertCount(0, $generated); $structure = vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure(); - $this->assertCount(0, $structure['sentry']); + Assert::assertCount(0, $structure['sentry']); } } diff --git a/tests/Metadata/BidirectionalAssociationTest.php b/tests/Metadata/BidirectionalAssociationTest.php index 1857a03..ee40191 100644 --- a/tests/Metadata/BidirectionalAssociationTest.php +++ b/tests/Metadata/BidirectionalAssociationTest.php @@ -4,6 +4,9 @@ namespace Consistence\Sentry\Metadata; +use Generator; +use PHPUnit\Framework\Assert; + class BidirectionalAssociationTest extends \PHPUnit\Framework\TestCase { @@ -23,50 +26,93 @@ public function testCreate(): void $sentryMethods ); - $this->assertSame('FooClass', $bidirectionalAssociation->getTargetClass()); - $this->assertSame('fooProperty', $bidirectionalAssociation->getTargetProperty()); - $this->assertTrue($bidirectionalAssociation->getTargetType()->equalsValue(BidirectionalAssociationType::ONE)); + Assert::assertSame('FooClass', $bidirectionalAssociation->getTargetClass()); + Assert::assertSame('fooProperty', $bidirectionalAssociation->getTargetProperty()); + Assert::assertTrue($bidirectionalAssociation->getTargetType()->equalsValue(BidirectionalAssociationType::ONE)); } - public function testGetTargetMethodForType(): void + /** + * @return mixed[][]|\Generator + */ + public function getTargetMethodForTypeDataProvider(): Generator { - $sentryMethods = [ - new SentryMethod( - new SentryAccess('get'), - 'getFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ), - new SentryMethod( - new SentryAccess('set'), - 'setFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ), + yield 'two public methods with different SentryAccess type' => [ + 'sentryMethods' => [ + new SentryMethod( + new SentryAccess('get'), + 'getFoo', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ), + new SentryMethod( + new SentryAccess('set'), + 'setFoo', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ), + ], + 'visibility' => Visibility::get(Visibility::VISIBILITY_PUBLIC), + 'expectedMethodName' => 'setFoo', ]; - $bidirectionalAssociation = new BidirectionalAssociation( - 'FooClass', - 'fooProperty', - BidirectionalAssociationType::get(BidirectionalAssociationType::ONE), - $sentryMethods - ); - $targetMethod = $bidirectionalAssociation->getTargetMethodForType( - new SentryAccess('set'), - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $this->assertSame('setFoo', $targetMethod->getMethodName()); - $this->assertTrue($targetMethod->getMethodVisibility()->equalsValue(Visibility::VISIBILITY_PUBLIC)); - $this->assertTrue($targetMethod->getSentryAccess()->equals(new SentryAccess('set'))); + yield 'single public method, looser visibility' => [ + 'sentryMethods' => [ + new SentryMethod( + new SentryAccess('set'), + 'setFoo', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ), + ], + 'visibility' => Visibility::get(Visibility::VISIBILITY_PRIVATE), + 'expectedMethodName' => 'setFoo', + ]; + + yield 'one public and one private method with same SentryAccess type, pick by visibility' => [ + 'sentryMethods' => [ + new SentryMethod( + new SentryAccess('set'), + 'setPrivate', + Visibility::get(Visibility::VISIBILITY_PRIVATE) + ), + new SentryMethod( + new SentryAccess('set'), + 'setPublic', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ), + ], + 'visibility' => Visibility::get(Visibility::VISIBILITY_PUBLIC), + 'expectedMethodName' => 'setPublic', + ]; + + yield 'two public methods with same SentryAccess type, pick by order' => [ + 'sentryMethods' => [ + new SentryMethod( + new SentryAccess('set'), + 'setFirst', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ), + new SentryMethod( + new SentryAccess('set'), + 'setSecond', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ), + ], + 'visibility' => Visibility::get(Visibility::VISIBILITY_PUBLIC), + 'expectedMethodName' => 'setFirst', + ]; } - public function testGetTargetMethodForTypeLooserVisibility(): void + /** + * @dataProvider getTargetMethodForTypeDataProvider + * + * @param \Consistence\Sentry\Metadata\SentryMethod[] $sentryMethods + * @param \Consistence\Sentry\Metadata\Visibility $visibility + * @param string $expectedMethodName + */ + public function testGetTargetMethodForType( + array $sentryMethods, + Visibility $visibility, + string $expectedMethodName + ): void { - $sentryMethods = [ - new SentryMethod( - new SentryAccess('set'), - 'setFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ), - ]; $bidirectionalAssociation = new BidirectionalAssociation( 'FooClass', 'fooProperty', @@ -76,11 +122,11 @@ public function testGetTargetMethodForTypeLooserVisibility(): void $targetMethod = $bidirectionalAssociation->getTargetMethodForType( new SentryAccess('set'), - Visibility::get(Visibility::VISIBILITY_PRIVATE) + $visibility ); - $this->assertSame('setFoo', $targetMethod->getMethodName()); - $this->assertTrue($targetMethod->getMethodVisibility()->equalsValue(Visibility::VISIBILITY_PUBLIC)); - $this->assertTrue($targetMethod->getSentryAccess()->equals(new SentryAccess('set'))); + Assert::assertSame($expectedMethodName, $targetMethod->getMethodName()); + Assert::assertTrue($targetMethod->getMethodVisibility()->equalsValue(Visibility::VISIBILITY_PUBLIC)); + Assert::assertTrue($targetMethod->getSentryAccess()->equals(new SentryAccess('set'))); } public function testGetTargetMethodForTypeRequiredVisibilityNotFound(): void @@ -104,72 +150,12 @@ public function testGetTargetMethodForTypeRequiredVisibilityNotFound(): void new SentryAccess('set'), Visibility::get(Visibility::VISIBILITY_PUBLIC) ); - $this->fail(); + Assert::fail('Exception expected'); } catch (\Consistence\Sentry\Metadata\NoSuitableMethodException $e) { - $this->assertSame('FooClass', $e->getClassName()); - $this->assertSame('fooProperty', $e->getPropertyName()); - $this->assertTrue($e->getSentryAccess()->equals(new SentryAccess('set'))); + Assert::assertSame('FooClass', $e->getClassName()); + Assert::assertSame('fooProperty', $e->getPropertyName()); + Assert::assertTrue($e->getSentryAccess()->equals(new SentryAccess('set'))); } } - public function testGetTargetMethodForTypePickByVisibility(): void - { - $sentryMethods = [ - new SentryMethod( - new SentryAccess('set'), - 'setPrivate', - Visibility::get(Visibility::VISIBILITY_PRIVATE) - ), - new SentryMethod( - new SentryAccess('set'), - 'setPublic', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ), - ]; - $bidirectionalAssociation = new BidirectionalAssociation( - 'FooClass', - 'fooProperty', - BidirectionalAssociationType::get(BidirectionalAssociationType::ONE), - $sentryMethods - ); - - $targetMethod = $bidirectionalAssociation->getTargetMethodForType( - new SentryAccess('set'), - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $this->assertSame('setPublic', $targetMethod->getMethodName()); - $this->assertTrue($targetMethod->getMethodVisibility()->equalsValue(Visibility::VISIBILITY_PUBLIC)); - $this->assertTrue($targetMethod->getSentryAccess()->equals(new SentryAccess('set'))); - } - - public function testGetTargetMethodForTypeMultipleSentryAccessPickByOrder(): void - { - $sentryMethods = [ - new SentryMethod( - new SentryAccess('set'), - 'setFirst', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ), - new SentryMethod( - new SentryAccess('set'), - 'setSecond', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ), - ]; - $bidirectionalAssociation = new BidirectionalAssociation( - 'FooClass', - 'fooProperty', - BidirectionalAssociationType::get(BidirectionalAssociationType::ONE), - $sentryMethods - ); - - $targetMethod = $bidirectionalAssociation->getTargetMethodForType( - new SentryAccess('set'), - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $this->assertSame('setFirst', $targetMethod->getMethodName()); - $this->assertTrue($targetMethod->getMethodVisibility()->equalsValue(Visibility::VISIBILITY_PUBLIC)); - $this->assertTrue($targetMethod->getSentryAccess()->equals(new SentryAccess('set'))); - } - } diff --git a/tests/Metadata/ClassMetadataTest.php b/tests/Metadata/ClassMetadataTest.php index 1a78926..a79d03d 100644 --- a/tests/Metadata/ClassMetadataTest.php +++ b/tests/Metadata/ClassMetadataTest.php @@ -4,6 +4,8 @@ namespace Consistence\Sentry\Metadata; +use PHPUnit\Framework\Assert; + class ClassMetadataTest extends \PHPUnit\Framework\TestCase { @@ -25,8 +27,8 @@ public function testCreate(): void 'FooClass', $properties ); - $this->assertSame('FooClass', $classMetadata->getName()); - $this->assertSame($properties, $classMetadata->getProperties()); + Assert::assertSame('FooClass', $classMetadata->getName()); + Assert::assertSame($properties, $classMetadata->getProperties()); } public function testGetSentryMethodByNameAndRequiredVisibility(): void @@ -55,9 +57,9 @@ public function testGetSentryMethodByNameAndRequiredVisibility(): void 'fooMethod', Visibility::get(Visibility::VISIBILITY_PRIVATE) ); - $this->assertInstanceOf(SentryMethodSearchResult::class, $searchResult); - $this->assertSame($fooProperty, $searchResult->getProperty()); - $this->assertSame($fooMethod, $searchResult->getSentryMethod()); + Assert::assertInstanceOf(SentryMethodSearchResult::class, $searchResult); + Assert::assertSame($fooProperty, $searchResult->getProperty()); + Assert::assertSame($fooMethod, $searchResult->getSentryMethod()); } public function testGetSentryMethodByNameAndRequiredVisibilityMethodNotFound(): void @@ -81,10 +83,10 @@ public function testGetSentryMethodByNameAndRequiredVisibilityMethodNotFound(): 'fooMethod', Visibility::get(Visibility::VISIBILITY_PUBLIC) ); - $this->fail(); + Assert::fail('Exception expected'); } catch (\Consistence\Sentry\Metadata\MethodNotFoundException $e) { - $this->assertSame('FooClass', $e->getClassName()); - $this->assertSame('fooMethod', $e->getMethodName()); + Assert::assertSame('FooClass', $e->getClassName()); + Assert::assertSame('fooMethod', $e->getMethodName()); } } @@ -103,7 +105,7 @@ public function testGetPropertyByName(): void ); $classMetadata = new ClassMetadata('FooClass', [$fooProperty]); - $this->assertSame($fooProperty, $classMetadata->getPropertyByName($propertyName)); + Assert::assertSame($fooProperty, $classMetadata->getPropertyByName($propertyName)); } public function testGetPropertyByNameNotFound(): void @@ -114,10 +116,10 @@ public function testGetPropertyByNameNotFound(): void try { $classMetadata->getPropertyByName($propertyName); - $this->fail(); + Assert::fail('Exception expected'); } catch (\Consistence\Sentry\Metadata\PropertyNotFoundException $e) { - $this->assertSame($className, $e->getClassName()); - $this->assertSame($propertyName, $e->getPropertyName()); + Assert::assertSame($className, $e->getClassName()); + Assert::assertSame($propertyName, $e->getPropertyName()); } } diff --git a/tests/Metadata/PropertyMetadataTest.php b/tests/Metadata/PropertyMetadataTest.php index 9338ed0..f59bdb0 100644 --- a/tests/Metadata/PropertyMetadataTest.php +++ b/tests/Metadata/PropertyMetadataTest.php @@ -4,71 +4,102 @@ namespace Consistence\Sentry\Metadata; +use Generator; +use PHPUnit\Framework\Assert; + class PropertyMetadataTest extends \PHPUnit\Framework\TestCase { - public function testCreate(): void + /** + * @return mixed[][]|\Generator + */ + public function createDataProvider(): Generator { - $targetClass = 'BarClass'; - $sentryIdentificator = new SentryIdentificator($targetClass); - $sentryMethods = [ - new SentryMethod( - new SentryAccess('get'), - 'fooMethod', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ), - ]; - $bidirectionalAssociation = new BidirectionalAssociation( - $targetClass, - 'barProperty', - BidirectionalAssociationType::get(BidirectionalAssociationType::ONE), - [ - new SentryMethod( - new SentryAccess('get'), - 'barMethod', - Visibility::get(Visibility::VISIBILITY_PUBLIC) + yield 'create object' => (function (): array { + $targetType = 'BarClass'; + + return [ + 'name' => 'fooProperty', + 'className' => 'FooClass', + 'targetType' => $targetType, + 'sentryIdentificator' => new SentryIdentificator($targetType), + 'nullable' => false, + 'sentryMethods' => [ + new SentryMethod( + new SentryAccess('get'), + 'fooMethod', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ), + ], + 'bidirectionalAssociation' => new BidirectionalAssociation( + $targetType, + 'barProperty', + BidirectionalAssociationType::get(BidirectionalAssociationType::ONE), + [ + new SentryMethod( + new SentryAccess('get'), + 'barMethod', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ), + ] ), - ] - ); - $property = new PropertyMetadata( - 'fooProperty', - 'FooClass', - $targetClass, - $sentryIdentificator, - false, - $sentryMethods, - $bidirectionalAssociation - ); + ]; + })(); + + yield 'create scalar' => (function (): array { + $targetType = 'int'; - $this->assertSame('fooProperty', $property->getName()); - $this->assertSame('FooClass', $property->getClassName()); - $this->assertSame($targetClass, $property->getType()); - $this->assertSame($sentryIdentificator, $property->getSentryIdentificator()); - $this->assertSame(false, $property->isNullable()); - $this->assertSame($sentryMethods, $property->getSentryMethods()); - $this->assertSame($bidirectionalAssociation, $property->getBidirectionalAssociation()); + return [ + 'name' => 'fooProperty', + 'className' => 'FooClass', + 'targetType' => $targetType, + 'sentryIdentificator' => new SentryIdentificator($targetType), + 'nullable' => false, + 'sentryMethods' => [], + 'bidirectionalAssociation' => null, + ]; + })(); } - public function testCreateScalar(): void + /** + * @dataProvider createDataProvider + * + * @param string $name + * @param string $className + * @param string $targetType + * @param \Consistence\Sentry\Metadata\SentryIdentificator $sentryIdentificator + * @param bool $nullable + * @param \Consistence\Sentry\Metadata\SentryMethod[] $sentryMethods + * @param \Consistence\Sentry\Metadata\BidirectionalAssociation|null $bidirectionalAssociation + * @return void + */ + public function testCreate( + string $name, + string $className, + string $targetType, + SentryIdentificator $sentryIdentificator, + bool $nullable, + array $sentryMethods, + ?BidirectionalAssociation $bidirectionalAssociation + ): void { - $sentryIdentificator = new SentryIdentificator('int'); $property = new PropertyMetadata( - 'fooProperty', - 'FooClass', - 'int', + $name, + $className, + $targetType, $sentryIdentificator, - false, - [], - null + $nullable, + $sentryMethods, + $bidirectionalAssociation ); - $this->assertSame('fooProperty', $property->getName()); - $this->assertSame('FooClass', $property->getClassName()); - $this->assertSame('int', $property->getType()); - $this->assertSame($sentryIdentificator, $property->getSentryIdentificator()); - $this->assertSame(false, $property->isNullable()); - $this->assertEmpty($property->getSentryMethods()); - $this->assertNull($property->getBidirectionalAssociation()); + Assert::assertSame($name, $property->getName()); + Assert::assertSame($className, $property->getClassName()); + Assert::assertSame($targetType, $property->getType()); + Assert::assertSame($sentryIdentificator, $property->getSentryIdentificator()); + Assert::assertSame($nullable, $property->isNullable()); + Assert::assertSame($sentryMethods, $property->getSentryMethods()); + Assert::assertSame($bidirectionalAssociation, $property->getBidirectionalAssociation()); } public function testGetSentryMethodByAccess(): void @@ -98,7 +129,7 @@ public function testGetSentryMethodByAccess(): void null ); - $this->assertSame($setMethod, $property->getSentryMethodByAccessAndRequiredVisibility( + Assert::assertSame($setMethod, $property->getSentryMethodByAccessAndRequiredVisibility( new SentryAccess('set'), Visibility::get(Visibility::VISIBILITY_PRIVATE) )); @@ -113,9 +144,11 @@ public function testGetSentryMethodByAccessNotFound(): void 'setFoo', Visibility::get(Visibility::VISIBILITY_PRIVATE) ); + $className = 'FooClass'; + $propertyName = 'fooProperty'; $property = new PropertyMetadata( - 'fooProperty', - 'FooClass', + $propertyName, + $className, $targetClass, $sentryIdentificator, false, @@ -125,35 +158,91 @@ public function testGetSentryMethodByAccessNotFound(): void null ); - $this->expectException(\Consistence\Sentry\Metadata\NoSuitableMethodException::class); + $sentryAccess = new SentryAccess('set'); - $property->getSentryMethodByAccessAndRequiredVisibility( - new SentryAccess('set'), - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); + try { + $property->getSentryMethodByAccessAndRequiredVisibility( + $sentryAccess, + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ); + Assert::fail('Exception expected'); + } catch (\Consistence\Sentry\Metadata\NoSuitableMethodException $e) { + Assert::assertSame($className, $e->getClassName()); + Assert::assertSame($propertyName, $e->getPropertyName()); + Assert::assertSame($sentryAccess, $e->getSentryAccess()); + } } - public function testGetSentryMethodByNameAndRequiredVisibility(): void + /** + * @return mixed[][]|\Generator + */ + public function getSentryMethodByNameAndRequiredVisibilityDataProvider(): Generator { - $sentryIdentificator = new SentryIdentificator('string'); - $fooMethod = new SentryMethod( - new SentryAccess('get'), - 'fooMethod', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $property = new PropertyMetadata( - 'fooProperty', - 'FooClass', - 'string', - $sentryIdentificator, - false, - [$fooMethod], - null - ); + yield 'case sensitive' => (function (): array { + $fooMethod = new SentryMethod( + new SentryAccess('get'), + 'fooMethod', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ); - $this->assertSame($fooMethod, $property->getSentryMethodByNameAndRequiredVisibility( - 'fooMethod', - Visibility::get(Visibility::VISIBILITY_PRIVATE) + return [ + 'expectedSentryMethod' => $fooMethod, + 'propertyMetadata' => new PropertyMetadata( + 'fooProperty', + 'FooClass', + 'string', + new SentryIdentificator('string'), + false, + [$fooMethod], + null + ), + 'methodName' => 'fooMethod', + 'requiredVisibility' => Visibility::get(Visibility::VISIBILITY_PRIVATE), + ]; + })(); + + yield 'case insensitive' => (function (): array { + $fooMethod = new SentryMethod( + new SentryAccess('get'), + 'fooMethod', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ); + + return [ + 'expectedSentryMethod' => $fooMethod, + 'propertyMetadata' => new PropertyMetadata( + 'fooProperty', + 'FooClass', + 'string', + new SentryIdentificator('string'), + false, + [$fooMethod], + null + ), + 'methodName' => 'FOOMethod', + 'requiredVisibility' => Visibility::get(Visibility::VISIBILITY_PRIVATE), + ]; + })(); + } + + /** + * @dataProvider getSentryMethodByNameAndRequiredVisibilityDataProvider + * + * @param \Consistence\Sentry\Metadata\SentryMethod $expectedSentryMethod + * @param \Consistence\Sentry\Metadata\PropertyMetadata $propertyMetadata + * @param string $methodName + * @param \Consistence\Sentry\Metadata\Visibility $requiredVisibility + */ + public function testGetSentryMethodByNameAndRequiredVisibility( + SentryMethod $expectedSentryMethod, + PropertyMetadata $propertyMetadata, + string $methodName, + Visibility $requiredVisibility + ): void + { + Assert::assertSame($expectedSentryMethod, $propertyMetadata->getSentryMethodByNameAndRequiredVisibility( + $methodName, + $requiredVisibility )); } @@ -174,38 +263,14 @@ public function testGetSentryMethodByNameAndRequiredVisibilityMethodNotFound(): 'fooMethod', Visibility::get(Visibility::VISIBILITY_PUBLIC) ); - $this->fail(); + Assert::fail('Exception expected'); } catch (\Consistence\Sentry\Metadata\MethodNotFoundForPropertyException $e) { - $this->assertSame('FooClass', $e->getClassName()); - $this->assertSame('fooProperty', $e->getPropertyName()); - $this->assertSame('fooMethod', $e->getMethodName()); + Assert::assertSame('FooClass', $e->getClassName()); + Assert::assertSame('fooProperty', $e->getPropertyName()); + Assert::assertSame('fooMethod', $e->getMethodName()); } } - public function testGetSentryMethodByNameAndRequiredVisibilityCaseInsensitive(): void - { - $sentryIdentificator = new SentryIdentificator('string'); - $fooMethod = new SentryMethod( - new SentryAccess('get'), - 'fooMethod', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $property = new PropertyMetadata( - 'fooProperty', - 'FooClass', - 'string', - $sentryIdentificator, - false, - [$fooMethod], - null - ); - - $this->assertSame($fooMethod, $property->getSentryMethodByNameAndRequiredVisibility( - 'FOOmethod', - Visibility::get(Visibility::VISIBILITY_PRIVATE) - )); - } - public function testGetDefinedSentryAccess(): void { $targetClass = 'BarClass'; @@ -233,7 +298,7 @@ public function testGetDefinedSentryAccess(): void null ); - $this->assertEquals([new SentryAccess('set')], $property->getDefinedSentryAccess()); + Assert::assertEquals([new SentryAccess('set')], $property->getDefinedSentryAccess()); } } diff --git a/tests/Metadata/SentryAccessTest.php b/tests/Metadata/SentryAccessTest.php index 0c2db0f..abcb014 100644 --- a/tests/Metadata/SentryAccessTest.php +++ b/tests/Metadata/SentryAccessTest.php @@ -4,25 +4,49 @@ namespace Consistence\Sentry\Metadata; +use Generator; +use PHPUnit\Framework\Assert; + class SentryAccessTest extends \PHPUnit\Framework\TestCase { public function testCreate(): void { $sentryAccess = new SentryAccess('foo'); - $this->assertSame('foo', $sentryAccess->getName()); + Assert::assertSame('foo', $sentryAccess->getName()); } - public function testEquals(): void + /** + * @return mixed[][]|\Generator + */ + public function equalsDataProvider(): Generator { - $sentryAccess = new SentryAccess('foo'); - $this->assertTrue($sentryAccess->equals(new SentryAccess('foo'))); + yield 'same name' => [ + 'firstSentryAccess' => new SentryAccess('foo'), + 'secondSentryAccess' => new SentryAccess('foo'), + 'expectedEquals' => true, + ]; + yield 'different name' => [ + 'firstSentryAccess' => new SentryAccess('foo'), + 'secondSentryAccess' => new SentryAccess('bar'), + 'expectedEquals' => false, + ]; } - public function testNotEquals(): void + /** + * @dataProvider equalsDataProvider + * + * @param \Consistence\Sentry\Metadata\SentryAccess $firstSentryAccess + * @param \Consistence\Sentry\Metadata\SentryAccess $secondSentryAccess + * @param bool $expectedEquals + */ + public function testEquals( + SentryAccess $firstSentryAccess, + SentryAccess $secondSentryAccess, + bool $expectedEquals + ): void { - $sentryAccess = new SentryAccess('foo'); - $this->assertFalse($sentryAccess->equals(new SentryAccess('bar'))); + Assert::assertSame($expectedEquals, $firstSentryAccess->equals($secondSentryAccess)); } } diff --git a/tests/Metadata/SentryIdentificatorTest.php b/tests/Metadata/SentryIdentificatorTest.php index 83d15c9..3bc763f 100644 --- a/tests/Metadata/SentryIdentificatorTest.php +++ b/tests/Metadata/SentryIdentificatorTest.php @@ -4,34 +4,68 @@ namespace Consistence\Sentry\Metadata; +use Generator; +use PHPUnit\Framework\Assert; use stdClass; class SentryIdentificatorTest extends \PHPUnit\Framework\TestCase { - public function testCreate(): void + /** + * @return mixed[][]|\Generator + */ + public function createDataProvider(): Generator { - $sentryIdentificator = new SentryIdentificator('foo'); - $this->assertSame('foo', $sentryIdentificator->getId()); + yield 'id is string' => [ + 'id' => 'foo', + ]; + yield 'is is object' => [ + 'id' => new stdClass(), + ]; } - public function testCreateObjectId(): void + /** + * @dataProvider createDataProvider + * + * @param mixed $id + */ + public function testCreate($id): void { - $object = new stdClass(); - $sentryIdentificator = new SentryIdentificator($object); - $this->assertSame($object, $sentryIdentificator->getId()); + $sentryIdentificator = new SentryIdentificator($id); + Assert::assertSame($id, $sentryIdentificator->getId()); } - public function testEquals(): void + /** + * @return mixed[][]|\Generator + */ + public function equalsDataProvider(): Generator { - $sentryIdentificator = new SentryIdentificator('foo'); - $this->assertTrue($sentryIdentificator->equals(new SentryIdentificator('foo'))); + yield 'same name' => [ + 'firstSentryIdentificator' => new SentryIdentificator('foo'), + 'secondSentryIdentificator' => new SentryIdentificator('foo'), + 'expectedEquals' => true, + ]; + yield 'different name' => [ + 'firstSentryIdentificator' => new SentryIdentificator('foo'), + 'secondSentryIdentificator' => new SentryIdentificator('bar'), + 'expectedEquals' => false, + ]; } - public function testNotEquals(): void + /** + * @dataProvider equalsDataProvider + * + * @param \Consistence\Sentry\Metadata\SentryIdentificator $firstSentryIdentificator + * @param \Consistence\Sentry\Metadata\SentryIdentificator $secondSentryIdentificator + * @param bool $expectedEquals + */ + public function testEquals( + SentryIdentificator $firstSentryIdentificator, + SentryIdentificator $secondSentryIdentificator, + bool $expectedEquals + ): void { - $sentryIdentificator = new SentryIdentificator('foo'); - $this->assertFalse($sentryIdentificator->equals(new SentryIdentificator('bar'))); + Assert::assertSame($expectedEquals, $firstSentryIdentificator->equals($secondSentryIdentificator)); } } diff --git a/tests/Metadata/SentryMethodTest.php b/tests/Metadata/SentryMethodTest.php index f69e41c..2a438fb 100644 --- a/tests/Metadata/SentryMethodTest.php +++ b/tests/Metadata/SentryMethodTest.php @@ -4,6 +4,8 @@ namespace Consistence\Sentry\Metadata; +use PHPUnit\Framework\Assert; + class SentryMethodTest extends \PHPUnit\Framework\TestCase { @@ -15,9 +17,9 @@ public function testCreate(): void Visibility::get(Visibility::VISIBILITY_PUBLIC) ); - $this->assertTrue($sentryMethod->getSentryAccess()->equals(new SentryAccess('get'))); - $this->assertSame('foo', $sentryMethod->getMethodName()); - $this->assertSame(Visibility::get(Visibility::VISIBILITY_PUBLIC), $sentryMethod->getMethodVisibility()); + Assert::assertTrue($sentryMethod->getSentryAccess()->equals(new SentryAccess('get'))); + Assert::assertSame('foo', $sentryMethod->getMethodName()); + Assert::assertSame(Visibility::get(Visibility::VISIBILITY_PUBLIC), $sentryMethod->getMethodVisibility()); } } diff --git a/tests/Metadata/VisibilityTest.php b/tests/Metadata/VisibilityTest.php index db61b34..c68bc54 100644 --- a/tests/Metadata/VisibilityTest.php +++ b/tests/Metadata/VisibilityTest.php @@ -4,75 +4,136 @@ namespace Consistence\Sentry\Metadata; +use Generator; +use PHPUnit\Framework\Assert; + class VisibilityTest extends \PHPUnit\Framework\TestCase { - public function testCreate(): void + /** + * @return mixed[][]|\Generator + */ + public function validVisibilityValueDataProvider(): Generator { - $this->assertInstanceOf(Visibility::class, Visibility::get(Visibility::VISIBILITY_PRIVATE)); - $this->assertInstanceOf(Visibility::class, Visibility::get(Visibility::VISIBILITY_PROTECTED)); - $this->assertInstanceOf(Visibility::class, Visibility::get(Visibility::VISIBILITY_PUBLIC)); - } + yield 'private' => [ + 'value' => Visibility::VISIBILITY_PRIVATE, + ]; - public function testGetName(): void - { - $this->assertSame('public', Visibility::get(Visibility::VISIBILITY_PUBLIC)->getName()); - } + yield 'protected' => [ + 'value' => Visibility::VISIBILITY_PROTECTED, + ]; - public function testLooserOrEqualVisibilityPublic(): void - { - $public = Visibility::get(Visibility::VISIBILITY_PUBLIC); - $this->assertTrue($public->isLooserOrEqualTo(Visibility::get(Visibility::VISIBILITY_PUBLIC))); - $this->assertTrue($public->isLooserOrEqualTo(Visibility::get(Visibility::VISIBILITY_PROTECTED))); - $this->assertTrue($public->isLooserOrEqualTo(Visibility::get(Visibility::VISIBILITY_PRIVATE))); + yield 'public' => [ + 'value' => Visibility::VISIBILITY_PUBLIC, + ]; } - public function testLooserOrEqualVisibilityProtected(): void + /** + * @dataProvider validVisibilityValueDataProvider + * + * @param mixed $value + */ + public function testCreate($value): void { - $protected = Visibility::get(Visibility::VISIBILITY_PROTECTED); - $this->assertFalse($protected->isLooserOrEqualTo(Visibility::get(Visibility::VISIBILITY_PUBLIC))); - $this->assertTrue($protected->isLooserOrEqualTo(Visibility::get(Visibility::VISIBILITY_PROTECTED))); - $this->assertTrue($protected->isLooserOrEqualTo(Visibility::get(Visibility::VISIBILITY_PRIVATE))); + Assert::assertInstanceOf(Visibility::class, Visibility::get($value)); } - public function testLooserOrEqualVisibilityPrivate(): void + public function testGetName(): void { - $private = Visibility::get(Visibility::VISIBILITY_PRIVATE); - $this->assertFalse($private->isLooserOrEqualTo(Visibility::get(Visibility::VISIBILITY_PUBLIC))); - $this->assertFalse($private->isLooserOrEqualTo(Visibility::get(Visibility::VISIBILITY_PROTECTED))); - $this->assertTrue($private->isLooserOrEqualTo(Visibility::get(Visibility::VISIBILITY_PRIVATE))); + Assert::assertSame('public', Visibility::get(Visibility::VISIBILITY_PUBLIC)->getName()); } - public function testGetRequiredVisibilitySameClass(): void + /** + * @return mixed[][]|\Generator + */ + public function isLooserOrEqualToDataProvider(): Generator { - $this->assertEquals( - Visibility::get(Visibility::VISIBILITY_PRIVATE), - Visibility::getRequiredVisibility(FooClass::class, FooClass::class) - ); + yield 'public' => [ + 'visibility' => Visibility::get(Visibility::VISIBILITY_PUBLIC), + 'expectedIsLooserOrEqualToPublic' => true, + 'expectedIsLooserOrEqualToProtected' => true, + 'expectedIsLooserOrEqualToPrivate' => true, + ]; + + yield 'protected' => [ + 'visibility' => Visibility::get(Visibility::VISIBILITY_PROTECTED), + 'expectedIsLooserOrEqualToPublic' => false, + 'expectedIsLooserOrEqualToProtected' => true, + 'expectedIsLooserOrEqualToPrivate' => true, + ]; + + yield 'private' => [ + 'visibility' => Visibility::get(Visibility::VISIBILITY_PRIVATE), + 'expectedIsLooserOrEqualToPublic' => false, + 'expectedIsLooserOrEqualToProtected' => false, + 'expectedIsLooserOrEqualToPrivate' => true, + ]; } - public function testGetRequiredVisibilityClassExtends(): void + /** + * @dataProvider isLooserOrEqualToDataProvider + * + * @param \Consistence\Sentry\Metadata\Visibility $visibility + * @param bool $expectedIsLooserOrEqualToPublic + * @param bool $expectedIsLooserOrEqualToProtected + * @param bool $expectedIsLooserOrEqualToPrivate + */ + public function testIsLooserOrEqualTo( + Visibility $visibility, + bool $expectedIsLooserOrEqualToPublic, + bool $expectedIsLooserOrEqualToProtected, + bool $expectedIsLooserOrEqualToPrivate + ): void { - $this->assertEquals( - Visibility::get(Visibility::VISIBILITY_PROTECTED), - Visibility::getRequiredVisibility(FooClass::class, BarClass::class) - ); + Assert::assertSame($expectedIsLooserOrEqualToPublic, $visibility->isLooserOrEqualTo(Visibility::get(Visibility::VISIBILITY_PUBLIC))); + Assert::assertSame($expectedIsLooserOrEqualToProtected, $visibility->isLooserOrEqualTo(Visibility::get(Visibility::VISIBILITY_PROTECTED))); + Assert::assertSame($expectedIsLooserOrEqualToPrivate, $visibility->isLooserOrEqualTo(Visibility::get(Visibility::VISIBILITY_PRIVATE))); } - public function testGetRequiredVisibilityClassExtended(): void + /** + * @return mixed[][]|\Generator + */ + public function getRequiredVisibilityDataProvider(): Generator { - $this->assertEquals( - Visibility::get(Visibility::VISIBILITY_PROTECTED), - Visibility::getRequiredVisibility(BarClass::class, FooClass::class) - ); + yield 'same class' => [ + 'targetClassFqn' => FooClass::class, + 'originClassFqn' => FooClass::class, + 'expectedRequiredVisibility' => Visibility::get(Visibility::VISIBILITY_PRIVATE), + ]; + + yield 'class extends' => [ + 'targetClassFqn' => FooClass::class, + 'originClassFqn' => BarClass::class, + 'expectedRequiredVisibility' => Visibility::get(Visibility::VISIBILITY_PROTECTED), + ]; + + yield 'class extended' => [ + 'targetClassFqn' => BarClass::class, + 'originClassFqn' => FooClass::class, + 'expectedRequiredVisibility' => Visibility::get(Visibility::VISIBILITY_PROTECTED), + ]; + + yield 'no relation classes' => [ + 'targetClassFqn' => FooClass::class, + 'originClassFqn' => BazClass::class, + 'expectedRequiredVisibility' => Visibility::get(Visibility::VISIBILITY_PUBLIC), + ]; } - public function testGetRequiredVisibilityNoRelationClasses(): void + /** + * @dataProvider getRequiredVisibilityDataProvider + * + * @param string $targetClassFqn + * @param string $originClassFqn + * @param \Consistence\Sentry\Metadata\Visibility $expectedRequiredVisibility + */ + public function testGetRequiredVisibility( + string $targetClassFqn, + string $originClassFqn, + Visibility $expectedRequiredVisibility + ): void { - $this->assertEquals( - Visibility::get(Visibility::VISIBILITY_PUBLIC), - Visibility::getRequiredVisibility(FooClass::class, BazClass::class) - ); + Assert::assertSame($expectedRequiredVisibility, Visibility::getRequiredVisibility($targetClassFqn, $originClassFqn)); } } diff --git a/tests/MetadataSource/Annotation/AnnotationMetadataSourceTest.php b/tests/MetadataSource/Annotation/AnnotationMetadataSourceTest.php index 53193ce..5ed26cd 100644 --- a/tests/MetadataSource/Annotation/AnnotationMetadataSourceTest.php +++ b/tests/MetadataSource/Annotation/AnnotationMetadataSourceTest.php @@ -4,270 +4,279 @@ namespace Consistence\Sentry\MetadataSource\Annotation; +use Closure; use Consistence\Annotation\Annotation; use Consistence\Annotation\AnnotationField; use Consistence\Annotation\AnnotationProvider; use Consistence\Sentry\Factory\SentryFactory; +use Consistence\Sentry\Metadata\PropertyMetadata; use Consistence\Sentry\Metadata\SentryAccess; use Consistence\Sentry\Metadata\SentryIdentificator; +use Consistence\Sentry\Metadata\SentryMethod; use Consistence\Sentry\Metadata\Visibility; use Consistence\Sentry\MetadataSource\FooClass; use Consistence\Sentry\SentryIdentificatorParser\SentryIdentificatorParser; use Consistence\Sentry\Type\SimpleType; +use Consistence\Type\ArrayType\ArrayType; +use Generator; +use PHPUnit\Framework\Assert; use ReflectionClass; use ReflectionProperty; class AnnotationMetadataSourceTest extends \PHPUnit\Framework\TestCase { - public function testGet(): void + /** + * @return mixed[][]|\Generator + */ + public function getMetadataForClassDataProvider(): Generator { - $type = 'string'; - $className = FooClass::class; - $propertyName = 'fooProperty'; - $classReflection = new ReflectionClass($className); - $sentryIdentificator = new SentryIdentificator($className . '::' . $type); - - $sentryIdentificatorAnnotation = Annotation::createAnnotationWithValue( - AnnotationMetadataSource::IDENTIFICATOR_ANNOTATION, - $type - ); - - $getAnnotationsCallback = function (ReflectionProperty $property, string $annotationName): array { - switch ($annotationName) { - case 'get': - return [Annotation::createAnnotationWithFields('get', [])]; - case 'set': - return []; - } - }; - - $sentryFactory = $this->createMock(SentryFactory::class); - $sentryFactory - ->expects($this->once()) - ->method('getSentry') - ->with($sentryIdentificator) - ->will($this->returnValue(new SimpleType())); - - $annotationProvider = $this->createMock(AnnotationProvider::class); - $annotationProvider - ->expects($this->once()) - ->method('getPropertyAnnotation') - ->with($classReflection->getProperty($propertyName), $this->isType('string')) - ->will($this->returnValue($sentryIdentificatorAnnotation)); - $annotationProvider - ->expects($this->exactly(2)) - ->method('getPropertyAnnotations') - ->will($this->returnCallback($getAnnotationsCallback)); - - $metadataSource = new AnnotationMetadataSource( - $sentryFactory, - new SentryIdentificatorParser(), - $annotationProvider - ); - $classMetadata = $metadataSource->getMetadataForClass($classReflection); - - $this->assertSame($className, $classMetadata->getName()); - $properties = $classMetadata->getProperties(); - $this->assertCount(1, $properties); - $fooPropety = $properties[0]; - $this->assertSame($propertyName, $fooPropety->getName()); - $this->assertSame($className, $fooPropety->getClassName()); - $this->assertSame($type, $fooPropety->getType()); - $this->assertTrue($sentryIdentificator->equals($fooPropety->getSentryIdentificator())); - $this->assertFalse($fooPropety->isNullable()); - $sentryMethods = $fooPropety->getSentryMethods(); - $this->assertCount(1, $sentryMethods); - $getMethod = $sentryMethods[0]; - $this->assertSame('getFooProperty', $getMethod->getMethodName()); - $this->assertSame(Visibility::get(Visibility::VISIBILITY_PUBLIC), $getMethod->getMethodVisibility()); - $this->assertTrue($getMethod->getSentryAccess()->equals(new SentryAccess('get'))); - $this->assertNull($fooPropety->getBidirectionalAssociation()); + yield 'get' => (function (): array { + $className = FooClass::class; + $propertyName = 'fooProperty'; + $propertyType = 'string'; + + return [ + 'className' => $className, + 'propertyName' => $propertyName, + 'propertyType' => $propertyType, + 'getAnnotationsCallback' => function (ReflectionProperty $property, string $annotationName): array { + switch ($annotationName) { + case 'get': + return [Annotation::createAnnotationWithFields('get', [])]; + case 'set': + return []; + } + }, + 'expectedPropertyMetadata' => new PropertyMetadata( + $propertyName, + $className, + $propertyType, + new SentryIdentificator(sprintf('%s::%s', $className, $propertyType)), + false, + [ + new SentryMethod( + new SentryAccess('get'), + 'getFooProperty', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ), + ], + null + ), + ]; + })(); + + yield 'custom method name' => (function (): array { + $className = FooClass::class; + $propertyName = 'fooProperty'; + $propertyType = 'string'; + + return [ + 'className' => $className, + 'propertyName' => $propertyName, + 'propertyType' => $propertyType, + 'getAnnotationsCallback' => function (ReflectionProperty $property, string $annotationName): array { + switch ($annotationName) { + case 'get': + return [Annotation::createAnnotationWithFields('get', [ + new AnnotationField(AnnotationMetadataSource::METHOD_PARAM_NAME, 'test'), + ])]; + case 'set': + return []; + } + }, + 'expectedPropertyMetadata' => new PropertyMetadata( + $propertyName, + $className, + $propertyType, + new SentryIdentificator(sprintf('%s::%s', $className, $propertyType)), + false, + [ + new SentryMethod( + new SentryAccess('get'), + 'test', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ), + ], + null + ), + ]; + })(); + + yield 'custom method visibility' => (function (): array { + $className = FooClass::class; + $propertyName = 'fooProperty'; + $propertyType = 'string'; + + return [ + 'className' => $className, + 'propertyName' => $propertyName, + 'propertyType' => $propertyType, + 'getAnnotationsCallback' => function (ReflectionProperty $property, string $annotationName): array { + switch ($annotationName) { + case 'get': + return [Annotation::createAnnotationWithFields('get', [ + new AnnotationField(AnnotationMetadataSource::METHOD_PARAM_VISIBILITY, Visibility::VISIBILITY_PRIVATE), + ])]; + case 'set': + return []; + } + }, + 'expectedPropertyMetadata' => new PropertyMetadata( + $propertyName, + $className, + $propertyType, + new SentryIdentificator(sprintf('%s::%s', $className, $propertyType)), + false, + [ + new SentryMethod( + new SentryAccess('get'), + 'getFooProperty', + Visibility::get(Visibility::VISIBILITY_PRIVATE) + ), + ], + null + ), + ]; + })(); + + yield 'multiple methods' => (function (): array { + $className = FooClass::class; + $propertyName = 'fooProperty'; + $propertyType = 'string'; + + return [ + 'className' => $className, + 'propertyName' => $propertyName, + 'propertyType' => $propertyType, + 'getAnnotationsCallback' => function (ReflectionProperty $property, string $annotationName): array { + switch ($annotationName) { + case 'get': + return [ + Annotation::createAnnotationWithFields('get', []), + Annotation::createAnnotationWithFields('get', [ + new AnnotationField(AnnotationMetadataSource::METHOD_PARAM_NAME, 'getFooPrivate'), + new AnnotationField(AnnotationMetadataSource::METHOD_PARAM_VISIBILITY, Visibility::VISIBILITY_PRIVATE), + ]), + ]; + case 'set': + return [ + Annotation::createAnnotationWithFields('set', []), + Annotation::createAnnotationWithFields('set', [ + new AnnotationField(AnnotationMetadataSource::METHOD_PARAM_NAME, 'setFooPrivate'), + new AnnotationField(AnnotationMetadataSource::METHOD_PARAM_VISIBILITY, Visibility::VISIBILITY_PRIVATE), + ]), + ]; + } + }, + 'expectedPropertyMetadata' => new PropertyMetadata( + $propertyName, + $className, + $propertyType, + new SentryIdentificator(sprintf('%s::%s', $className, $propertyType)), + false, + [ + new SentryMethod( + new SentryAccess('get'), + 'getFooProperty', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ), + new SentryMethod( + new SentryAccess('get'), + 'getFooPrivate', + Visibility::get(Visibility::VISIBILITY_PRIVATE) + ), + new SentryMethod( + new SentryAccess('set'), + 'setFooProperty', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ), + new SentryMethod( + new SentryAccess('set'), + 'setFooPrivate', + Visibility::get(Visibility::VISIBILITY_PRIVATE) + ), + ], + null + ), + ]; + })(); } - public function testCustomMethodName(): void + /** + * @dataProvider getMetadataForClassDataProvider + * + * @param string $className + * @param string $propertyName + * @param string $propertyType + * @param \Closure $getAnnotationsCallback + * @param \Consistence\Sentry\Metadata\PropertyMetadata $expectedPropertyMetadata + */ + public function testGetMetadataForClass( + string $className, + string $propertyName, + string $propertyType, + Closure $getAnnotationsCallback, + PropertyMetadata $expectedPropertyMetadata + ): void { - $type = 'string'; - $className = FooClass::class; - $propertyName = 'fooProperty'; $classReflection = new ReflectionClass($className); - $sentryIdentificator = new SentryIdentificator($className . '::' . $type); + $sentryIdentificator = new SentryIdentificator($className . '::' . $propertyType); $sentryIdentificatorAnnotation = Annotation::createAnnotationWithValue( AnnotationMetadataSource::IDENTIFICATOR_ANNOTATION, - $type + $propertyType ); - $getAnnotationsCallback = function (ReflectionProperty $property, string $annotationName): array { - switch ($annotationName) { - case 'get': - return [Annotation::createAnnotationWithFields('get', [ - new AnnotationField(AnnotationMetadataSource::METHOD_PARAM_NAME, 'test'), - ])]; - case 'set': - return []; - } - }; - $sentryFactory = $this->createMock(SentryFactory::class); $sentryFactory - ->expects($this->once()) + ->expects(self::once()) ->method('getSentry') ->with($sentryIdentificator) - ->will($this->returnValue(new SimpleType())); + ->will(self::returnValue(new SimpleType())); $annotationProvider = $this->createMock(AnnotationProvider::class); $annotationProvider - ->expects($this->once()) + ->expects(self::once()) ->method('getPropertyAnnotation') - ->with($classReflection->getProperty($propertyName), $this->isType('string')) - ->will($this->returnValue($sentryIdentificatorAnnotation)); + ->with($classReflection->getProperty($propertyName), Assert::isType('string')) + ->will(self::returnValue($sentryIdentificatorAnnotation)); $annotationProvider - ->expects($this->exactly(2)) + ->expects(self::exactly(2)) ->method('getPropertyAnnotations') - ->will($this->returnCallback($getAnnotationsCallback)); + ->will(self::returnCallback($getAnnotationsCallback)); $metadataSource = new AnnotationMetadataSource( $sentryFactory, new SentryIdentificatorParser(), $annotationProvider ); - $classMetadata = $metadataSource->getMetadataForClass($classReflection); - - $this->assertSame($className, $classMetadata->getName()); - $properties = $classMetadata->getProperties(); - $this->assertCount(1, $properties); - $fooPropety = $properties[0]; - $sentryMethods = $fooPropety->getSentryMethods(); - $this->assertCount(1, $sentryMethods); - $getMethod = $sentryMethods[0]; - $this->assertSame('test', $getMethod->getMethodName()); - } - - public function testCustomMethodVisibility(): void - { - $type = 'string'; - $className = FooClass::class; - $propertyName = 'fooProperty'; - $classReflection = new ReflectionClass($className); - $sentryIdentificator = new SentryIdentificator($className . '::' . $type); - $sentryIdentificatorAnnotation = Annotation::createAnnotationWithValue( - AnnotationMetadataSource::IDENTIFICATOR_ANNOTATION, - $type - ); - - $getAnnotationsCallback = function (ReflectionProperty $property, string $annotationName): array { - switch ($annotationName) { - case 'get': - return [Annotation::createAnnotationWithFields('get', [ - new AnnotationField(AnnotationMetadataSource::METHOD_PARAM_VISIBILITY, Visibility::VISIBILITY_PRIVATE), - ])]; - case 'set': - return []; - } - }; - - $sentryFactory = $this->createMock(SentryFactory::class); - $sentryFactory - ->expects($this->once()) - ->method('getSentry') - ->with($sentryIdentificator) - ->will($this->returnValue(new SimpleType())); - - $annotationProvider = $this->createMock(AnnotationProvider::class); - $annotationProvider - ->expects($this->once()) - ->method('getPropertyAnnotation') - ->with($classReflection->getProperty($propertyName), $this->isType('string')) - ->will($this->returnValue($sentryIdentificatorAnnotation)); - $annotationProvider - ->expects($this->exactly(2)) - ->method('getPropertyAnnotations') - ->will($this->returnCallback($getAnnotationsCallback)); - - $metadataSource = new AnnotationMetadataSource( - $sentryFactory, - new SentryIdentificatorParser(), - $annotationProvider - ); $classMetadata = $metadataSource->getMetadataForClass($classReflection); + Assert::assertSame($expectedPropertyMetadata->getClassName(), $classMetadata->getName()); - $this->assertSame($className, $classMetadata->getName()); $properties = $classMetadata->getProperties(); - $this->assertCount(1, $properties); - $fooPropety = $properties[0]; - $sentryMethods = $fooPropety->getSentryMethods(); - $this->assertCount(1, $sentryMethods); - $getMethod = $sentryMethods[0]; - $this->assertSame(Visibility::get(Visibility::VISIBILITY_PRIVATE), $getMethod->getMethodVisibility()); - } - - public function testGetMultipleMethods(): void - { - $type = 'string'; - $className = FooClass::class; - $propertyName = 'fooProperty'; - $classReflection = new ReflectionClass($className); - $sentryIdentificator = new SentryIdentificator($className . '::' . $type); - - $sentryIdentificatorAnnotation = Annotation::createAnnotationWithValue( - AnnotationMetadataSource::IDENTIFICATOR_ANNOTATION, - $type - ); - - $getAnnotationsCallback = function (ReflectionProperty $property, string $annotationName): array { - switch ($annotationName) { - case 'get': - return [ - Annotation::createAnnotationWithFields('get', []), - Annotation::createAnnotationWithFields('get', [ - new AnnotationField(AnnotationMetadataSource::METHOD_PARAM_NAME, 'getFooPrivate'), - new AnnotationField(AnnotationMetadataSource::METHOD_PARAM_VISIBILITY, Visibility::VISIBILITY_PRIVATE), - ]), - ]; - case 'set': - return [ - Annotation::createAnnotationWithFields('set', []), - Annotation::createAnnotationWithFields('set', [ - new AnnotationField(AnnotationMetadataSource::METHOD_PARAM_NAME, 'setFooPrivate'), - new AnnotationField(AnnotationMetadataSource::METHOD_PARAM_VISIBILITY, Visibility::VISIBILITY_PRIVATE), - ]), - ]; - } - }; - - $sentryFactory = $this->createMock(SentryFactory::class); - $sentryFactory - ->expects($this->once()) - ->method('getSentry') - ->with($sentryIdentificator) - ->will($this->returnValue(new SimpleType())); - - $annotationProvider = $this->createMock(AnnotationProvider::class); - $annotationProvider - ->expects($this->once()) - ->method('getPropertyAnnotation') - ->with($classReflection->getProperty($propertyName), $this->isType('string')) - ->will($this->returnValue($sentryIdentificatorAnnotation)); - $annotationProvider - ->expects($this->exactly(2)) - ->method('getPropertyAnnotations') - ->will($this->returnCallback($getAnnotationsCallback)); - - $metadataSource = new AnnotationMetadataSource( - $sentryFactory, - new SentryIdentificatorParser(), - $annotationProvider - ); - $classMetadata = $metadataSource->getMetadataForClass($classReflection); - - $this->assertSame($className, $classMetadata->getName()); - $properties = $classMetadata->getProperties(); - $this->assertCount(1, $properties); - $fooPropety = $properties[0]; - $sentryMethods = $fooPropety->getSentryMethods(); - $this->assertCount(4, $sentryMethods); + Assert::assertCount(1, $properties); + + $property = $properties[0]; + Assert::assertSame($expectedPropertyMetadata->getName(), $property->getName()); + Assert::assertSame($expectedPropertyMetadata->getClassName(), $property->getClassName()); + Assert::assertSame($expectedPropertyMetadata->getType(), $property->getType()); + Assert::assertTrue($expectedPropertyMetadata->getSentryIdentificator()->equals($property->getSentryIdentificator())); + Assert::assertSame($expectedPropertyMetadata->isNullable(), $property->isNullable()); + Assert::assertSame($expectedPropertyMetadata->getBidirectionalAssociation(), $property->getBidirectionalAssociation()); + + foreach ($expectedPropertyMetadata->getSentryMethods() as $expectedSentryMethod) { + Assert::assertTrue(ArrayType::containsValueByValueCallback( + $property->getSentryMethods(), + static function (SentryMethod $sentryMethod) use ($expectedSentryMethod): bool { + return $expectedSentryMethod->getMethodName() === $sentryMethod->getMethodName() + && $expectedSentryMethod->getSentryAccess()->equals($sentryMethod->getSentryAccess()) + && $expectedSentryMethod->getMethodVisibility()->equals($sentryMethod->getMethodVisibility()); + } + )); + } + Assert::assertCount(count($expectedPropertyMetadata->getSentryMethods()), $property->getSentryMethods()); } public function testClassIsNotSentryAware(): void @@ -276,16 +285,21 @@ public function testClassIsNotSentryAware(): void $annotationProvider = $this->createMock(AnnotationProvider::class); - $metedataSource = new AnnotationMetadataSource( + $metadataSource = new AnnotationMetadataSource( $sentryFactory, new SentryIdentificatorParser(), $annotationProvider ); - $this->expectException(\Consistence\Sentry\MetadataSource\ClassMetadataCouldNotBeCreatedException::class); - $this->expectExceptionMessage('SentryAware'); + $reflectionClass = new ReflectionClass($this); - $metedataSource->getMetadataForClass(new ReflectionClass($this)); + try { + $metadataSource->getMetadataForClass($reflectionClass); + Assert::fail('Exception expected'); + } catch (\Consistence\Sentry\MetadataSource\ClassMetadataCouldNotBeCreatedException $e) { + Assert::assertSame($reflectionClass, $e->getClassReflection()); + Assert::assertStringContainsString('SentryAware', $e->getMessage()); + } } public function testInvalidSentryIdentificator(): void @@ -301,19 +315,19 @@ public function testInvalidSentryIdentificator(): void $annotationProvider = $this->createMock(AnnotationProvider::class); $annotationProvider - ->expects($this->once()) + ->expects(self::once()) ->method('getPropertyAnnotation') ->with($classReflection->getProperty('fooProperty'), AnnotationMetadataSource::IDENTIFICATOR_ANNOTATION) - ->will($this->returnValue($sentryIdentificatorAnnotation)); + ->will(self::returnValue($sentryIdentificatorAnnotation)); - $metedataSource = new AnnotationMetadataSource( + $metadataSource = new AnnotationMetadataSource( $sentryFactory, new SentryIdentificatorParser(), $annotationProvider ); - $classMetadata = $metedataSource->getMetadataForClass($classReflection); - $this->assertEmpty($classMetadata->getProperties()); + $classMetadata = $metadataSource->getMetadataForClass($classReflection); + Assert::assertEmpty($classMetadata->getProperties()); } public function testMissingSentryIdentificator(): void @@ -325,22 +339,22 @@ public function testMissingSentryIdentificator(): void $annotationProvider = $this->createMock(AnnotationProvider::class); $annotationProvider - ->expects($this->once()) + ->expects(self::once()) ->method('getPropertyAnnotation') ->with($propertyReflection, AnnotationMetadataSource::IDENTIFICATOR_ANNOTATION) - ->will($this->throwException(new \Consistence\Annotation\AnnotationNotFoundException( + ->will(self::throwException(new \Consistence\Annotation\AnnotationNotFoundException( AnnotationMetadataSource::IDENTIFICATOR_ANNOTATION, $propertyReflection ))); - $metedataSource = new AnnotationMetadataSource( + $metadataSource = new AnnotationMetadataSource( $sentryFactory, new SentryIdentificatorParser(), $annotationProvider ); - $classMetadata = $metedataSource->getMetadataForClass($classReflection); - $this->assertEmpty($classMetadata->getProperties()); + $classMetadata = $metadataSource->getMetadataForClass($classReflection); + Assert::assertEmpty($classMetadata->getProperties()); } public function testNoSentryFound(): void @@ -350,10 +364,10 @@ public function testNoSentryFound(): void $sentryFactory = $this->createMock(SentryFactory::class); $sentryFactory - ->expects($this->once()) + ->expects(self::once()) ->method('getSentry') ->with($sentryIdentificator) - ->will($this->throwException(new \Consistence\Sentry\Factory\NoSentryForIdentificatorException($sentryIdentificator))); + ->will(self::throwException(new \Consistence\Sentry\Factory\NoSentryForIdentificatorException($sentryIdentificator))); $sentryIdentificatorAnnotation = Annotation::createAnnotationWithValue( AnnotationMetadataSource::IDENTIFICATOR_ANNOTATION, @@ -362,19 +376,19 @@ public function testNoSentryFound(): void $annotationProvider = $this->createMock(AnnotationProvider::class); $annotationProvider - ->expects($this->once()) + ->expects(self::once()) ->method('getPropertyAnnotation') ->with($classReflection->getProperty('fooProperty'), AnnotationMetadataSource::IDENTIFICATOR_ANNOTATION) - ->will($this->returnValue($sentryIdentificatorAnnotation)); + ->will(self::returnValue($sentryIdentificatorAnnotation)); - $metedataSource = new AnnotationMetadataSource( + $metadataSource = new AnnotationMetadataSource( $sentryFactory, new SentryIdentificatorParser(), $annotationProvider ); - $classMetadata = $metedataSource->getMetadataForClass($classReflection); - $this->assertEmpty($classMetadata->getProperties()); + $classMetadata = $metadataSource->getMetadataForClass($classReflection); + Assert::assertEmpty($classMetadata->getProperties()); } } diff --git a/tests/MetadataSource/MetadataSourceTest.php b/tests/MetadataSource/MetadataSourceTest.php index 8b44da7..8fca3cb 100644 --- a/tests/MetadataSource/MetadataSourceTest.php +++ b/tests/MetadataSource/MetadataSourceTest.php @@ -4,6 +4,7 @@ namespace Consistence\Sentry\MetadataSource; +use PHPUnit\Framework\Assert; use ReflectionClass; class MetadataSourceTest extends \PHPUnit\Framework\TestCase @@ -13,7 +14,7 @@ public function testGetSentry(): void { $factory = $this->createMock(MetadataSource::class); $factory - ->expects($this->once()) + ->expects(self::once()) ->method('getMetadataForClass'); $factory->getMetadataForClass(new ReflectionClass(FooClass::class)); @@ -24,17 +25,17 @@ public function testCouldNotBeCreated(): void $classReflection = new ReflectionClass(FooClass::class); $factory = $this->createMock(MetadataSource::class); $factory - ->expects($this->once()) + ->expects(self::once()) ->method('getMetadataForClass') - ->will($this->throwException(new \Consistence\Sentry\MetadataSource\ClassMetadataCouldNotBeCreatedException($classReflection, 'test'))); + ->will(self::throwException(new \Consistence\Sentry\MetadataSource\ClassMetadataCouldNotBeCreatedException($classReflection, 'test'))); try { $factory->getMetadataForClass($classReflection); - $this->fail(); + Assert::fail('Exception expected'); } catch (\Consistence\Sentry\MetadataSource\ClassMetadataCouldNotBeCreatedException $e) { - $this->assertSame($classReflection, $e->getClassReflection()); - $this->assertStringContainsString(FooClass::class, $e->getMessage()); - $this->assertStringContainsString('test', $e->getMessage()); + Assert::assertSame($classReflection, $e->getClassReflection()); + Assert::assertStringContainsString(FooClass::class, $e->getMessage()); + Assert::assertStringContainsString('test', $e->getMessage()); } } diff --git a/tests/SentryIdentificatorParser/SentryIdentificatorParserTest.php b/tests/SentryIdentificatorParser/SentryIdentificatorParserTest.php index 59bdb6e..c223b9e 100644 --- a/tests/SentryIdentificatorParser/SentryIdentificatorParserTest.php +++ b/tests/SentryIdentificatorParser/SentryIdentificatorParserTest.php @@ -5,52 +5,174 @@ namespace Consistence\Sentry\SentryIdentificatorParser; use Consistence\Sentry\Metadata\SentryIdentificator; +use Generator; +use PHPUnit\Framework\Assert; class SentryIdentificatorParserTest extends \PHPUnit\Framework\TestCase { /** - * @return mixed[][] + * @return mixed[][]|\Generator */ - public function matchesProvider(): array + public function matchesDataProvider(): Generator { - return [ - [new SentryIdentificator('string'), 'string', false, false, null], - [new SentryIdentificator('string[]'), 'string', true, false, null], - [new SentryIdentificator('string|NULL'), 'string', false, true, null], - [new SentryIdentificator('string|null'), 'string', false, true, null], - [new SentryIdentificator('string[]|NULL'), 'string', true, true, null], - [new SentryIdentificator('string[]|null'), 'string', true, true, null], - [new SentryIdentificator('string[][]'), 'string', true, false, null], - [new SentryIdentificator('string[][]|null'), 'string', true, true, null], - [new SentryIdentificator('Foo'), 'Foo', false, false, null], - [new SentryIdentificator('\Foo'), 'Foo', false, false, null], - [new SentryIdentificator('Foo\Bar'), 'Foo\Bar', false, false, null], - [new SentryIdentificator('\Foo\Bar'), 'Foo\Bar', false, false, null], - [new SentryIdentificator('\Foo\Bar[]'), 'Foo\Bar', true, false, null], - [new SentryIdentificator('\Foo\Bar[]|null'), 'Foo\Bar', true, true, null], - [new SentryIdentificator('\Foo\Bar foobar'), 'Foo\Bar', false, false, null], - [new SentryIdentificator('\Foo\Bar nullable'), 'Foo\Bar', false, false, null], - [new SentryIdentificator('\Collection of \Foo\Bar'), 'Collection', false, false, null], - [new SentryIdentificator('Foo::Bar'), 'Bar', false, false, 'Foo'], - [new SentryIdentificator('\Foo::\Bar'), 'Bar', false, false, 'Foo'], - [new SentryIdentificator('Long\Class\Name\Which\Tests\The\Backtracking\Limit::Bar'), 'Bar', false, false, 'Long\Class\Name\Which\Tests\The\Backtracking\Limit'], + yield 'string' => [ + 'sentryIdentificator' => new SentryIdentificator('string'), + 'expectedType' => 'string', + 'expectedMany' => false, + 'expectedNullable' => false, + 'sourceClass' => null, + ]; + yield 'array of strings' => [ + 'sentryIdentificator' => new SentryIdentificator('string[]'), + 'expectedType' => 'string', + 'expectedMany' => true, + 'expectedNullable' => false, + 'sourceClass' => null, + ]; + yield 'nullable string, uppercase NULL' => [ + 'sentryIdentificator' => new SentryIdentificator('string|NULL'), + 'expectedType' => 'string', + 'expectedMany' => false, + 'expectedNullable' => true, + 'sourceClass' => null, + ]; + yield 'nullable string, lowercase null' => [ + 'sentryIdentificator' => new SentryIdentificator('string|null'), + 'expectedType' => 'string', + 'expectedMany' => false, + 'expectedNullable' => true, + 'sourceClass' => null, + ]; + yield 'nullable array of strings, uppercase NULL' => [ + 'sentryIdentificator' => new SentryIdentificator('string[]|NULL'), + 'expectedType' => 'string', + 'expectedMany' => true, + 'expectedNullable' => true, + 'sourceClass' => null, + ]; + yield 'nullable array of strings, lowercase null' => [ + 'sentryIdentificator' => new SentryIdentificator('string[]|null'), + 'expectedType' => 'string', + 'expectedMany' => true, + 'expectedNullable' => true, + 'sourceClass' => null, + ]; + yield 'two-dimensional array of strings' => [ + 'sentryIdentificator' => new SentryIdentificator('string[][]'), + 'expectedType' => 'string', + 'expectedMany' => true, + 'expectedNullable' => false, + 'sourceClass' => null, + ]; + yield 'nullable two-dimensional array of strings, lowercase null' => [ + 'sentryIdentificator' => new SentryIdentificator('string[][]|null'), + 'expectedType' => 'string', + 'expectedMany' => true, + 'expectedNullable' => true, + 'sourceClass' => null, + ]; + yield 'object of class without leading backslash' => [ + 'sentryIdentificator' => new SentryIdentificator('Foo'), + 'expectedType' => 'Foo', + 'expectedMany' => false, + 'expectedNullable' => false, + 'sourceClass' => null, + ]; + yield 'object of class with leading backslash' => [ + 'sentryIdentificator' => new SentryIdentificator('\Foo'), + 'expectedType' => 'Foo', + 'expectedMany' => false, + 'expectedNullable' => false, + 'sourceClass' => null, + ]; + yield 'object of namespaced class without leading backslash' => [ + 'sentryIdentificator' => new SentryIdentificator('Foo\Bar'), + 'expectedType' => 'Foo\Bar', + 'expectedMany' => false, + 'expectedNullable' => false, + 'sourceClass' => null, + ]; + yield 'object of namespaced class with leading backslash' => [ + 'sentryIdentificator' => new SentryIdentificator('\Foo\Bar'), + 'expectedType' => 'Foo\Bar', + 'expectedMany' => false, + 'expectedNullable' => false, + 'sourceClass' => null, + ]; + yield 'array of objects of namespaced class with leading backslash' => [ + 'sentryIdentificator' => new SentryIdentificator('\Foo\Bar[]'), + 'expectedType' => 'Foo\Bar', + 'expectedMany' => true, + 'expectedNullable' => false, + 'sourceClass' => null, + ]; + yield 'nullable array of objects of namespaced class with leading backslash, lowercase null' => [ + 'sentryIdentificator' => new SentryIdentificator('\Foo\Bar[]|null'), + 'expectedType' => 'Foo\Bar', + 'expectedMany' => true, + 'expectedNullable' => true, + 'sourceClass' => null, + ]; + yield 'object of namespaced class with leading backslash followed by name' => [ + 'sentryIdentificator' => new SentryIdentificator('\Foo\Bar foobar'), + 'expectedType' => 'Foo\Bar', + 'expectedMany' => false, + 'expectedNullable' => false, + 'sourceClass' => null, + ]; + yield 'object of namespaced class with leading backslash followed by nullable keyword' => [ + 'sentryIdentificator' => new SentryIdentificator('\Foo\Bar nullable'), + 'expectedType' => 'Foo\Bar', + 'expectedMany' => false, + 'expectedNullable' => false, + 'sourceClass' => null, + ]; + yield 'object of Collection class with leading backslash followed by elements type' => [ + 'sentryIdentificator' => new SentryIdentificator('\Collection of \Foo\Bar'), + 'expectedType' => 'Collection', + 'expectedMany' => false, + 'expectedNullable' => false, + 'sourceClass' => null, + ]; + yield 'object of class with source class, both without leading backslash' => [ + 'sentryIdentificator' => new SentryIdentificator('Foo::Bar'), + 'expectedType' => 'Bar', + 'expectedMany' => false, + 'expectedNullable' => false, + 'sourceClass' => 'Foo', + ]; + yield 'object of class with source class, both with leading backslash' => [ + 'sentryIdentificator' => new SentryIdentificator('\Foo::\Bar'), + 'expectedType' => 'Bar', + 'expectedMany' => false, + 'expectedNullable' => false, + 'sourceClass' => 'Foo', + ]; + yield 'object of class with namespaced source class, both without leading backslash' => [ + 'sentryIdentificator' => new SentryIdentificator('Long\Class\Name\Which\Tests\The\Backtracking\Limit::Bar'), + 'expectedType' => 'Bar', + 'expectedMany' => false, + 'expectedNullable' => false, + 'sourceClass' => 'Long\Class\Name\Which\Tests\The\Backtracking\Limit', ]; } /** - * @return string[][] + * @return string[][]|\Generator */ - public function doesNotMatchProvider(): array + public function doesNotMatchDataProvider(): Generator { - return [ - [''], - ['Long\Class\Name\Which\Tests\The\Backtracking\Limit::'], + yield 'empty string' => [ + 'pattern' => '', + ]; + yield 'namespaced source class without object' => [ + 'pattern' => 'Long\Class\Name\Which\Tests\The\Backtracking\Limit::', ]; } /** - * @dataProvider matchesProvider + * @dataProvider matchesDataProvider * * @param \Consistence\Sentry\Metadata\SentryIdentificator $sentryIdentificator * @param string $expectedType @@ -68,16 +190,16 @@ public function testMatch( { $parser = new SentryIdentificatorParser(); $result = $parser->parse($sentryIdentificator); - $this->assertInstanceOf(SentryIdentificatorParseResult::class, $result); - $this->assertSame($sentryIdentificator, $result->getSentryIdentificator()); - $this->assertSame($expectedType, $result->getType()); - $this->assertSame($expectedMany, $result->isMany()); - $this->assertSame($expectedNullable, $result->isNullable()); - $this->assertSame($sourceClass, $result->getSourceClass()); + Assert::assertInstanceOf(SentryIdentificatorParseResult::class, $result); + Assert::assertSame($sentryIdentificator, $result->getSentryIdentificator()); + Assert::assertSame($expectedType, $result->getType()); + Assert::assertSame($expectedMany, $result->isMany()); + Assert::assertSame($expectedNullable, $result->isNullable()); + Assert::assertSame($sourceClass, $result->getSourceClass()); } /** - * @dataProvider doesNotMatchProvider + * @dataProvider doesNotMatchDataProvider * * @param string $pattern */ @@ -87,9 +209,9 @@ public function testDoesNotMatch(string $pattern): void $sentryIdentificator = new SentryIdentificator($pattern); try { $parser->parse($sentryIdentificator); - $this->fail(); + Assert::fail('Exception expected'); } catch (\Consistence\Sentry\SentryIdentificatorParser\PatternDoesNotMatchException $e) { - $this->assertSame($sentryIdentificator, $e->getSentryIdentificator()); + Assert::assertSame($sentryIdentificator, $e->getSentryIdentificator()); } } diff --git a/tests/Type/AbstractSentryTest.php b/tests/Type/AbstractSentryTest.php index 78efb62..8d43ae3 100644 --- a/tests/Type/AbstractSentryTest.php +++ b/tests/Type/AbstractSentryTest.php @@ -10,6 +10,8 @@ use Consistence\Sentry\Metadata\SentryIdentificator; use Consistence\Sentry\Metadata\SentryMethod; use Consistence\Sentry\Metadata\Visibility; +use Generator; +use PHPUnit\Framework\Assert; class AbstractSentryTest extends \PHPUnit\Framework\TestCase { @@ -18,18 +20,46 @@ public function testSupportedAccess(): void { $sentry = $this->getMockForAbstractClass(AbstractSentry::class); - $this->assertEquals([ + Assert::assertEquals([ new SentryAccess('get'), new SentryAccess('set'), ], $sentry->getSupportedAccess()); } - public function testDefaultMethodNames(): void + /** + * @return mixed[][]|\Generator + */ + public function getDefaultMethodNameDataProvider(): Generator + { + yield 'get' => [ + 'sentryAccess' => new SentryAccess('get'), + 'propertyName' => 'foo', + 'expectedDefaultMethodName' => 'getFoo', + ]; + + yield 'set' => [ + 'sentryAccess' => new SentryAccess('set'), + 'propertyName' => 'foo', + 'expectedDefaultMethodName' => 'setFoo', + ]; + } + + /** + * @dataProvider getDefaultMethodNameDataProvider + * + * @param \Consistence\Sentry\Metadata\SentryAccess $sentryAccess + * @param string $propertyName + * @param string $expectedDefaultMethodName + */ + public function testGetDefaultMethodName( + SentryAccess $sentryAccess, + string $propertyName, + string $expectedDefaultMethodName + ): void { $sentry = $this->getMockForAbstractClass(AbstractSentry::class); - $this->assertSame('getFoo', $sentry->getDefaultMethodName(new SentryAccess('get'), 'foo')); - $this->assertSame('setFoo', $sentry->getDefaultMethodName(new SentryAccess('set'), 'foo')); + Assert::assertSame($expectedDefaultMethodName, $sentry->getDefaultMethodName($sentryAccess, $propertyName)); } public function testDefaultMethodNameUnsupportedSentryAccess(): void @@ -39,10 +69,10 @@ public function testDefaultMethodNameUnsupportedSentryAccess(): void $sentryAccess = new SentryAccess('xxx'); try { $sentry->getDefaultMethodName($sentryAccess, 'foo'); - $this->fail(); + Assert::fail('Exception expected'); } catch (\Consistence\Sentry\Type\SentryAccessNotSupportedException $e) { - $this->assertSame($sentryAccess, $e->getSentryAccess()); - $this->assertSame('MockAbstractSentryTest', $e->getSentryClassName()); + Assert::assertSame($sentryAccess, $e->getSentryAccess()); + Assert::assertSame('MockAbstractSentryTest', $e->getSentryClassName()); } } @@ -50,33 +80,38 @@ public function testTargetAssociationAccessForAccess(): void { $sentry = $this->getMockForAbstractClass(AbstractSentry::class); - $this->assertEmpty($sentry->getTargetAssociationAccessForAccess( + Assert::assertEmpty($sentry->getTargetAssociationAccessForAccess( new SentryAccess('get'), BidirectionalAssociationType::get(BidirectionalAssociationType::ONE) )); } - public function testGenerateGet(): void + /** + * @return mixed[][]|\Generator + */ + public function generateMethodDataProvider(): Generator { - $sentry = $this->getMockForAbstractClass(AbstractSentry::class); - $getMethod = new SentryMethod( - new SentryAccess('get'), - 'getFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'fooProperty', - FooClass::class, - 'int', - new SentryIdentificator('int'), - false, - [ - $getMethod, - ], - null - ); + yield 'get scalar' => (function (): array { + $sentryMethod = new SentryMethod( + new SentryAccess('get'), + 'getFoo', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ); - $method = ' + return [ + 'propertyMetadata' => new PropertyMetadata( + 'fooProperty', + FooClass::class, + 'int', + new SentryIdentificator('int'), + false, + [ + $sentryMethod, + ], + null + ), + 'sentryMethod' => $sentryMethod, + 'expectedGeneratedMethod' => ' /** * Generated int getter * @@ -85,32 +120,31 @@ public function testGenerateGet(): void public function getFoo() { return $this->fooProperty; - }'; - $this->assertSame($method, $sentry->generateMethod($propertyMetadata, $getMethod)); - } - + }', + ]; + })(); - public function testGenerateObjectGet(): void - { - $sentry = $this->getMockForAbstractClass(AbstractSentry::class); - $getMethod = new SentryMethod( - new SentryAccess('get'), - 'getFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'fooProperty', - FooClass::class, - 'stdClass', - new SentryIdentificator('stdClass'), - false, - [ - $getMethod, - ], - null - ); + yield 'get object' => (function (): array { + $sentryMethod = new SentryMethod( + new SentryAccess('get'), + 'getFoo', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ); - $method = ' + return [ + 'propertyMetadata' => new PropertyMetadata( + 'fooProperty', + FooClass::class, + 'stdClass', + new SentryIdentificator('stdClass'), + false, + [ + $sentryMethod, + ], + null + ), + 'sentryMethod' => $sentryMethod, + 'expectedGeneratedMethod' => ' /** * Generated stdClass getter * @@ -119,31 +153,31 @@ public function testGenerateObjectGet(): void public function getFoo() { return $this->fooProperty; - }'; - $this->assertSame($method, $sentry->generateMethod($propertyMetadata, $getMethod)); - } + }', + ]; + })(); - public function testGenerateSet(): void - { - $sentry = $this->getMockForAbstractClass(AbstractSentry::class); - $setMethod = new SentryMethod( - new SentryAccess('set'), - 'setFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'fooProperty', - FooClass::class, - 'int', - new SentryIdentificator('int'), - false, - [ - $setMethod, - ], - null - ); + yield 'set scalar' => (function (): array { + $sentryMethod = new SentryMethod( + new SentryAccess('set'), + 'setFoo', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ); - $method = ' + return [ + 'propertyMetadata' => new PropertyMetadata( + 'fooProperty', + FooClass::class, + 'int', + new SentryIdentificator('int'), + false, + [ + $sentryMethod, + ], + null + ), + 'sentryMethod' => $sentryMethod, + 'expectedGeneratedMethod' => ' /** * Generated int setter * @@ -152,31 +186,31 @@ public function testGenerateSet(): void public function setFoo($newValue) { $this->fooProperty = $newValue; - }'; - $this->assertSame($method, $sentry->generateMethod($propertyMetadata, $setMethod)); - } + }', + ]; + })(); - public function testGenerateObjectSet(): void - { - $sentry = $this->getMockForAbstractClass(AbstractSentry::class); - $setMethod = new SentryMethod( - new SentryAccess('set'), - 'setFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'fooProperty', - FooClass::class, - 'stdClass', - new SentryIdentificator('stdClass'), - false, - [ - $setMethod, - ], - null - ); + yield 'set object' => (function (): array { + $sentryMethod = new SentryMethod( + new SentryAccess('set'), + 'setFoo', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ); - $method = ' + return [ + 'propertyMetadata' => new PropertyMetadata( + 'fooProperty', + FooClass::class, + 'stdClass', + new SentryIdentificator('stdClass'), + false, + [ + $sentryMethod, + ], + null + ), + 'sentryMethod' => $sentryMethod, + 'expectedGeneratedMethod' => ' /** * Generated stdClass setter * @@ -185,8 +219,27 @@ public function testGenerateObjectSet(): void public function setFoo($newValue) { $this->fooProperty = $newValue; - }'; - $this->assertSame($method, $sentry->generateMethod($propertyMetadata, $setMethod)); + }', + ]; + })(); + } + + /** + * @dataProvider generateMethodDataProvider + * + * @param \Consistence\Sentry\Metadata\PropertyMetadata $propertyMetadata + * @param \Consistence\Sentry\Metadata\SentryMethod $sentryMethod + * @param string $expectedGeneratedMethod + */ + public function testGenerateMethod( + PropertyMetadata $propertyMetadata, + SentryMethod $sentryMethod, + string $expectedGeneratedMethod + ): void + { + $sentry = $this->getMockForAbstractClass(AbstractSentry::class); + + Assert::assertSame($expectedGeneratedMethod, $sentry->generateMethod($propertyMetadata, $sentryMethod)); } public function testGenerateUnsupportedSentryAccess(): void @@ -213,11 +266,11 @@ public function testGenerateUnsupportedSentryAccess(): void $args = []; try { $sentry->generateMethod($propertyMetadata, $xxxMethod, $args); - $this->fail(); + Assert::fail('Exception expected'); } catch (\Consistence\Sentry\Type\SentryAccessNotSupportedForPropertyException $e) { - $this->assertSame($propertyMetadata, $e->getProperty()); - $this->assertSame($xxxSentryAccess, $e->getSentryAccess()); - $this->assertSame(get_class($sentry), $e->getSentryClassName()); + Assert::assertSame($propertyMetadata, $e->getProperty()); + Assert::assertSame($xxxSentryAccess, $e->getSentryAccess()); + Assert::assertSame(get_class($sentry), $e->getSentryClassName()); } } diff --git a/tests/Type/CollectionTest.php b/tests/Type/CollectionTest.php index d88886f..144d8c7 100644 --- a/tests/Type/CollectionTest.php +++ b/tests/Type/CollectionTest.php @@ -9,6 +9,8 @@ use Consistence\Sentry\Metadata\SentryIdentificator; use Consistence\Sentry\Metadata\SentryMethod; use Consistence\Sentry\Metadata\Visibility; +use Generator; +use PHPUnit\Framework\Assert; class CollectionTest extends \PHPUnit\Framework\TestCase { @@ -17,7 +19,7 @@ public function testSupportedAccess(): void { $collection = new CollectionType(); - $this->assertEquals([ + Assert::assertEquals([ new SentryAccess('get'), new SentryAccess('set'), new SentryAccess('add'), @@ -26,38 +28,86 @@ public function testSupportedAccess(): void ], $collection->getSupportedAccess()); } - public function testDefaultMethodNames(): void + /** + * @return mixed[][]|\Generator + */ + public function getDefaultMethodNameDataProvider(): Generator { - $collection = new CollectionType(); + yield 'get' => [ + 'sentryAccess' => new SentryAccess('get'), + 'propertyName' => 'children', + 'expectedDefaultMethodName' => 'getChildren', + ]; + + yield 'set' => [ + 'sentryAccess' => new SentryAccess('set'), + 'propertyName' => 'children', + 'expectedDefaultMethodName' => 'setChildren', + ]; + + yield 'add' => [ + 'sentryAccess' => new SentryAccess('add'), + 'propertyName' => 'children', + 'expectedDefaultMethodName' => 'addChild', + ]; + + yield 'remove' => [ + 'sentryAccess' => new SentryAccess('remove'), + 'propertyName' => 'children', + 'expectedDefaultMethodName' => 'removeChild', + ]; - $this->assertSame('getChildren', $collection->getDefaultMethodName(new SentryAccess('get'), 'children')); - $this->assertSame('setChildren', $collection->getDefaultMethodName(new SentryAccess('set'), 'children')); - $this->assertSame('addChild', $collection->getDefaultMethodName(new SentryAccess('add'), 'children')); - $this->assertSame('removeChild', $collection->getDefaultMethodName(new SentryAccess('remove'), 'children')); - $this->assertSame('containsChild', $collection->getDefaultMethodName(new SentryAccess('contains'), 'children')); + yield 'contains' => [ + 'sentryAccess' => new SentryAccess('contains'), + 'propertyName' => 'children', + 'expectedDefaultMethodName' => 'containsChild', + ]; } - public function testGenerateGet(): void + /** + * @dataProvider getDefaultMethodNameDataProvider + * + * @param \Consistence\Sentry\Metadata\SentryAccess $sentryAccess + * @param string $propertyName + * @param string $expectedDefaultMethodName + */ + public function testGetDefaultMethodName( + SentryAccess $sentryAccess, + string $propertyName, + string $expectedDefaultMethodName + ): void { $collection = new CollectionType(); - $getMethod = new SentryMethod( - new SentryAccess('get'), - 'getFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'children', - FooClass::class, - 'int', - new SentryIdentificator('int[]'), - false, - [ - $getMethod, - ], - null - ); - - $method = ' + + Assert::assertSame($expectedDefaultMethodName, $collection->getDefaultMethodName($sentryAccess, $propertyName)); + } + + /** + * @return mixed[][]|\Generator + */ + public function generateMethodDataProvider(): Generator + { + yield 'get' => (function (): array { + $sentryMethod = new SentryMethod( + new SentryAccess('get'), + 'getFoo', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ); + + return [ + 'propertyMetadata' => new PropertyMetadata( + 'children', + FooClass::class, + 'int', + new SentryIdentificator('int[]'), + false, + [ + $sentryMethod, + ], + null + ), + 'sentryMethod' => $sentryMethod, + 'expectedGeneratedMethod' => ' /** * Generated int collection getter * @@ -66,31 +116,31 @@ public function testGenerateGet(): void public function getFoo() { return $this->children; - }'; - $this->assertSame($method, $collection->generateMethod($propertyMetadata, $getMethod)); - } + }', + ]; + })(); - public function testGenerateSet(): void - { - $collection = new CollectionType(); - $setMethod = new SentryMethod( - new SentryAccess('set'), - 'setFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'children', - FooClass::class, - 'int', - new SentryIdentificator('int[]'), - false, - [ - $setMethod, - ], - null - ); - - $method = ' + yield 'set' => (function (): array { + $sentryMethod = new SentryMethod( + new SentryAccess('set'), + 'setFoo', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ); + + return [ + 'propertyMetadata' => new PropertyMetadata( + 'children', + FooClass::class, + 'int', + new SentryIdentificator('int[]'), + false, + [ + $sentryMethod, + ], + null + ), + 'sentryMethod' => $sentryMethod, + 'expectedGeneratedMethod' => ' /** * Generated int collection setter * @@ -107,31 +157,31 @@ public function setFoo($newValues) $collection[] = $el; } } - }'; - $this->assertSame($method, $collection->generateMethod($propertyMetadata, $setMethod)); - } + }', + ]; + })(); - public function testGenerateContains(): void - { - $collection = new CollectionType(); - $containsMethod = new SentryMethod( - new SentryAccess('contains'), - 'containsFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'children', - FooClass::class, - 'int', - new SentryIdentificator('int[]'), - false, - [ - $containsMethod, - ], - null - ); - - $method = ' + yield 'contains' => (function (): array { + $sentryMethod = new SentryMethod( + new SentryAccess('contains'), + 'containsFoo', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ); + + return [ + 'propertyMetadata' => new PropertyMetadata( + 'children', + FooClass::class, + 'int', + new SentryIdentificator('int[]'), + false, + [ + $sentryMethod, + ], + null + ), + 'sentryMethod' => $sentryMethod, + 'expectedGeneratedMethod' => ' /** * Generated int collection contains * @@ -142,31 +192,31 @@ public function containsFoo($value) { \Consistence\Type\Type::checkType($value, \'int\'); return \Consistence\Type\ArrayType\ArrayType::containsValue($this->children, $value); - }'; - $this->assertSame($method, $collection->generateMethod($propertyMetadata, $containsMethod)); - } + }', + ]; + })(); - public function testGenerateAdd(): void - { - $collection = new CollectionType(); - $addMethod = new SentryMethod( - new SentryAccess('add'), - 'addFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'children', - FooClass::class, - 'int', - new SentryIdentificator('int[]'), - false, - [ - $addMethod, - ], - null - ); - - $method = ' + yield 'add' => (function (): array { + $sentryMethod = new SentryMethod( + new SentryAccess('add'), + 'addFoo', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ); + + return [ + 'propertyMetadata' => new PropertyMetadata( + 'children', + FooClass::class, + 'int', + new SentryIdentificator('int[]'), + false, + [ + $sentryMethod, + ], + null + ), + 'sentryMethod' => $sentryMethod, + 'expectedGeneratedMethod' => ' /** * Generated int collection add * @@ -184,31 +234,31 @@ public function addFoo($newValue) } return false; - }'; - $this->assertSame($method, $collection->generateMethod($propertyMetadata, $addMethod)); - } + }', + ]; + })(); - public function testGenerateRemove(): void - { - $collection = new CollectionType(); - $removeMethod = new SentryMethod( - new SentryAccess('remove'), - 'removeFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'children', - FooClass::class, - 'int', - new SentryIdentificator('int[]'), - false, - [ - $removeMethod, - ], - null - ); - - $method = ' + yield 'remove' => (function (): array { + $sentryMethod = new SentryMethod( + new SentryAccess('remove'), + 'removeFoo', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ); + + return [ + 'propertyMetadata' => new PropertyMetadata( + 'children', + FooClass::class, + 'int', + new SentryIdentificator('int[]'), + false, + [ + $sentryMethod, + ], + null + ), + 'sentryMethod' => $sentryMethod, + 'expectedGeneratedMethod' => ' /** * Generated int collection remove * @@ -219,8 +269,27 @@ public function removeFoo($value) { \Consistence\Type\Type::checkType($value, \'int\'); return \Consistence\Type\ArrayType\ArrayType::removeValue($this->children, $value); - }'; - $this->assertSame($method, $collection->generateMethod($propertyMetadata, $removeMethod)); + }', + ]; + })(); + } + + /** + * @dataProvider generateMethodDataProvider + * + * @param \Consistence\Sentry\Metadata\PropertyMetadata $propertyMetadata + * @param \Consistence\Sentry\Metadata\SentryMethod $sentryMethod + * @param string $expectedGeneratedMethod + */ + public function testGenerateMethod( + PropertyMetadata $propertyMetadata, + SentryMethod $sentryMethod, + string $expectedGeneratedMethod + ): void + { + $collection = new CollectionType(); + + Assert::assertSame($expectedGeneratedMethod, $collection->generateMethod($propertyMetadata, $sentryMethod)); } } diff --git a/tests/Type/SimpleTypeTest.php b/tests/Type/SimpleTypeTest.php index e7185e6..3181114 100644 --- a/tests/Type/SimpleTypeTest.php +++ b/tests/Type/SimpleTypeTest.php @@ -9,31 +9,38 @@ use Consistence\Sentry\Metadata\SentryIdentificator; use Consistence\Sentry\Metadata\SentryMethod; use Consistence\Sentry\Metadata\Visibility; +use Generator; +use PHPUnit\Framework\Assert; class SimpleTypeTest extends \PHPUnit\Framework\TestCase { - public function testGenerateGet(): void + /** + * @return mixed[][]|\Generator + */ + public function generateMethodDataProvider(): Generator { - $integerSentry = new SimpleType(); - $getMethod = new SentryMethod( - new SentryAccess('get'), - 'getFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'fooProperty', - FooClass::class, - 'int', - new SentryIdentificator('int'), - false, - [ - $getMethod, - ], - null - ); + yield 'get' => (function (): array { + $sentryMethod = new SentryMethod( + new SentryAccess('get'), + 'getFoo', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ); - $method = ' + return [ + 'propertyMetadata' => new PropertyMetadata( + 'fooProperty', + FooClass::class, + 'int', + new SentryIdentificator('int'), + false, + [ + $sentryMethod, + ], + null + ), + 'sentryMethod' => $sentryMethod, + 'expectedGeneratedMethod' => ' /** * Generated int getter * @@ -42,31 +49,31 @@ public function testGenerateGet(): void public function getFoo() { return $this->fooProperty; - }'; - $this->assertSame($method, $integerSentry->generateMethod($propertyMetadata, $getMethod)); - } + }', + ]; + })(); - public function testGenerateNullableGet(): void - { - $integerSentry = new SimpleType(); - $getMethod = new SentryMethod( - new SentryAccess('get'), - 'getFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'fooProperty', - FooClass::class, - 'int', - new SentryIdentificator('int|null'), - true, - [ - $getMethod, - ], - null - ); + yield 'get nullable' => (function (): array { + $sentryMethod = new SentryMethod( + new SentryAccess('get'), + 'getFoo', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ); - $method = ' + return [ + 'propertyMetadata' => new PropertyMetadata( + 'fooProperty', + FooClass::class, + 'int', + new SentryIdentificator('int|null'), + true, + [ + $sentryMethod, + ], + null + ), + 'sentryMethod' => $sentryMethod, + 'expectedGeneratedMethod' => ' /** * Generated int getter * @@ -75,31 +82,31 @@ public function testGenerateNullableGet(): void public function getFoo() { return $this->fooProperty; - }'; - $this->assertSame($method, $integerSentry->generateMethod($propertyMetadata, $getMethod)); - } + }', + ]; + })(); - public function testGenerateSet(): void - { - $integerSentry = new SimpleType(); - $setMethod = new SentryMethod( - new SentryAccess('set'), - 'setFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'fooProperty', - FooClass::class, - 'int', - new SentryIdentificator('int'), - false, - [ - $setMethod, - ], - null - ); + yield 'set' => (function (): array { + $sentryMethod = new SentryMethod( + new SentryAccess('set'), + 'setFoo', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ); - $method = ' + return [ + 'propertyMetadata' => new PropertyMetadata( + 'fooProperty', + FooClass::class, + 'int', + new SentryIdentificator('int'), + false, + [ + $sentryMethod, + ], + null + ), + 'sentryMethod' => $sentryMethod, + 'expectedGeneratedMethod' => ' /** * Generated int setter * @@ -109,31 +116,31 @@ public function setFoo($newValue) { \Consistence\Type\Type::checkType($newValue, \'int\'); $this->fooProperty = $newValue; - }'; - $this->assertSame($method, $integerSentry->generateMethod($propertyMetadata, $setMethod)); - } + }', + ]; + })(); - public function testGenerateSetNullable(): void - { - $integerSentry = new SimpleType(); - $setMethod = new SentryMethod( - new SentryAccess('set'), - 'setFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'fooProperty', - FooClass::class, - 'int', - new SentryIdentificator('int|null'), - true, - [ - $setMethod, - ], - null - ); + yield 'set nullable' => (function (): array { + $sentryMethod = new SentryMethod( + new SentryAccess('set'), + 'setFoo', + Visibility::get(Visibility::VISIBILITY_PUBLIC) + ); - $method = ' + return [ + 'propertyMetadata' => new PropertyMetadata( + 'fooProperty', + FooClass::class, + 'int', + new SentryIdentificator('int|null'), + true, + [ + $sentryMethod, + ], + null + ), + 'sentryMethod' => $sentryMethod, + 'expectedGeneratedMethod' => ' /** * Generated int setter * @@ -143,8 +150,27 @@ public function setFoo($newValue) { \Consistence\Type\Type::checkType($newValue, \'int|null\'); $this->fooProperty = $newValue; - }'; - $this->assertSame($method, $integerSentry->generateMethod($propertyMetadata, $setMethod)); + }', + ]; + })(); + } + + /** + * @dataProvider generateMethodDataProvider + * + * @param \Consistence\Sentry\Metadata\PropertyMetadata $propertyMetadata + * @param \Consistence\Sentry\Metadata\SentryMethod $sentryMethod + * @param string $expectedGeneratedMethod + */ + public function testGenerateMethod( + PropertyMetadata $propertyMetadata, + SentryMethod $sentryMethod, + string $expectedGeneratedMethod + ): void + { + $integerSentry = new SimpleType(); + + Assert::assertSame($expectedGeneratedMethod, $integerSentry->generateMethod($propertyMetadata, $sentryMethod)); } }