mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-28 10:40:17 +02:00
Add unit tests and usage example to README
This commit is contained in:
@@ -43,8 +43,10 @@ class Attribute implements ValueAccessInterface
|
||||
*/
|
||||
public function removeValue(ValueInterface $value)
|
||||
{
|
||||
if ($key = array_search($value, $this->values, true)) {
|
||||
unset($this->values[$key]);
|
||||
$index = array_search($value, $this->values, true);
|
||||
|
||||
if (false !== $index) {
|
||||
unset($this->values[$index]);
|
||||
}
|
||||
|
||||
return $this;
|
||||
|
@@ -43,8 +43,10 @@ class Entity implements ValueAccessInterface
|
||||
*/
|
||||
public function removeValue(ValueInterface $value)
|
||||
{
|
||||
if ($key = array_search($value, $this->values, true)) {
|
||||
unset($this->values[$key]);
|
||||
$index = array_search($value, $this->values, true);
|
||||
|
||||
if (false !== $index) {
|
||||
unset($this->values[$index]);
|
||||
}
|
||||
|
||||
return $this;
|
||||
|
@@ -9,7 +9,74 @@ Purpose
|
||||
Examples
|
||||
--------
|
||||
|
||||
...
|
||||
```
|
||||
use DesignPatterns\More\EAV\Entity;
|
||||
use DesignPatterns\More\EAV\Attribute;
|
||||
use DesignPatterns\More\EAV\Value;
|
||||
|
||||
// color attribute
|
||||
$color = (new Attribute())->setName('Color');
|
||||
// color values
|
||||
$colorSilver = (new Value($color))->setName('Silver');
|
||||
$colorGold = (new Value($color))->setName('Gold');
|
||||
$colorSpaceGrey = (new Value($color))->setName('Space Grey');
|
||||
|
||||
// memory attribute
|
||||
$memory = (new Attribute())->setName('Memory');
|
||||
// memory values
|
||||
$memory4Gb = (new Value($memory))->setName('4GB');
|
||||
$memory8Gb = (new Value($memory))->setName('8GB');
|
||||
$memory16Gb = (new Value($memory))->setName('16GB');
|
||||
|
||||
// storage attribute
|
||||
$storage = (new Attribute())->setName('Storage');
|
||||
// storage values
|
||||
$storage128Gb = (new Value($storage))->setName('128GB');
|
||||
$storage256Gb = (new Value($storage))->setName('256GB');
|
||||
$storage512Gb = (new Value($storage))->setName('512GB');
|
||||
$storage1Tb = (new Value($storage))->setName('1TB');
|
||||
|
||||
// entities
|
||||
$mac = (new Entity())
|
||||
->setName('MacBook')
|
||||
// colors
|
||||
->addValue($colorSilver)
|
||||
->addValue($colorGold)
|
||||
->addValue($colorSpaceGrey)
|
||||
// memories
|
||||
->addValue($memory8Gb)
|
||||
// storages
|
||||
->addValue($storage256Gb)
|
||||
->addValue($storage512Gb)
|
||||
;
|
||||
|
||||
$macAir = (new Entity())
|
||||
->setName('MacBook Air')
|
||||
// colors
|
||||
->addValue($colorSilver)
|
||||
// memories
|
||||
->addValue($memory4Gb)
|
||||
->addValue($memory8Gb)
|
||||
// storages
|
||||
->addValue($storage128Gb)
|
||||
->addValue($storage256Gb)
|
||||
->addValue($storage512Gb)
|
||||
;
|
||||
|
||||
$macPro = (new Entity())
|
||||
->setName('MacBook Pro')
|
||||
// colors
|
||||
->addValue($colorSilver)
|
||||
// memories
|
||||
->addValue($memory8Gb)
|
||||
->addValue($memory16Gb)
|
||||
// storages
|
||||
->addValue($storage128Gb)
|
||||
->addValue($storage256Gb)
|
||||
->addValue($storage512Gb)
|
||||
->addValue($storage1Tb)
|
||||
;
|
||||
```
|
||||
|
||||
UML Diagram
|
||||
-----------
|
||||
@@ -24,7 +91,23 @@ You can also find these code on `GitHub`_
|
||||
Test
|
||||
----
|
||||
|
||||
...
|
||||
Tests/EntityTest.php
|
||||
|
||||
.. literalinclude:: Tests/EntityTest.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Tests/AttributeTest.php
|
||||
|
||||
.. literalinclude:: Tests/AttributeTest.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Tests/ValueTest.php
|
||||
|
||||
.. literalinclude:: Tests/ValueTest.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
|
||||
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/More/EAV
|
||||
|
66
More/EAV/Tests/AttributeTest.php
Normal file
66
More/EAV/Tests/AttributeTest.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\More\EAV\Tests;
|
||||
|
||||
use DesignPatterns\More\EAV\Attribute;
|
||||
use DesignPatterns\More\EAV\Value;
|
||||
|
||||
/**
|
||||
* AttributeTest tests the Attribute model of EAV pattern
|
||||
*/
|
||||
class AttributeTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testCreationSuccess()
|
||||
{
|
||||
$attribute = new Attribute();
|
||||
|
||||
$this->assertInstanceOf('\DesignPatterns\More\EAV\Attribute', $attribute);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreationSuccess
|
||||
*/
|
||||
public function testSetGetName()
|
||||
{
|
||||
$attribute = new Attribute();
|
||||
$attribute->setName('Color');
|
||||
|
||||
$this->assertEquals('Color', $attribute->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreationSuccess
|
||||
*/
|
||||
public function testAddValue()
|
||||
{
|
||||
$attribute = new Attribute();
|
||||
$attribute->setName('Color');
|
||||
|
||||
$values[] = (new Value($attribute))->setName('Silver');
|
||||
$values[] = (new Value($attribute))->setName('Gold');
|
||||
$values[] = (new Value($attribute))->setName('Space Grey');
|
||||
|
||||
$this->assertEquals($values, $attribute->getValues());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testAddValue
|
||||
*/
|
||||
public function testRemoveValue()
|
||||
{
|
||||
$values = [];
|
||||
|
||||
$attribute = new Attribute();
|
||||
$attribute->setName('Color');
|
||||
|
||||
$values[] = (new Value($attribute))->setName('Silver');
|
||||
$values[] = (new Value($attribute))->setName('Gold');
|
||||
$values[] = (new Value($attribute))->setName('Space Grey');
|
||||
|
||||
$randomIndex = array_rand($values);
|
||||
$attribute->removeValue($values[$randomIndex]);
|
||||
unset($values[$randomIndex]);
|
||||
|
||||
$this->assertEquals($values, $attribute->getValues());
|
||||
}
|
||||
}
|
132
More/EAV/Tests/EntityTest.php
Normal file
132
More/EAV/Tests/EntityTest.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\More\EAV\Tests;
|
||||
|
||||
use DesignPatterns\More\EAV\Entity;
|
||||
use DesignPatterns\More\EAV\Attribute;
|
||||
use DesignPatterns\More\EAV\Value;
|
||||
|
||||
/**
|
||||
* EntityTest tests the Entity model of EAV pattern
|
||||
*/
|
||||
class EntityTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider valueProvider
|
||||
*
|
||||
* @var string $name
|
||||
*/
|
||||
public function testSetGetName($name)
|
||||
{
|
||||
$macBook = new Entity();
|
||||
$macBook->setName($name);
|
||||
|
||||
$this->assertEquals($name, $macBook->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider valueProvider
|
||||
*
|
||||
* @var string $name
|
||||
* @var array|Value[] $values
|
||||
*/
|
||||
public function testAddValue($name, array $values)
|
||||
{
|
||||
$macBook = new Entity();
|
||||
$macBook->setName($name);
|
||||
|
||||
foreach ($values as $value) {
|
||||
$macBook->addValue($value);
|
||||
}
|
||||
|
||||
$this->assertEquals($values, $macBook->getValues());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testAddValue
|
||||
* @dataProvider valueProvider
|
||||
*
|
||||
* @var string $name
|
||||
* @var array|Value[] $values
|
||||
*/
|
||||
public function testRemoveValue($name, array $values)
|
||||
{
|
||||
$macBook = new Entity();
|
||||
$macBook->setName($name);
|
||||
|
||||
foreach ($values as $value) {
|
||||
$macBook->addValue($value);
|
||||
}
|
||||
|
||||
$randomIndex = array_rand($values);
|
||||
$macBook->removeValue($values[$randomIndex]);
|
||||
unset($values[$randomIndex]);
|
||||
|
||||
$this->assertEquals($values, $macBook->getValues());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function valueProvider()
|
||||
{
|
||||
// color attribute
|
||||
$color = (new Attribute())->setName('Color');
|
||||
// color values
|
||||
$colorSilver = (new Value($color))->setName('Silver');
|
||||
$colorGold = (new Value($color))->setName('Gold');
|
||||
$colorSpaceGrey = (new Value($color))->setName('Space Grey');
|
||||
|
||||
// memory attribute
|
||||
$memory = (new Attribute())->setName('Memory');
|
||||
// memory values
|
||||
$memory4Gb = (new Value($memory))->setName('4GB');
|
||||
$memory8Gb = (new Value($memory))->setName('8GB');
|
||||
$memory16Gb = (new Value($memory))->setName('16GB');
|
||||
|
||||
// storage attribute
|
||||
$storage = (new Attribute())->setName('Storage');
|
||||
// storage values
|
||||
$storage128Gb = (new Value($storage))->setName('128GB');
|
||||
$storage256Gb = (new Value($storage))->setName('256GB');
|
||||
$storage512Gb = (new Value($storage))->setName('512GB');
|
||||
$storage1Tb = (new Value($storage))->setName('1TB');
|
||||
|
||||
return [
|
||||
[
|
||||
'MacBook',
|
||||
[
|
||||
$colorSilver,
|
||||
$colorGold,
|
||||
$colorSpaceGrey,
|
||||
$memory8Gb,
|
||||
$storage256Gb,
|
||||
$storage512Gb,
|
||||
],
|
||||
],
|
||||
[
|
||||
'MacBook Air',
|
||||
[
|
||||
$colorSilver,
|
||||
$memory4Gb,
|
||||
$memory8Gb,
|
||||
$storage128Gb,
|
||||
$storage256Gb,
|
||||
$storage512Gb,
|
||||
],
|
||||
],
|
||||
[
|
||||
'MacBook Pro',
|
||||
[
|
||||
$colorSilver,
|
||||
$memory8Gb,
|
||||
$memory16Gb,
|
||||
$storage128Gb,
|
||||
$storage256Gb,
|
||||
$storage512Gb,
|
||||
$storage1Tb,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
59
More/EAV/Tests/ValueTest.php
Normal file
59
More/EAV/Tests/ValueTest.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\More\EAV\Tests;
|
||||
|
||||
use DesignPatterns\More\EAV\Attribute;
|
||||
use DesignPatterns\More\EAV\Value;
|
||||
|
||||
/**
|
||||
* ValueTest tests the Value model of EAV pattern
|
||||
*/
|
||||
class ValueTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testCreationFailureWithoutAttribute()
|
||||
{
|
||||
$isFailure = false;
|
||||
|
||||
try {
|
||||
new Value();
|
||||
} catch (\Exception $e) {
|
||||
$isFailure = true;
|
||||
}
|
||||
|
||||
$this->assertTrue($isFailure);
|
||||
}
|
||||
|
||||
public function testCreationSuccessWithAttribute()
|
||||
{
|
||||
$attribute = new Attribute();
|
||||
$attribute->setName('Color');
|
||||
|
||||
$value = new Value($attribute);
|
||||
|
||||
$this->assertInstanceOf('\DesignPatterns\More\EAV\Value', $value);
|
||||
}
|
||||
|
||||
public function testSetGetName()
|
||||
{
|
||||
$attribute = new Attribute();
|
||||
$attribute->setName('Color');
|
||||
|
||||
$value = new Value($attribute);
|
||||
$value->setName('Silver');
|
||||
|
||||
$this->assertEquals('Silver', $value->getName());
|
||||
}
|
||||
|
||||
public function testSetGetAttribute()
|
||||
{
|
||||
$attribute = new Attribute();
|
||||
$attribute->setName('Color');
|
||||
|
||||
$value = new Value($attribute);
|
||||
$value->setName('Silver');
|
||||
$this->assertSame($attribute, $value->getAttribute());
|
||||
|
||||
$value->setAttribute($attribute);
|
||||
$this->assertSame($attribute, $value->getAttribute());
|
||||
}
|
||||
}
|
@@ -26,6 +26,16 @@ class Value implements ValueInterface
|
||||
$this->attribute = $attribute;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Attribute $attribute
|
||||
*/
|
||||
public function setAttribute(Attribute $attribute)
|
||||
{
|
||||
$this->attribute->removeValue($this); // Remove value from current attribute
|
||||
$attribute->addValue($this); // Add value to new attribute
|
||||
$this->attribute = $attribute;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Attribute
|
||||
*/
|
||||
|
@@ -12,6 +12,11 @@ interface ValueInterface
|
||||
*/
|
||||
public function __construct(Attribute $attribute);
|
||||
|
||||
/**
|
||||
* @param Attribute $attribute
|
||||
*/
|
||||
public function setAttribute(Attribute $attribute);
|
||||
|
||||
/**
|
||||
* @return Attribute
|
||||
*/
|
||||
|
Reference in New Issue
Block a user