mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-10 00:46:38 +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());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user