diff --git a/src/Entity.php b/src/Entity.php index 3d4e0ba..d467d73 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -21,6 +21,16 @@ public function __construct(Model $model, string $id, ?stdClass $data = null) $this->data->id = $id; } + public function __set($name, $value) : void + { + $this->data->{$name} = $value; + } + + public function __unset($name) + { + unset($this->data->{$name}); + } + public static function createFromJsonFile($filePath, string $id) : Entity { if (!file_exists($filePath)) { diff --git a/src/Exception/BadMethodCallException.php b/src/Exception/BadMethodCallException.php new file mode 100644 index 0000000..06ff2e9 --- /dev/null +++ b/src/Exception/BadMethodCallException.php @@ -0,0 +1,9 @@ +data->{$name}; } - public function __set($name, $value) : void - { - $this->data->{$name} = $value; - } - public function __isset($name) : bool { return property_exists($this->data, $name); } - public function __unset($name) - { - unset($this->data->{$name}); - } - - public function validate() : bool + final public function validate() : bool { return $this->validateData($this->data, $this->model->getSchema()); } - public function validatePartially(array $requiredFields) : bool + final public function validatePartially(array $requiredFields) : bool { $model = $this->model->getModel(); $model->required = $requiredFields; @@ -75,16 +65,11 @@ private function validateData($data, $schema) : bool public function equals($rightHandedObject) : bool { - return (string) $this === (string) $rightHandedObject; + return (string) json_encode($this->data) === (string) json_encode($rightHandedObject); } public function jsonSerialize() : stdClass { return $this->data; } - - public function __toString() : string - { - return (string) json_encode($this); - } } diff --git a/src/ValueObject.php b/src/ValueObject.php index 5c0ec84..4ffd6d2 100644 --- a/src/ValueObject.php +++ b/src/ValueObject.php @@ -6,30 +6,39 @@ use stdClass; use JsonSerializable; use Selami\Entity\Exception\UnexpectedValueException; +use Selami\Entity\Exception\BadMethodCallException; final class ValueObject implements JsonSerializable { use ObjectTrait; - public function __construct(Model $model, ?stdClass $data = null) + public function __construct(Model $model, stdClass $data) { $this->model = $model; $this->data = $data; - if ($data === null) { - $this->data = new stdClass(); - } } - public static function createFromJsonFile($filePath) : ValueObject + final public function __set($name, $value) : void + { + throw new BadMethodCallException('Can\'t manipulate Immutable Object'); + } + + final public function __unset($name) + { + throw new BadMethodCallException('Can\'t manipulate Immutable Object'); + } + + public static function createFromJsonFile($filePath, stdClass $data) : ValueObject { if (!file_exists($filePath)) { throw new UnexpectedValueException(sprintf('Model definition file (%s) does not exist!', $filePath)); } $json = file_get_contents($filePath); - return static::createFromJson($json); + return static::createFromJson($json, $data); } - public static function createFromJson($json) : ValueObject + + public static function createFromJson($json, stdClass $data) : ValueObject { - return new static(new Model($json)); + return new static(new Model($json), $data); } } diff --git a/tests/unit/EntityTest.php b/tests/unit/EntityTest.php index d513625..3a08b3b 100644 --- a/tests/unit/EntityTest.php +++ b/tests/unit/EntityTest.php @@ -40,7 +40,7 @@ public function shouldReturnEntityObjectSuccessfully() : void $item->value = 100; $entity->skills = [$item]; $this->assertTrue($entity->validate()); - $arrayFromJson = json_decode($entity, true); + $arrayFromJson = json_decode(json_encode($entity), true); $this->assertEquals(31, $arrayFromJson['age']); $this->assertTrue(isset($entity->name)); unset($entity->name); @@ -62,6 +62,7 @@ public function shouldValidatePartiallySuccessfully() : void $this->expectException(\Selami\Entity\Exception\InvalidArgumentException::class); $entity->validatePartially($requiredFields); } + /** * @test */ @@ -91,9 +92,6 @@ public function shouldCompareTwoEntityObjectSuccessfully() : void $this->assertFalse($entity1->equals($entity3)); } - - - /** * @test * @expectedException \Selami\Entity\Exception\InvalidArgumentException diff --git a/tests/unit/ValueObjectTest.php b/tests/unit/ValueObjectTest.php index adde5b6..536fdf2 100644 --- a/tests/unit/ValueObjectTest.php +++ b/tests/unit/ValueObjectTest.php @@ -23,7 +23,7 @@ protected function _after() */ public function shouldReturnValueObjectSuccessfully() : void { - $valueObject = ValueObject::createFromJsonFile(__DIR__.'/../resources/test-schema-value-object.json'); + $valueObject = new stdClass(); $valueObject->name = 'John Doe'; $valueObject->age = 31; $valueObject->email = "john@example.com"; @@ -37,12 +37,16 @@ public function shouldReturnValueObjectSuccessfully() : void $item->name = 'PHP'; $item->value = 100; $valueObject->skills = [$item]; - $this->assertTrue($valueObject->validate()); - $arrayFromJson = json_decode($valueObject, true); + + $valObject = ValueObject::createFromJsonFile( + __DIR__.'/../resources/test-schema-value-object.json', + $valueObject + ); + + $this->assertTrue($valObject->validate()); + $arrayFromJson = json_decode(json_encode($valObject), true); $this->assertEquals(31, $arrayFromJson['age']); - $this->assertTrue(isset($valueObject->name)); - unset($valueObject->name); - $this->assertFalse(isset($valueObject->name)); + $this->assertTrue(isset($valObject->name)); } /** @@ -50,39 +54,56 @@ public function shouldReturnValueObjectSuccessfully() : void */ public function shouldValidatePartiallySuccessfully() : void { - $valueObject = ValueObject::createFromJsonFile(__DIR__.'/../resources/test-schema-value-object.json'); + $valueObject = new stdClass(); $valueObject->name = 'John Doe'; $valueObject->age = 31; $requiredFields = ['name', 'age']; - $this->assertTrue($valueObject->validatePartially($requiredFields)); + $valObject = ValueObject::createFromJsonFile( + __DIR__.'/../resources/test-schema-value-object.json', + $valueObject + ); + + $this->assertTrue($valObject->validatePartially($requiredFields)); $requiredFields = ['name', 'age', 'email']; $this->expectException(\Selami\Entity\Exception\InvalidArgumentException::class); - $valueObject->validatePartially($requiredFields); + $valObject->validatePartially($requiredFields); } /** * @test */ public function shouldCompareTwoValueObjectSuccessfully() : void { - $valueObject1 = ValueObject::createFromJsonFile(__DIR__.'/../resources/test-schema-value-object.json'); - $valueObject2 = ValueObject::createFromJsonFile(__DIR__.'/../resources/test-schema-value-object.json'); - $valueObject3 = ValueObject::createFromJsonFile(__DIR__.'/../resources/test-schema-value-object.json'); + $object1 = new stdClass(); + $object2 = new stdClass(); + $object3 = new stdClass(); - $valueObject1->name = 'Kedibey'; - $valueObject1->details = new stdClass(); - $valueObject1->details->age = 11; - $valueObject1->details->type = 'Angora'; + $object1->name = 'Kedibey'; + $object1->details = new stdClass(); + $object1->details->age = 11; + $object1->details->type = 'Angora'; - $valueObject2->name = 'Kedibey'; - $valueObject2->details = new stdClass(); - $valueObject2->details->age = 11; - $valueObject2->details->type = 'Angora'; + $object2->name = 'Kedibey'; + $object2->details = new stdClass(); + $object2->details->age = 11; + $object2->details->type = 'Angora'; - $valueObject3->name = 'Kedibey'; - $valueObject3->details = new stdClass(); - $valueObject3->details->age = 11; - $valueObject3->details->type = 'Van'; + $object3->name = 'Kedibey'; + $object3->details = new stdClass(); + $object3->details->age = 11; + $object3->details->type = 'Van'; + $valueObject1 = ValueObject::createFromJsonFile( + __DIR__.'/../resources/test-schema-value-object.json', + $object1 + ); + $valueObject2 = ValueObject::createFromJsonFile( + __DIR__.'/../resources/test-schema-value-object.json', + $object2 + ); + $valueObject3 = ValueObject::createFromJsonFile( + __DIR__.'/../resources/test-schema-value-object.json', + $object3 + ); $this->assertTrue($valueObject1->equals($valueObject2)); $this->assertFalse($valueObject1->equals($valueObject3)); } @@ -96,12 +117,14 @@ public function shouldCompareTwoValueObjectSuccessfully() : void */ public function shouldFailForRequiredInput() : void { + $object = new stdClass(); + $object->name = 'John Doe'; + $object->age = 31; + $object->email = "john@example.com"; + $object->website = null; + $model = Model::createFromJsonFile(__DIR__.'/../resources/test-schema.json'); - $valueObject = new ValueObject($model); - $valueObject->name = 'John Doe'; - $valueObject->age = 31; - $valueObject->email = "john@example.com"; - $valueObject->website = null; + $valueObject = new ValueObject($model, $object); $valueObject->validate(); } @@ -111,6 +134,31 @@ public function shouldFailForRequiredInput() : void */ public function shouldFailForAModelFileDoesNotExist() : void { - ValueObject::createFromJsonFile(__DIR__.'/../resources/test-schema-no-file.json'); + ValueObject::createFromJsonFile(__DIR__.'/../resources/test-schema-no-file.json', new stdClass()); + } + + /** + * @test + * @expectedException \Selami\Entity\Exception\BadMethodCallException + */ + public function shouldFailForSettingNewValue() : void + { + $valueObject = ValueObject::createFromJsonFile(__DIR__.'/../resources/test-schema-value-object.json', new stdClass()); + $valueObject->name = 'Kedibey'; + } + /** + * @test + * @expectedException \Selami\Entity\Exception\BadMethodCallException + */ + public function shouldFailForUnsettingAValue() : void + { + $object = new stdClass(); + $object->name='Mırmır'; + + $valueObject = ValueObject::createFromJsonFile( + __DIR__.'/../resources/test-schema-value-object.json', + $object + ); + unset($valueObject->name); } }