From 19260f326b68b1d1488a42f0632b8e03a19aed09 Mon Sep 17 00:00:00 2001 From: Woody Gilk Date: Tue, 5 Apr 2016 10:43:48 -0500 Subject: [PATCH] Remove functionality that is specific to data Also adds missing individual tests. --- CHANGELOG.md | 26 +------- README.md | 16 ++--- composer.json | 17 ++--- src/ArraySerializableInterface.php | 4 +- src/ArraySerializableTrait.php | 23 +++++++ src/DiffableInterface.php | 15 ----- src/EntityInterface.php | 30 --------- .../ImmutableValueObjectTrait.php | 22 +------ src/{Traits => }/JsonAwareTrait.php | 7 +- .../ProtectedValueObjectTrait.php | 2 +- src/Repository/CountRepositoryInterface.php | 15 ----- src/Repository/CreateRepositoryInterface.php | 15 ----- src/Repository/DeleteRepositoryInterface.php | 35 ---------- src/Repository/PartialRepositoryInterface.php | 45 ------------- src/Repository/RepositoryInterface.php | 45 ------------- src/Repository/UpdateRepositoryInterface.php | 16 ----- src/Traits/DateAwareTrait.php | 32 --------- src/Traits/DiffableEntityTrait.php | 37 ----------- src/Traits/DiffableTrait.php | 21 ------ src/Traits/EntityTrait.php | 11 ---- src/Traits/SerializeAwareTrait.php | 31 --------- tests/ArraySerializableTest.php | 39 +++++++++++ tests/Entity.php | 23 ------- tests/EntityTest.php | 66 ------------------- .../{Traits => }/ImmutableValueObjectTest.php | 51 ++++---------- tests/{Traits => }/JsonAwareTest.php | 10 +-- tests/ProtectedValueObjectTest.php | 52 +++++++++++++++ tests/Traits/DateAware.php | 12 ---- tests/Traits/DateAwareTest.php | 26 -------- tests/Traits/Diffable.php | 22 ------- tests/Traits/DiffableEntity.php | 17 ----- tests/Traits/DiffableTest.php | 51 -------------- tests/Traits/JsonAware.php | 18 ----- tests/Traits/ObjectDiffer.php | 12 ---- tests/Traits/SerializeAware.php | 25 ------- tests/Traits/SerializeAwareTest.php | 25 ------- tests/Traits/TypelessImmutableValueObject.php | 14 ---- tests/_fixture/ArraySerializable.php | 14 ++++ .../ImmutableValueObject.php | 8 ++- tests/_fixture/JsonAware.php | 16 +++++ .../NestedImmutableValueObject.php | 8 ++- tests/_fixture/ProtectedObject.php | 13 ++++ .../_fixture/TypelessImmutableValueObject.php | 16 +++++ 43 files changed, 229 insertions(+), 774 deletions(-) create mode 100644 src/ArraySerializableTrait.php delete mode 100644 src/DiffableInterface.php delete mode 100644 src/EntityInterface.php rename src/{Traits => }/ImmutableValueObjectTrait.php (85%) rename src/{Traits => }/JsonAwareTrait.php (60%) rename src/{Traits => }/ProtectedValueObjectTrait.php (97%) delete mode 100644 src/Repository/CountRepositoryInterface.php delete mode 100644 src/Repository/CreateRepositoryInterface.php delete mode 100644 src/Repository/DeleteRepositoryInterface.php delete mode 100644 src/Repository/PartialRepositoryInterface.php delete mode 100644 src/Repository/RepositoryInterface.php delete mode 100644 src/Repository/UpdateRepositoryInterface.php delete mode 100644 src/Traits/DateAwareTrait.php delete mode 100644 src/Traits/DiffableEntityTrait.php delete mode 100644 src/Traits/DiffableTrait.php delete mode 100644 src/Traits/EntityTrait.php delete mode 100644 src/Traits/SerializeAwareTrait.php create mode 100644 tests/ArraySerializableTest.php delete mode 100644 tests/Entity.php delete mode 100644 tests/EntityTest.php rename tests/{Traits => }/ImmutableValueObjectTest.php (69%) rename tests/{Traits => }/JsonAwareTest.php (70%) create mode 100644 tests/ProtectedValueObjectTest.php delete mode 100644 tests/Traits/DateAware.php delete mode 100644 tests/Traits/DateAwareTest.php delete mode 100644 tests/Traits/Diffable.php delete mode 100644 tests/Traits/DiffableEntity.php delete mode 100644 tests/Traits/DiffableTest.php delete mode 100644 tests/Traits/JsonAware.php delete mode 100644 tests/Traits/ObjectDiffer.php delete mode 100644 tests/Traits/SerializeAware.php delete mode 100644 tests/Traits/SerializeAwareTest.php delete mode 100644 tests/Traits/TypelessImmutableValueObject.php create mode 100644 tests/_fixture/ArraySerializable.php rename tests/{Traits => _fixture}/ImmutableValueObject.php (74%) create mode 100644 tests/_fixture/JsonAware.php rename tests/{Traits => _fixture}/NestedImmutableValueObject.php (56%) create mode 100644 tests/_fixture/ProtectedObject.php create mode 100644 tests/_fixture/TypelessImmutableValueObject.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 89f0890..983f77b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,30 +7,6 @@ This project adheres to [Semantic Versioning](http://semver.org/). _..._ -## 2.3.0 - 2016-04-04 - -- Split `ProtectedValueObjectTrait` functionality from `ImmutableValueObjectTrait` - -## 2.2.0 - 2016-03-31 - -* Add `PartialRepositoryInterface` - -## 2.1.0 - 2016-03-25 - -* Modify `RepositoryInterface` to support `Traversable` return values - -## 2.0.0 - 2016-03-24 - -* Add `findByIds()` method to `RepositoryInterface` - -## 1.2.0 - 2016-01-19 - -* Add `CountRepositoryInterface` - -## 1.1.0 - 2016-01-14 - -* Add `DiffableEntityTrait` - ## 1.0.0 - 2016-01-05 -* Changed from `Spark` to `Equip` namespace +* Extract immutable traits from `equip/data` package diff --git a/README.md b/README.md index 14fe138..fa377f2 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ -## Equip Data +## Equip Immutable -[![Latest Stable Version](https://img.shields.io/packagist/v/equip/data.svg)](https://packagist.org/packages/equip/data) -[![License](https://img.shields.io/packagist/l/equip/data.svg)](https://github.com/equip/data/blob/master/LICENSE) -[![Build Status](https://travis-ci.org/equip/data.svg)](https://travis-ci.org/equip/data) -[![Code Coverage](https://scrutinizer-ci.com/g/equip/data/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/equip/data/?branch=master) -[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/equip/data/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/equip/data/?branch=master) +[![Latest Stable Version](https://img.shields.io/packagist/v/equip/immutable.svg)](https://packagist.org/packages/equip/immutable) +[![License](https://img.shields.io/packagist/l/equip/immutable.svg)](https://github.com/equip/immutable/blob/master/LICENSE) +[![Build Status](https://travis-ci.org/equip/immutable.svg)](https://travis-ci.org/equip/immutable) +[![Code Coverage](https://scrutinizer-ci.com/g/equip/immutable/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/equip/immutable/?branch=master) +[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/equip/immutable/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/equip/immutable/?branch=master) -Interfaces and traits for creating a data layer in [Equip](http://equip.github.io/). +Interfaces and traits for creating immutable value objects in [Equip](http://equip.github.io/). Attempts to be [PSR-1](http://www.php-fig.org/psr/psr-1/), [PSR-2](http://www.php-fig.org/psr/psr-2/), and [PSR-4](http://www.php-fig.org/psr/psr-4/) compliant. -For more information, see [the documentation](http://equipframework.readthedocs.org/en/latest/data). +For more information, see [the documentation](http://equipframework.readthedocs.org/en/latest/immutable). diff --git a/composer.json b/composer.json index 44ce036..01f5a59 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { - "name": "equip/data", - "description": "Interfaces and traits for creating a data layer", + "name": "equip/immutable", + "description": "Interfaces and traits for immutable value objects", "type": "library", "license": "MIT", "authors": [ @@ -9,24 +9,21 @@ "homepage": "https://github.com/equip" } ], - "replace": { - "sparkphp/data": "self.version" - }, "require": { - "php": ">=5.5", - "nesbot/carbon": "^1" + "php": ">=5.5" }, - "require-dev": { + "suggest": { "phpunit/phpunit": "^4.8|^5.0" }, "autoload": { "psr-4": { - "Equip\\Data\\": "src/" + "Equip\\Immutable\\": "src/" } }, "autoload-dev": { "psr-4": { - "EquipTests\\Data\\": "tests/" + "Equip\\Immutable\\": "tests/", + "Equip\\Immutable\\Fixture\\": "tests/_fixture/" } } } diff --git a/src/ArraySerializableInterface.php b/src/ArraySerializableInterface.php index 60cbc7d..a654db0 100644 --- a/src/ArraySerializableInterface.php +++ b/src/ArraySerializableInterface.php @@ -1,11 +1,11 @@ toArray(); + } + return $value; + }, get_object_vars($this)); + } +} diff --git a/src/DiffableInterface.php b/src/DiffableInterface.php deleted file mode 100644 index 2264e82..0000000 --- a/src/DiffableInterface.php +++ /dev/null @@ -1,15 +0,0 @@ -toArray(); - } - return $value; - }, get_object_vars($this)); - } } diff --git a/src/Traits/JsonAwareTrait.php b/src/JsonAwareTrait.php similarity index 60% rename from src/Traits/JsonAwareTrait.php rename to src/JsonAwareTrait.php index bf1ecd9..f4626d1 100644 --- a/src/Traits/JsonAwareTrait.php +++ b/src/JsonAwareTrait.php @@ -1,9 +1,14 @@ $key); - } - - /** - * Get a storage-friendly date string for a property - * - * @param string $key - * - * @return string - */ - public function dateString($key) - { - return $this->date($key)->format('r'); - } -} diff --git a/src/Traits/DiffableEntityTrait.php b/src/Traits/DiffableEntityTrait.php deleted file mode 100644 index 557636a..0000000 --- a/src/Traits/DiffableEntityTrait.php +++ /dev/null @@ -1,37 +0,0 @@ -withData($values); - return array_diff_assoc($copy->toArray(), $this->toArray()); - } -} diff --git a/src/Traits/DiffableTrait.php b/src/Traits/DiffableTrait.php deleted file mode 100644 index d0bc367..0000000 --- a/src/Traits/DiffableTrait.php +++ /dev/null @@ -1,21 +0,0 @@ -diff($values); - } -} diff --git a/src/Traits/EntityTrait.php b/src/Traits/EntityTrait.php deleted file mode 100644 index 45cdd86..0000000 --- a/src/Traits/EntityTrait.php +++ /dev/null @@ -1,11 +0,0 @@ -toArray()); - } - - /** - * @see \Equip\Data\Traits\ImmutableValueObjectTrait::apply() - * - * @inheritDoc - */ - public function unserialize($data) - { - $this->apply(unserialize($data)); - } -} diff --git a/tests/ArraySerializableTest.php b/tests/ArraySerializableTest.php new file mode 100644 index 0000000..6461a75 --- /dev/null +++ b/tests/ArraySerializableTest.php @@ -0,0 +1,39 @@ +id = 1; + $object->name = 'Test Case'; + + $data = $object->toArray(); + + $this->assertArrayHasKey('id', $data); + $this->assertArrayHasKey('name', $data); + + $this->assertSame($object->id, $data['id']); + $this->assertSame($object->name, $data['name']); + + $object->name = new ArraySerializable; + $object->name->id = 5; + $object->name->name = 'nested'; + + $data = $object->toArray(); + + $this->assertSame([ + 'id' => $object->id, + 'name' => [ + 'id' => $object->name->id, + 'name' => $object->name->name, + ], + ], $data); + } +} diff --git a/tests/Entity.php b/tests/Entity.php deleted file mode 100644 index e6fe258..0000000 --- a/tests/Entity.php +++ /dev/null @@ -1,23 +0,0 @@ - 'int', - 'name' => 'string', - ]; - } -} diff --git a/tests/EntityTest.php b/tests/EntityTest.php deleted file mode 100644 index d585c83..0000000 --- a/tests/EntityTest.php +++ /dev/null @@ -1,66 +0,0 @@ -data = [ - 'id' => 42, - 'name' => 'Life', - 'created_at' => '2015-10-31 11:10:33', - ]; - - $this->entity = new Entity($this->data); - } - - public function testImplements() - { - $this->assertInstanceOf('Equip\Data\EntityInterface', $this->entity); - $this->assertInstanceOf('JsonSerializable', $this->entity); - $this->assertInstanceOf('Serializable', $this->entity); - } - - public function testToArray() - { - $this->assertSame($this->data, $this->entity->toArray()); - } - - public function testDate() - { - $this->assertInstanceOf('Carbon\Carbon', $this->entity->date('created_at')); - } - - public function testJsonEncode() - { - $json = json_encode($this->entity); - - $this->assertJson($json); - - $this->assertSame($this->data, json_decode($json, true)); - } - - public function testSerialize() - { - $frozen = serialize($this->entity); - - $this->assertInternalType('string', $frozen); - - $thawed = unserialize($frozen); - - $this->assertInstanceOf(get_class($this->entity), $thawed); - $this->assertNotSame($this->entity, $thawed); - $this->assertSame($this->data, $thawed->toArray()); - } -} diff --git a/tests/Traits/ImmutableValueObjectTest.php b/tests/ImmutableValueObjectTest.php similarity index 69% rename from tests/Traits/ImmutableValueObjectTest.php rename to tests/ImmutableValueObjectTest.php index f2847be..1563bcb 100644 --- a/tests/Traits/ImmutableValueObjectTest.php +++ b/tests/ImmutableValueObjectTest.php @@ -1,8 +1,13 @@ assertTrue($object->has($key)); - $this->assertSame($data[$key], $object->$key); + $this->assertSame($data[$key], $object->toArray()[$key]); } } @@ -49,10 +54,12 @@ public function testConstructionSetsType() $object = new ImmutableValueObject($data); + $values = $object->toArray(); + $this->assertNotSame($data, $object->toArray()); - $this->assertSame(10, $object->id); - $this->assertSame('1337', $object->name); + $this->assertSame(10, $values['id']); + $this->assertSame('1337', $values['name']); // Without type coercion, input data is exactly the same $object = new TypelessImmutableValueObject($data); @@ -84,36 +91,6 @@ public function testValidateThrowsException() ]); } - /** - * @expectedException \RuntimeException - * @expectedExceptionMessageRegExp /modification of immutable object .* not allowed/i - */ - public function testSetThrowsException() - { - $object = new ImmutableValueObject; - $object->name = 'Cheery Charlie'; - } - - /** - * @expectedException \RuntimeException - * @expectedExceptionMessageRegExp /modification of immutable object .* not allowed/i - */ - public function testUnsetThrowsException() - { - $object = new ImmutableValueObject; - unset($object->name); - } - - public function testPropertyIsSet() - { - $object = new ImmutableValueObject([ - 'name' => 'Eager Erin', - ]); - - $this->assertFalse(isset($object->id)); - $this->assertTrue(isset($object->name)); - } - public function testWithData() { $data = [ @@ -126,8 +103,8 @@ public function testWithData() $this->assertNotSame($object, $copied); - $this->assertSame('Jonny Jones', $object->name); - $this->assertSame('JJ', $copied->name); + $this->assertSame('Jonny Jones', $object->toArray()['name']); + $this->assertSame('JJ', $copied->toArray()['name']); } public function testToArrayRecursion() diff --git a/tests/Traits/JsonAwareTest.php b/tests/JsonAwareTest.php similarity index 70% rename from tests/Traits/JsonAwareTest.php rename to tests/JsonAwareTest.php index 9796a6f..00648bb 100644 --- a/tests/Traits/JsonAwareTest.php +++ b/tests/JsonAwareTest.php @@ -1,10 +1,13 @@ assertSame($object->id, $data['id']); $this->assertSame($object->name, $data['name']); - } } diff --git a/tests/ProtectedValueObjectTest.php b/tests/ProtectedValueObjectTest.php new file mode 100644 index 0000000..8b4dc2e --- /dev/null +++ b/tests/ProtectedValueObjectTest.php @@ -0,0 +1,52 @@ +object = new ProtectedObject; + } + + public function testGet() + { + $this->assertSame(1, $this->object->id); + $this->assertSame('Test', $this->object->name); + } + + public function testIsset() + { + $this->assertTrue(isset($this->object->id)); + $this->assertTrue(isset($this->object->name)); + } + + public function testSet() + { + $this->setExpectedExceptionRegExp( + RuntimeException::class, + '/modification of immutable object .+ is not allowed/i' + ); + + $this->object->id = 10; + } + + public function testUnset() + { + $this->setExpectedExceptionRegExp( + RuntimeException::class, + '/modification of immutable object .+ is not allowed/i' + ); + + unset($this->object->id); + } +} diff --git a/tests/Traits/DateAware.php b/tests/Traits/DateAware.php deleted file mode 100644 index cf97dc0..0000000 --- a/tests/Traits/DateAware.php +++ /dev/null @@ -1,12 +0,0 @@ -object = new DateAware; - $this->object->at = '2015-10-30 15:05:00'; - } - - public function testDate() - { - $date = $this->object->date('at'); - $this->assertInstanceOf('Carbon\Carbon', $date); - } - - public function testDateString() - { - $date = $this->object->dateString('at'); - $this->assertStringStartsWith('Fri, 30 Oct 2015 15:05:00 ', $date); - } -} diff --git a/tests/Traits/Diffable.php b/tests/Traits/Diffable.php deleted file mode 100644 index 41f5927..0000000 --- a/tests/Traits/Diffable.php +++ /dev/null @@ -1,22 +0,0 @@ -toArray()); - } - - public function toArray() - { - return get_object_vars($this); - } -} diff --git a/tests/Traits/DiffableEntity.php b/tests/Traits/DiffableEntity.php deleted file mode 100644 index 6504bc5..0000000 --- a/tests/Traits/DiffableEntity.php +++ /dev/null @@ -1,17 +0,0 @@ -object = new Diffable; - $this->object->id = 42; - $this->object->name = 'Life'; - } - - public function testDiff() - { - $differ = new ObjectDiffer; - - $changes = $differ->diff($this->object, [ - 'id' => 42, - 'name' => 'World', - ]); - - $this->assertSame(['name' => 'World'], $changes); - - $changes = $differ->diff($this->object, [ - 'id' => 5, - ]); - - $this->assertSame(['id' => 5], $changes); - - $changes = $differ->diff($this->object, $this->object->toArray()); - - $this->assertEmpty($changes); - } - - public function testEntityDiff() - { - $entity = new DiffableEntity([ - 'id' => 99, - 'name' => 'Joe', - 'role' => 'admin', - ]); - - $change = [ - 'role' => 'user', - ]; - $values = array_replace($entity->toArray(), $change); - - $this->assertSame($change, $entity->diff($values)); - } -} diff --git a/tests/Traits/JsonAware.php b/tests/Traits/JsonAware.php deleted file mode 100644 index 76cf942..0000000 --- a/tests/Traits/JsonAware.php +++ /dev/null @@ -1,18 +0,0 @@ - $value) { - $this->$key = $value; - } - } - - public function toArray() - { - return get_object_vars($this); - } -} diff --git a/tests/Traits/SerializeAwareTest.php b/tests/Traits/SerializeAwareTest.php deleted file mode 100644 index e0b0e57..0000000 --- a/tests/Traits/SerializeAwareTest.php +++ /dev/null @@ -1,25 +0,0 @@ -id = 1; - $object->name = 'Test Case'; - - $frozen = serialize($object); - - $this->assertInternalType('string', $frozen); - - $thawed = unserialize($frozen); - - $this->assertNotSame($object, $thawed); - - $this->assertSame($object->id, $thawed->id); - $this->assertSame($object->name, $thawed->name); - } -} diff --git a/tests/Traits/TypelessImmutableValueObject.php b/tests/Traits/TypelessImmutableValueObject.php deleted file mode 100644 index 60511d3..0000000 --- a/tests/Traits/TypelessImmutableValueObject.php +++ /dev/null @@ -1,14 +0,0 @@ -