diff --git a/src/Runtime/RuntimeHelper.php b/src/Runtime/RuntimeHelper.php deleted file mode 100644 index f876d07..0000000 --- a/src/Runtime/RuntimeHelper.php +++ /dev/null @@ -1,82 +0,0 @@ -metadataSource = $metadataSource; - $this->sentryFactory = $sentryFactory; - } - - /** - * @param \Consistence\Sentry\SentryAware $object - * @param string $methodName - * @param mixed[] $args - * @param \Closure|null $nothingDoneCallback - * @return mixed - */ - public function run(SentryAware $object, string $methodName, array $args, Closure $nothingDoneCallback = null) - { - $searchResult = $this->findMethod(new ReflectionClass($object), $methodName); - if ($searchResult === null && $nothingDoneCallback !== null) { - return $nothingDoneCallback($object, $methodName, $args); - } - $sentry = $this->sentryFactory->getSentry($searchResult->getProperty()->getSentryIdentificator()); - - return $sentry->processMethod($searchResult->getProperty(), $object, $searchResult->getSentryMethod(), $args); - } - - /** - * @param \ReflectionClass $classReflection - * @param string $methodName - * @return \Consistence\Sentry\Metadata\SentryMethodSearchResult|null - */ - private function findMethod(ReflectionClass $classReflection, string $methodName) - { - try { - $classMetadata = $this->metadataSource->getMetadataForClass($classReflection); - // in runtime, the context from which the method is called is not available, searches for all methods - return $classMetadata->getSentryMethodByNameAndRequiredVisibility( - $methodName, - Visibility::get(Visibility::VISIBILITY_PRIVATE) - ); - } catch (\Consistence\Sentry\Metadata\MethodNotFoundException $e) { - return $this->findMethodInParentClass($classReflection, $methodName); - } - } - - /** - * - * @param \ReflectionClass $classReflection - * @param string $methodName - * @return \Consistence\Sentry\Metadata\SentryMethodSearchResult|null - */ - private function findMethodInParentClass(ReflectionClass $classReflection, string $methodName) - { - $parent = $classReflection->getParentClass(); - if ($parent !== false && $parent->implementsInterface(SentryAware::class) === true) { - return $this->findMethod($parent, $methodName); - } - - return null; - } - -} diff --git a/src/Runtime/RuntimeHelperBridge.php b/src/Runtime/RuntimeHelperBridge.php deleted file mode 100644 index a9cce80..0000000 --- a/src/Runtime/RuntimeHelperBridge.php +++ /dev/null @@ -1,41 +0,0 @@ -run($this, $method, $args, function (SentryAware $object, $methodName) { - ObjectMixin::magicCall($object, $methodName); - }); - } - } diff --git a/src/Type/AbstractSentry.php b/src/Type/AbstractSentry.php index ab348cb..af8caba 100644 --- a/src/Type/AbstractSentry.php +++ b/src/Type/AbstractSentry.php @@ -8,7 +8,6 @@ use Consistence\Sentry\Metadata\PropertyMetadata; use Consistence\Sentry\Metadata\SentryAccess; use Consistence\Sentry\Metadata\SentryMethod; -use Consistence\Sentry\SentryAware; use Consistence\Type\ArrayType\ArrayType; /** @@ -31,22 +30,6 @@ public function getSupportedAccess() ]; } - /** - * Redirects process requests to methods named {sentryAccess}(...) - * - * @param \Consistence\Sentry\Metadata\PropertyMetadata $propertyMetadata - * @param \Consistence\Sentry\SentryAware $object - * @param \Consistence\Sentry\Metadata\SentryMethod $sentryMethod - * @param mixed[] $args - * @return mixed - */ - public function processMethod(PropertyMetadata $propertyMetadata, SentryAware $object, SentryMethod $sentryMethod, array $args) - { - $this->checkSupportedSentryAccess($propertyMetadata, $sentryMethod->getSentryAccess()); - - return $this->{lcfirst($sentryMethod->getSentryAccess()->getName())}($propertyMetadata, $object, $args); - } - /** * Redirects generate requests to methods named generate{SentryAccess}(...) * @@ -101,27 +84,6 @@ public function getTargetAssociationAccessForAccess(SentryAccess $sentryAccess, return []; } - /** - * @param \Consistence\Sentry\Metadata\PropertyMetadata $property - * @param \Consistence\Sentry\SentryAware $object - * @param mixed[] $args - * @return mixed - */ - public function get(PropertyMetadata $property, SentryAware $object, array $args) - { - return TypeHelper::getPropertyValue($property, $object); - } - - /** - * @param \Consistence\Sentry\Metadata\PropertyMetadata $property - * @param \Consistence\Sentry\SentryAware $object - * @param mixed[] $args - */ - public function set(PropertyMetadata $property, SentryAware $object, array $args) - { - TypeHelper::setPropertyValue($property, $object, TypeHelper::getFirstArg($args)); - } - public function generateGet(PropertyMetadata $property, SentryMethod $sentryMethod): string { return TypeHelper::generateGet($property, $sentryMethod); diff --git a/src/Type/CollectionType.php b/src/Type/CollectionType.php index 942734f..735e518 100644 --- a/src/Type/CollectionType.php +++ b/src/Type/CollectionType.php @@ -7,7 +7,6 @@ use Consistence\Sentry\Metadata\PropertyMetadata; use Consistence\Sentry\Metadata\SentryAccess; use Consistence\Sentry\Metadata\SentryMethod; -use Consistence\Sentry\SentryAware; use Consistence\Type\ArrayType\ArrayType; use Consistence\Type\Type; use Doctrine\Common\Inflector\Inflector; @@ -47,101 +46,6 @@ public function getDefaultMethodName(SentryAccess $sentryAccess, string $propert } } - /** - * @param \Consistence\Sentry\Metadata\PropertyMetadata $property - * @param \Consistence\Sentry\SentryAware $object - * @param mixed[] $args - */ - public function set(PropertyMetadata $property, SentryAware $object, array $args) - { - $newValues = TypeHelper::getFirstArg($args); - Type::checkType($newValues, 'array'); - - $collection = []; - foreach ($newValues as $item) { - $this->addValue($collection, $property, $item); - } - TypeHelper::setPropertyValue($property, $object, $collection); - } - - /** - * @param \Consistence\Sentry\Metadata\PropertyMetadata $property - * @param \Consistence\Sentry\SentryAware $object - * @param mixed[] $args - * @return bool was element really added? - */ - public function add(PropertyMetadata $property, SentryAware $object, array $args): bool - { - $value = TypeHelper::getFirstArg($args); - - $collection = TypeHelper::getPropertyValue($property, $object); - $changed = $this->addValue($collection, $property, $value); - TypeHelper::setPropertyValue($property, $object, $collection); - - return $changed; - } - - /** - * @param \Consistence\Sentry\Metadata\PropertyMetadata $property - * @param \Consistence\Sentry\SentryAware $object - * @param mixed[] $args - * @return bool was element really removed? - */ - public function remove(PropertyMetadata $property, SentryAware $object, array $args): bool - { - $value = TypeHelper::getFirstArg($args); - - $collection = TypeHelper::getPropertyValue($property, $object); - $changed = $this->removeValue($collection, $property, $value); - TypeHelper::setPropertyValue($property, $object, $collection); - - return $changed; - } - - /** - * @param \Consistence\Sentry\Metadata\PropertyMetadata $property - * @param \Consistence\Sentry\SentryAware $object - * @param mixed[] $args - * @return bool - */ - public function contains(PropertyMetadata $property, SentryAware $object, array $args): bool - { - $value = TypeHelper::getFirstArg($args); - Type::checkType($value, $property->getType()); - $collection = TypeHelper::getPropertyValue($property, $object); - - return ArrayType::containsValue($collection, $value); - } - - /** - * @param mixed[] $collection - * @param \Consistence\Sentry\Metadata\PropertyMetadata $property - * @param mixed $value - * @return bool was element really added? - */ - private function addValue(array &$collection, PropertyMetadata $property, $value): bool - { - Type::checkType($value, $property->getType()); - if (ArrayType::containsValue($collection, $value)) { - return false; - } - $collection[] = $value; - - return true; - } - - /** - * @param mixed[] $collection - * @param \Consistence\Sentry\Metadata\PropertyMetadata $property - * @param mixed $value - * @return bool was element really removed? - */ - private function removeValue(array &$collection, PropertyMetadata $property, $value): bool - { - Type::checkType($value, $property->getType()); - return ArrayType::removeValue($collection, $value); - } - public function generateGet(PropertyMetadata $property, SentryMethod $sentryMethod): string { return ' diff --git a/src/Type/Sentry.php b/src/Type/Sentry.php index 9192b82..8a788e6 100644 --- a/src/Type/Sentry.php +++ b/src/Type/Sentry.php @@ -8,7 +8,6 @@ use Consistence\Sentry\Metadata\PropertyMetadata; use Consistence\Sentry\Metadata\SentryAccess; use Consistence\Sentry\Metadata\SentryMethod; -use Consistence\Sentry\SentryAware; interface Sentry { @@ -20,17 +19,6 @@ interface Sentry */ public function getSupportedAccess(); - /** - * Logic which should be executed while processing SentryMethods in runtime - * - * @param \Consistence\Sentry\Metadata\PropertyMetadata $property - * @param \Consistence\Sentry\SentryAware $object - * @param \Consistence\Sentry\Metadata\SentryMethod $sentryMethod - * @param mixed[] $args arguments given to called method - * @return mixed - */ - public function processMethod(PropertyMetadata $property, SentryAware $object, SentryMethod $sentryMethod, array $args); - /** * Generate code for a SentryMethod * diff --git a/src/Type/SimpleType.php b/src/Type/SimpleType.php index ca091d4..6196800 100644 --- a/src/Type/SimpleType.php +++ b/src/Type/SimpleType.php @@ -6,24 +6,11 @@ use Consistence\Sentry\Metadata\PropertyMetadata; use Consistence\Sentry\Metadata\SentryMethod; -use Consistence\Sentry\SentryAware; use Consistence\Type\Type; class SimpleType extends \Consistence\Sentry\Type\AbstractSentry { - /** - * @param \Consistence\Sentry\Metadata\PropertyMetadata $property - * @param \Consistence\Sentry\SentryAware $object - * @param mixed[] $args - */ - public function set(PropertyMetadata $property, SentryAware $object, array $args) - { - $value = TypeHelper::getFirstArg($args); - Type::checkType($value, TypeHelper::getRequiredTypeString($property)); - TypeHelper::setPropertyValue($property, $object, $value); - } - public function generateSet(PropertyMetadata $property, SentryMethod $sentryMethod): string { $method = ' diff --git a/src/Type/TypeHelper.php b/src/Type/TypeHelper.php index 530a7f4..4e075b4 100644 --- a/src/Type/TypeHelper.php +++ b/src/Type/TypeHelper.php @@ -6,8 +6,6 @@ use Consistence\Sentry\Metadata\PropertyMetadata; use Consistence\Sentry\Metadata\SentryMethod; -use Consistence\Sentry\SentryAware; -use ReflectionProperty; class TypeHelper extends \Consistence\ObjectPrototype { @@ -22,19 +20,6 @@ public static function getRequiredTypeString(PropertyMetadata $propertyMetadata) return $propertyMetadata->getType() . ($propertyMetadata->isNullable() ? '|null' : ''); } - /** - * @param mixed[] $args - * @return mixed first argument - */ - public static function getFirstArg(array $args) - { - if (!array_key_exists(0, $args)) { - throw new \Consistence\Sentry\Type\MissingArgumentException($args, 1); - } - - return $args[0]; - } - public static function formatTypeToString(PropertyMetadata $propertyMetadata): string { return (static::isObjectType($propertyMetadata->getType()) ? '\\' : '') @@ -59,31 +44,6 @@ public static function isObjectType(string $type): bool } } - /** - * @param \Consistence\Sentry\Metadata\PropertyMetadata $propertyMetadata - * @param \Consistence\Sentry\SentryAware $object - * @return mixed - */ - public static function getPropertyValue(PropertyMetadata $propertyMetadata, SentryAware $object) - { - $propertyReflection = new ReflectionProperty($propertyMetadata->getClassName(), $propertyMetadata->getName()); - $propertyReflection->setAccessible(true); - - return $propertyReflection->getValue($object); - } - - /** - * @param \Consistence\Sentry\Metadata\PropertyMetadata $propertyMetadata - * @param \Consistence\Sentry\SentryAware $object - * @param mixed $value - */ - public static function setPropertyValue(PropertyMetadata $propertyMetadata, SentryAware $object, $value) - { - $propertyReflection = new ReflectionProperty($propertyMetadata->getClassName(), $propertyMetadata->getName()); - $propertyReflection->setAccessible(true); - $propertyReflection->setValue($object, $value); - } - public static function generateGet(PropertyMetadata $property, SentryMethod $sentryMethod): string { return ' diff --git a/src/Type/exceptions/MissingArgumentException.php b/src/Type/exceptions/MissingArgumentException.php deleted file mode 100644 index 308c075..0000000 --- a/src/Type/exceptions/MissingArgumentException.php +++ /dev/null @@ -1,44 +0,0 @@ -args = $args; - $this->requiredCountOfArguments = $requiredCountOfArguments; - } - - /** - * @return mixed[] - */ - public function getArgs(): array - { - return $this->args; - } - - public function getRequiredCountOfArguments(): int - { - return $this->requiredCountOfArguments; - } - -} diff --git a/tests/Runtime/RuntimeHelperTest.php b/tests/Runtime/RuntimeHelperTest.php deleted file mode 100644 index f6b8bc4..0000000 --- a/tests/Runtime/RuntimeHelperTest.php +++ /dev/null @@ -1,148 +0,0 @@ -createMock(SentryIdentificator::class); - - $property = $this->createMock(PropertyMetadata::class); - $property - ->expects($this->once()) - ->method('getSentryIdentificator') - ->will($this->returnValue($sentryIdentificator)); - - $sentryMethod = $this->createMock(SentryMethod::class); - - $searchResult = new SentryMethodSearchResult($sentryMethod, $property); - - $classMetadata = $this->createMock(ClassMetadata::class); - $classMetadata - ->expects($this->once()) - ->method('getSentryMethodByNameAndRequiredVisibility') - ->with($fooMethod, $this->identicalTo(Visibility::get(Visibility::VISIBILITY_PRIVATE))) - ->will($this->returnValue($searchResult)); - - $metadataSource = $this->createMock(MetadataSource::class); - $metadataSource - ->expects($this->once()) - ->method('getMetadataForClass') - ->with(new ReflectionClass($foo)) - ->will($this->returnValue($classMetadata)); - - $sentry = $this->createMock(Sentry::class); - $sentry - ->expects($this->once()) - ->method('processMethod') - ->with( - $this->identicalTo($property), - $this->identicalTo($foo), - $this->identicalTo($sentryMethod), - [$fooParam] - ); - - $sentryFactory = $this->createMock(SentryFactory::class); - $sentryFactory - ->expects($this->once()) - ->method('getSentry') - ->with($this->identicalTo($sentryIdentificator)) - ->will($this->returnValue($sentry)); - - $runtimeHelper = new RuntimeHelper($metadataSource, $sentryFactory); - - $runtimeHelper->run($foo, $fooMethod, [$fooParam]); - } - - public function testMethodNotFoundCallbackParentDoesNotImplementSentryAware() - { - $foo = new FooClass(); - $fooMethod = 'fooMethod'; - $fooParam = 'test'; - - $classMetadata = $this->createMock(ClassMetadata::class); - $classMetadata - ->expects($this->once()) - ->method('getSentryMethodByNameAndRequiredVisibility') - ->with($fooMethod, $this->identicalTo(Visibility::get(Visibility::VISIBILITY_PRIVATE))) - ->will($this->throwException( - new \Consistence\Sentry\Metadata\MethodNotFoundException($fooMethod, get_class($foo)) - )); - - $metadataSource = $this->createMock(MetadataSource::class); - $metadataSource - ->expects($this->once()) - ->method('getMetadataForClass') - ->with(new ReflectionClass($foo)) - ->will($this->returnValue($classMetadata)); - - $sentryFactory = $this->createMock(SentryFactory::class); - $sentryFactory - ->expects($this->never()) - ->method('getSentry'); - - $runtimeHelper = new RuntimeHelper($metadataSource, $sentryFactory); - - $this->assertSame('test', $runtimeHelper->run($foo, $fooMethod, [$fooParam], function (): string { - return 'test'; - })); - } - - public function testMethodNotFoundCallbackParentImplementsSentryAware() - { - $foo = new BarClass(); - $fooMethod = 'fooMethod'; - $fooParam = 'test'; - - $metadataReturnCallback = function () use ($fooMethod, $foo): ClassMetadata { - $classMetadata = $this->createMock(ClassMetadata::class); - $classMetadata - ->expects($this->once()) - ->method('getSentryMethodByNameAndRequiredVisibility') - ->with($fooMethod, $this->identicalTo(Visibility::get(Visibility::VISIBILITY_PRIVATE))) - ->will($this->throwException( - new \Consistence\Sentry\Metadata\MethodNotFoundException($fooMethod, get_class($foo)) - )); - - return $classMetadata; - }; - - $metadataSource = $this->createMock(MetadataSource::class); - $metadataSource - ->expects($this->exactly(2)) - ->method('getMetadataForClass') - ->will($this->returnCallback($metadataReturnCallback)); - - $sentryFactory = $this->createMock(SentryFactory::class); - $sentryFactory - ->expects($this->never()) - ->method('getSentry'); - - $runtimeHelper = new RuntimeHelper($metadataSource, $sentryFactory); - - $this->assertSame('test', $runtimeHelper->run($foo, $fooMethod, [$fooParam], function (): string { - return 'test'; - })); - } - -} diff --git a/tests/Runtime/data/BarClass.php b/tests/Runtime/data/BarClass.php deleted file mode 100644 index 71db210..0000000 --- a/tests/Runtime/data/BarClass.php +++ /dev/null @@ -1,12 +0,0 @@ -getMockForAbstractClass(AbstractSentry::class); - $getMethod = new SentryMethod( - new SentryAccess('get'), - 'getFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $setMethod = new SentryMethod( - new SentryAccess('set'), - 'setFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'fooProperty', - FooClass::class, - 'int', - new SentryIdentificator('int'), - false, - [ - $getMethod, - $setMethod, - ], - null - ); - $foo = new FooClass(); - - $this->assertNull($sentry->processMethod($propertyMetadata, $foo, $getMethod, [])); - $this->assertNull($sentry->processMethod($propertyMetadata, $foo, $setMethod, [123])); - $this->assertSame(123, $sentry->processMethod($propertyMetadata, $foo, $getMethod, [])); - } - - public function testProccessSetMissingArgument() - { - $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 - ); - $foo = new FooClass(); - - $args = []; - try { - $sentry->processMethod($propertyMetadata, $foo, $setMethod, $args); - $this->fail(); - } catch (\Consistence\Sentry\Type\MissingArgumentException $e) { - $this->assertSame($args, $e->getArgs()); - $this->assertSame(1, $e->getRequiredCountOfArguments()); - } - } - - public function testProccessUnsupportedSentryAccess() - { - $sentry = $this->getMockForAbstractClass(AbstractSentry::class); - $xxxSentryAccess = new SentryAccess('xxx'); - $xxxMethod = new SentryMethod( - $xxxSentryAccess, - 'xxxMethod', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'fooProperty', - FooClass::class, - 'int', - new SentryIdentificator('int'), - false, - [ - $xxxMethod, - ], - null - ); - $foo = new FooClass(); - - $args = []; - try { - $sentry->processMethod($propertyMetadata, $foo, $xxxMethod, $args); - $this->fail(); - } catch (\Consistence\Sentry\Type\SentryAccessNotSupportedForPropertyException $e) { - $this->assertSame($propertyMetadata, $e->getProperty()); - $this->assertSame($xxxSentryAccess, $e->getSentryAccess()); - $this->assertSame(get_class($sentry), $e->getSentryClassName()); - } - } - public function testGenerateGet() { $sentry = $this->getMockForAbstractClass(AbstractSentry::class); diff --git a/tests/Type/CollectionTest.php b/tests/Type/CollectionTest.php index 78ab012..6ceb3fd 100644 --- a/tests/Type/CollectionTest.php +++ b/tests/Type/CollectionTest.php @@ -37,292 +37,6 @@ public function testDefaultMethodNames() $this->assertSame('containsChild', $collection->getDefaultMethodName(new SentryAccess('contains'), 'children')); } - public function testProcessGetAndSet() - { - $collection = new CollectionType(); - $getMethod = new SentryMethod( - new SentryAccess('get'), - 'getChildren', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $setMethod = new SentryMethod( - new SentryAccess('set'), - 'setChildren', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'children', - FooClass::class, - 'string', - new SentryIdentificator('string[]'), - false, - [ - $getMethod, - $setMethod, - ], - null - ); - $foo = new FooClass(); - - $parameters = ['foo', 'bar']; - $this->assertEmpty($collection->processMethod($propertyMetadata, $foo, $getMethod, [])); - $this->assertNull($collection->processMethod($propertyMetadata, $foo, $setMethod, [$parameters])); - $this->assertEquals($parameters, $collection->processMethod($propertyMetadata, $foo, $getMethod, [])); - } - - public function testProcessSetChangeValues() - { - $collection = new CollectionType(); - $getMethod = new SentryMethod( - new SentryAccess('get'), - 'getChildren', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $setMethod = new SentryMethod( - new SentryAccess('set'), - 'setChildren', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'children', - FooClass::class, - 'string', - new SentryIdentificator('string[]'), - false, - [ - $getMethod, - $setMethod, - ], - null - ); - $foo = new FooClass(); - - $parameters = ['foo', 'bar']; - $this->assertNull($collection->processMethod($propertyMetadata, $foo, $setMethod, [$parameters])); - $this->assertEquals($parameters, $collection->processMethod($propertyMetadata, $foo, $getMethod, [])); - - $newParameters = ['test']; - $this->assertNull($collection->processMethod($propertyMetadata, $foo, $setMethod, [$newParameters])); - $this->assertContains('test', $collection->processMethod($propertyMetadata, $foo, $getMethod, [])); - $this->assertNotContains('foo', $collection->processMethod($propertyMetadata, $foo, $getMethod, [])); - $this->assertNotContains('bar', $collection->processMethod($propertyMetadata, $foo, $getMethod, [])); - } - - public function testProcessSetNotArrayParams() - { - $collection = new CollectionType(); - $setMethod = new SentryMethod( - new SentryAccess('set'), - 'setChildren', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'children', - FooClass::class, - 'string', - new SentryIdentificator('string[]'), - false, - [ - $setMethod, - ], - null - ); - $foo = new FooClass(); - - $this->expectException(\Consistence\InvalidArgumentTypeException::class); - $this->expectExceptionMessage('array expected'); - - $collection->processMethod($propertyMetadata, $foo, $setMethod, ['foo']); - } - - public function testProcessSetWrongType() - { - $collection = new CollectionType(); - $setMethod = new SentryMethod( - new SentryAccess('set'), - 'setChildren', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'children', - FooClass::class, - 'string', - new SentryIdentificator('string[]'), - false, - [ - $setMethod, - ], - null - ); - $foo = new FooClass(); - - $this->expectException(\Consistence\InvalidArgumentTypeException::class); - $this->expectExceptionMessage('string expected'); - - $collection->processMethod($propertyMetadata, $foo, $setMethod, [[1, 2]]); - } - - public function testProcessContains() - { - $collection = new CollectionType(); - $setMethod = new SentryMethod( - new SentryAccess('set'), - 'setChildren', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $containsMethod = new SentryMethod( - new SentryAccess('contains'), - 'containsChild', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'children', - FooClass::class, - 'string', - new SentryIdentificator('string[]'), - false, - [ - $setMethod, - $containsMethod, - ], - null - ); - $foo = new FooClass(); - $this->assertNull($collection->processMethod($propertyMetadata, $foo, $setMethod, [['foo', 'bar']])); - - $this->assertTrue($collection->processMethod($propertyMetadata, $foo, $containsMethod, ['foo'])); - $this->assertTrue($collection->processMethod($propertyMetadata, $foo, $containsMethod, ['bar'])); - $this->assertFalse($collection->processMethod($propertyMetadata, $foo, $containsMethod, ['xxx'])); - } - - public function testProcessAdd() - { - $collection = new CollectionType(); - $addMethod = new SentryMethod( - new SentryAccess('add'), - 'addChildren', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $containsMethod = new SentryMethod( - new SentryAccess('contains'), - 'containsChild', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'children', - FooClass::class, - 'string', - new SentryIdentificator('string[]'), - false, - [ - $addMethod, - $containsMethod, - ], - null - ); - $foo = new FooClass(); - - $this->assertFalse($collection->processMethod($propertyMetadata, $foo, $containsMethod, ['foo'])); - $this->assertTrue($collection->processMethod($propertyMetadata, $foo, $addMethod, ['foo'])); - $this->assertTrue($collection->processMethod($propertyMetadata, $foo, $containsMethod, ['foo'])); - } - - public function testProcessAddOnlyOnce() - { - $collection = new CollectionType(); - $addMethod = new SentryMethod( - new SentryAccess('add'), - 'addChildren', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $getMethod = new SentryMethod( - new SentryAccess('get'), - 'getChildren', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'children', - FooClass::class, - 'string', - new SentryIdentificator('string[]'), - false, - [ - $addMethod, - $getMethod, - ], - null - ); - $foo = new FooClass(); - - $this->assertEmpty($collection->processMethod($propertyMetadata, $foo, $getMethod, [])); - $this->assertTrue($collection->processMethod($propertyMetadata, $foo, $addMethod, ['foo'])); - $this->assertFalse($collection->processMethod($propertyMetadata, $foo, $addMethod, ['foo'])); - $this->assertCount(1, $collection->processMethod($propertyMetadata, $foo, $getMethod, [])); - } - - public function testProcessRemove() - { - $collection = new CollectionType(); - $setMethod = new SentryMethod( - new SentryAccess('set'), - 'setChildren', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $removeMethod = new SentryMethod( - new SentryAccess('remove'), - 'removeChildren', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $containsMethod = new SentryMethod( - new SentryAccess('contains'), - 'containsChild', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'children', - FooClass::class, - 'string', - new SentryIdentificator('string[]'), - false, - [ - $setMethod, - $removeMethod, - $containsMethod, - ], - null - ); - $foo = new FooClass(); - - $this->assertNull($collection->processMethod($propertyMetadata, $foo, $setMethod, [['foo', 'bar']])); - $this->assertTrue($collection->processMethod($propertyMetadata, $foo, $removeMethod, ['foo'])); - $this->assertFalse($collection->processMethod($propertyMetadata, $foo, $containsMethod, ['foo'])); - $this->assertTrue($collection->processMethod($propertyMetadata, $foo, $containsMethod, ['bar'])); - } - - public function testProcessRemoveMissing() - { - $collection = new CollectionType(); - $removeMethod = new SentryMethod( - new SentryAccess('remove'), - 'removeChildren', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'children', - FooClass::class, - 'string', - new SentryIdentificator('string[]'), - false, - [ - $removeMethod, - ], - null - ); - $foo = new FooClass(); - - $this->assertFalse($collection->processMethod($propertyMetadata, $foo, $removeMethod, ['foo'])); - } - public function testGenerateGet() { $collection = new CollectionType(); diff --git a/tests/Type/SimpleTypeTest.php b/tests/Type/SimpleTypeTest.php index dd7284c..2db9746 100644 --- a/tests/Type/SimpleTypeTest.php +++ b/tests/Type/SimpleTypeTest.php @@ -13,127 +13,6 @@ class SimpleTypeTest extends \PHPUnit\Framework\TestCase { - public function testProcessGetAndSet() - { - $integerSentry = new SimpleType(); - $getMethod = new SentryMethod( - new SentryAccess('get'), - 'getFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $setMethod = new SentryMethod( - new SentryAccess('set'), - 'setFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'fooProperty', - FooClass::class, - 'int', - new SentryIdentificator('int'), - false, - [ - $getMethod, - $setMethod, - ], - null - ); - $foo = new FooClass(); - - $this->assertNull($integerSentry->processMethod($propertyMetadata, $foo, $getMethod, [])); - $this->assertNull($integerSentry->processMethod($propertyMetadata, $foo, $setMethod, [123])); - $this->assertSame(123, $integerSentry->processMethod($propertyMetadata, $foo, $getMethod, [])); - } - - public function testProcessSetNull() - { - $integerSentry = new SimpleType(); - $getMethod = new SentryMethod( - new SentryAccess('get'), - 'getFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $setMethod = new SentryMethod( - new SentryAccess('set'), - 'setFoo', - Visibility::get(Visibility::VISIBILITY_PUBLIC) - ); - $propertyMetadata = new PropertyMetadata( - 'fooProperty', - FooClass::class, - 'int', - new SentryIdentificator('int|null'), - true, - [ - $getMethod, - $setMethod, - ], - null - ); - $foo = new FooClass(); - - $this->assertNull($integerSentry->processMethod($propertyMetadata, $foo, $getMethod, [])); - $this->assertNull($integerSentry->processMethod($propertyMetadata, $foo, $setMethod, [123])); - $this->assertSame(123, $integerSentry->processMethod($propertyMetadata, $foo, $getMethod, [])); - - $this->assertNull($integerSentry->processMethod($propertyMetadata, $foo, $setMethod, [null])); - $this->assertNull($integerSentry->processMethod($propertyMetadata, $foo, $getMethod, [])); - } - - public function testProcessSetWrongType() - { - $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 - ); - $foo = new FooClass(); - - $this->expectException(\Consistence\InvalidArgumentTypeException::class); - $this->expectExceptionMessage('int expected, 123 [string] given'); - - $integerSentry->processMethod($propertyMetadata, $foo, $setMethod, ['123']); - } - - public function testProcessSetNullableWrongType() - { - $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 - ); - $foo = new FooClass(); - - $this->expectException(\Consistence\InvalidArgumentTypeException::class); - $this->expectExceptionMessage('int|null expected, 123 [string] given'); - - $integerSentry->processMethod($propertyMetadata, $foo, $setMethod, ['123']); - } - public function testGenerateGet() { $integerSentry = new SimpleType();