From 38ac1226ac909ef7637a54703a4c7f5567792adb Mon Sep 17 00:00:00 2001 From: 1stthomas Date: Sun, 4 Oct 2020 16:14:55 +0200 Subject: [PATCH] #29 base model classes implemented --- src/Models/AbstractLoadableModel.php | 32 +++++++ src/Models/AbstractTransLoadableModel.php | 52 ++++++++++ src/Models/Loadable.php | 36 +++++++ src/Models/TransLoadable.php | 37 +++++++ src/Utility/Traits/CanAddByPropertyName.php | 25 +++++ .../Unit/Models/AbstractLoadableModelTest.php | 46 +++++++++ .../Models/AbstractTransLoadableModelTest.php | 96 +++++++++++++++++++ tests/Unit/Models/LoadableModelTest_.php | 30 ++++++ .../Traits/CanAddByPropertyNameTest.php | 67 +++++++++++++ 9 files changed, 421 insertions(+) create mode 100644 src/Models/AbstractLoadableModel.php create mode 100644 src/Models/AbstractTransLoadableModel.php create mode 100644 src/Models/Loadable.php create mode 100644 src/Models/TransLoadable.php create mode 100644 src/Utility/Traits/CanAddByPropertyName.php create mode 100644 tests/Unit/Models/AbstractLoadableModelTest.php create mode 100644 tests/Unit/Models/AbstractTransLoadableModelTest.php create mode 100644 tests/Unit/Models/LoadableModelTest_.php create mode 100644 tests/Unit/Utility/Traits/CanAddByPropertyNameTest.php diff --git a/src/Models/AbstractLoadableModel.php b/src/Models/AbstractLoadableModel.php new file mode 100644 index 0000000..3a0d322 --- /dev/null +++ b/src/Models/AbstractLoadableModel.php @@ -0,0 +1,32 @@ +load($data); + } + + public function load(array $data) + { + foreach ($data as $name => $value) { + $this->addByPropertyName($name, $value); + } + } + +} diff --git a/src/Models/AbstractTransLoadableModel.php b/src/Models/AbstractTransLoadableModel.php new file mode 100644 index 0000000..50a6b03 --- /dev/null +++ b/src/Models/AbstractTransLoadableModel.php @@ -0,0 +1,52 @@ +setTransformations($transformations); + + $this->load($data); + } + + public function getTransformations() + { + return $this->transformations; + } + + public function load(array $data) + { + $transformations = $this->getTransformations(); + + foreach ($data as $name => $value) { + if (array_key_exists($name, $transformations)) { + $name = $transformations[$name]; + } + + $this->addByPropertyName($name, $value); + } + } + + public function setTransformations(array $transformations) + { + $this->transformations = $transformations; + } + +} diff --git a/src/Models/Loadable.php b/src/Models/Loadable.php new file mode 100644 index 0000000..59fe823 --- /dev/null +++ b/src/Models/Loadable.php @@ -0,0 +1,36 @@ +$method($value); + } else { + throw new \InvalidArgumentException('Unkown property: ' . $name); + } + } + +} diff --git a/tests/Unit/Models/AbstractLoadableModelTest.php b/tests/Unit/Models/AbstractLoadableModelTest.php new file mode 100644 index 0000000..caedde1 --- /dev/null +++ b/tests/Unit/Models/AbstractLoadableModelTest.php @@ -0,0 +1,46 @@ +getMockBuilder($className) + ->setMethods(['load']) + ->getMockForAbstractClass(); + + $mock->expects($this->once()) + ->method('load') + ->with($this->equalTo($items)); + + $reflectedClass = new \ReflectionClass($className); + $constructor = $reflectedClass->getConstructor(); + $constructor->invoke($mock, $items); + } + + public function testLoad() + { + $className = AbstractLoadableModel::class; + + $items = [1, 2, 5]; + + $mock = $this->getMockBuilder($className) + ->setMethods(['addByPropertyName']) + ->getMockForAbstractClass(); + $mock->expects($this->exactly(3)) + ->method('addByPropertyName') + ->withConsecutive([0, 1], [1, 2], [2, 5]); + + $mock->load($items); + } + +} diff --git a/tests/Unit/Models/AbstractTransLoadableModelTest.php b/tests/Unit/Models/AbstractTransLoadableModelTest.php new file mode 100644 index 0000000..a8a78fb --- /dev/null +++ b/tests/Unit/Models/AbstractTransLoadableModelTest.php @@ -0,0 +1,96 @@ + 'testProperty1', + 'test2' => 'testProperty2', + ]; + + $mock = $this->getMockBuilder($className) + ->setMethods(['getTransformations', 'setTransformations', 'load']) + ->getMockForAbstractClass(); + + $mock->expects($this->once()) + ->method('setTransformations') + ->with($this->equalTo($transformations)); + $mock->expects($this->once()) + ->method('load') + ->with($this->equalTo($items)); + + $reflectedClass = new \ReflectionClass($className); + $constructor = $reflectedClass->getConstructor(); + $constructor->invoke($mock, $items, $transformations); + } + + public function testLoad() + { + $className = AbstractTransLoadableModel::class; + + $items = [1, 2, 5]; + + /* @var $mock AbstractTransLoadableModel */ + $mock = $this->getMockBuilder($className) + ->setMethods(['getTransformations', 'addByPropertyName']) + ->getMockForAbstractClass(); + + $mock->expects($this->once()) + ->method('getTransformations') + ->willReturn([0 => 10, 1 => 100, 2 => 1000]); + $mock->expects($this->exactly(3)) + ->method('addByPropertyName') + ->withConsecutive( + [$this->equalTo(10), $this->equalTo(1)], + [$this->equalTo(100), $this->equalTo(2)], + [$this->equalTo(1000), $this->equalTo(5)] + ); + + $mock->load($items); + } + + public function testGetTranformations() + { + $className = AbstractTransLoadableModel::class; + + $expected = [0 => 10, 5 => 20]; + + $mock = $this->getMockForAbstractClass($className); + + $reflectedClass = new \ReflectionClass($className); + $property = $reflectedClass->getProperty('transformations'); + $property->setAccessible(true); + $property->setValue($mock, $expected); + + $this->assertEquals($expected, $mock->getTransformations()); + } + + public function testSetTranformations() + { + $className = AbstractTransLoadableModel::class; + + $expected = [0 => 10, 5 => 20]; + + $mock = $this->getMockForAbstractClass($className); + + $mock->setTransformations($expected); + + $reflectedClass = new \ReflectionClass($className); + $property = $reflectedClass->getProperty('transformations'); + $property->setAccessible(true); + $property->setValue($mock, $expected); + + $this->assertEquals($expected, $property->getValue($mock)); + } + +} diff --git a/tests/Unit/Models/LoadableModelTest_.php b/tests/Unit/Models/LoadableModelTest_.php new file mode 100644 index 0000000..20f6423 --- /dev/null +++ b/tests/Unit/Models/LoadableModelTest_.php @@ -0,0 +1,30 @@ +getMockBuilder($className) + ->setMethods(['load']) + ->disableOriginalConstructor() + ->getMock(); + $mock->expects($this->once()) + ->method('load') + ->with($this->equalTo($items)); + + $reflectedClass = new \ReflectionClass($className); + $constructor = $reflectedClass->getConstructor(); + $constructor->invoke($mock, $items); + } + +} diff --git a/tests/Unit/Utility/Traits/CanAddByPropertyNameTest.php b/tests/Unit/Utility/Traits/CanAddByPropertyNameTest.php new file mode 100644 index 0000000..aea4dc2 --- /dev/null +++ b/tests/Unit/Utility/Traits/CanAddByPropertyNameTest.php @@ -0,0 +1,67 @@ + 'value 1', + 'testProperty2' => 'value 2' + ]; + + $mock = new class + { + + use CanAddByPropertyName; + + protected $testOne; + protected $testTwo; + + public function setTestProperty1($test1) + { + return $this->testOne = $test1; + } + + public function setTestProperty2($test2) + { + $this->testTwo = $test2; + } + }; + + $reflectedClass = new \ReflectionClass($mock); + $testProperty1 = $reflectedClass->getProperty('testOne'); + $testProperty1->setAccessible(true); + $testProperty2 = $reflectedClass->getProperty('testTwo'); + $testProperty2->setAccessible(true); + + $this->assertNull($testProperty1->getValue($mock)); + $this->assertNull($testProperty2->getValue($mock)); + + $mock->addByPropertyName('testProperty1', $items['testProperty1']); + $mock->addByPropertyName('testProperty2', $items['testProperty2']); + + $this->assertEquals($items['testProperty1'], $testProperty1->getValue($mock)); + $this->assertEquals($items['testProperty2'], $testProperty2->getValue($mock)); + } + + public function testAddByPropertyNameWithException() + { + $mock = $this->getMockForTrait(CanAddByPropertyName::class); + + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageRegExp('/(U|u)nkown property(.)+unknownProperty/'); + + $mock->addByPropertyName('unknownProperty', 'value'); + } + +}