mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-09-25 05:51:46 +02:00
Add unit tests and usage example to README
This commit is contained in:
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());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user