1
0
mirror of https://github.com/DesignPatternsPHP/DesignPatternsPHP.git synced 2025-07-19 22:31:14 +02:00
This commit is contained in:
Antonio Spinelli
2014-04-16 17:59:03 -03:00
parent da35c96b90
commit fc3b6a1608
25 changed files with 65 additions and 64 deletions

@@ -45,7 +45,8 @@ class ChainTest extends \PHPUnit_Framework_TestCase
$this->assertObjectHasAttribute('response', $request); $this->assertObjectHasAttribute('response', $request);
$this->assertEquals('baz', $request->response); $this->assertEquals('baz', $request->response);
// despite both handle owns the 'bar' key, the FastStorage is responding first // despite both handle owns the 'bar' key, the FastStorage is responding first
$this->assertEquals('DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\FastStorage', $request->forDebugOnly); $className = 'DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\FastStorage';
$this->assertEquals($className, $request->forDebugOnly);
} }
/** /**
@@ -60,7 +61,8 @@ class ChainTest extends \PHPUnit_Framework_TestCase
$this->assertObjectHasAttribute('response', $request); $this->assertObjectHasAttribute('response', $request);
$this->assertEquals('bar', $request->response); $this->assertEquals('bar', $request->response);
// FastStorage has no 'foo' key, the SlowStorage is responding // FastStorage has no 'foo' key, the SlowStorage is responding
$this->assertEquals('DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowStorage', $request->forDebugOnly); $className = 'DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowStorage';
$this->assertEquals($className, $request->forDebugOnly);
} }
/** /**
@@ -73,6 +75,7 @@ class ChainTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($ret); $this->assertFalse($ret);
// the last responsible : // the last responsible :
$this->assertEquals('DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowStorage', $request->forDebugOnly); $className = 'DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowStorage';
$this->assertEquals($className, $request->forDebugOnly);
} }
} }

@@ -13,7 +13,7 @@ class BookList implements \Countable
return $this->books[$bookNumberToGet]; return $this->books[$bookNumberToGet];
} }
return NULL; return null;
} }
public function addBook(Book $book) public function addBook(Book $book)

@@ -74,4 +74,4 @@ class BookListIterator implements \Iterator
{ {
$this->currentBook = 0; $this->currentBook = 0;
} }
} }

@@ -20,4 +20,4 @@ class BookListReverseIterator extends BookListIterator
{ {
return 0 <= $this->currentBook; return 0 <= $this->currentBook;
} }
} }

@@ -63,4 +63,4 @@ class IteratorTest extends \PHPUnit_Framework_TestCase
$iterator->next(); $iterator->next();
} }
} }
} }

@@ -53,10 +53,9 @@ class ObserverTest extends \PHPUnit_Framework_TestCase
$subject->attach($observer); $subject->attach($observer);
$observer->expects($this->once()) $observer->expects($this->once())
->method('update') ->method('update')
->with($subject); ->with($subject);
$subject->notify(); $subject->notify();
} }
}
}

@@ -8,18 +8,18 @@ abstract class AbstractSpecification implements SpecificationInterface
{ {
/** /**
* Checks if given item meets all criteria * Checks if given item meets all criteria
* *
* @param Item $item * @param Item $item
* *
* @return bool * @return bool
*/ */
abstract public function isSatisfiedBy(Item $item); abstract public function isSatisfiedBy(Item $item);
/** /**
* Creates a new logical AND specification * Creates a new logical AND specification
* *
* @param SpecificationInterface $spec * @param SpecificationInterface $spec
* *
* @return SpecificationInterface * @return SpecificationInterface
*/ */
public function plus(SpecificationInterface $spec) public function plus(SpecificationInterface $spec)
@@ -29,9 +29,9 @@ abstract class AbstractSpecification implements SpecificationInterface
/** /**
* Creates a new logical OR composite specification * Creates a new logical OR composite specification
* *
* @param SpecificationInterface $spec * @param SpecificationInterface $spec
* *
* @return SpecificationInterface * @return SpecificationInterface
*/ */
public function either(SpecificationInterface $spec) public function either(SpecificationInterface $spec)
@@ -41,11 +41,11 @@ abstract class AbstractSpecification implements SpecificationInterface
/** /**
* Creates a new logical NOT specification * Creates a new logical NOT specification
* *
* @return SpecificationInterface * @return SpecificationInterface
*/ */
public function not() public function not()
{ {
return new Not($this); return new Not($this);
} }
} }

@@ -31,4 +31,4 @@ class Not extends AbstractSpecification
{ {
return !$this->spec->isSatisfiedBy($item); return !$this->spec->isSatisfiedBy($item);
} }
} }

@@ -12,10 +12,9 @@ class Plus extends AbstractSpecification
/** /**
* Creation of a locical AND of two specifications * Creation of a locical AND of two specifications
* *
* @param SpecificationInterface $left * @param SpecificationInterface $left
* @param SpecificationInterface $right * @param SpecificationInterface $right
*/ */
public function __construct(SpecificationInterface $left, SpecificationInterface $right) public function __construct(SpecificationInterface $left, SpecificationInterface $right)
{ {
@@ -25,13 +24,13 @@ class Plus extends AbstractSpecification
/** /**
* Checks if the composite AND of specifications passes * Checks if the composite AND of specifications passes
* *
* @param Item $item * @param Item $item
* *
* @return bool * @return bool
*/ */
public function isSatisfiedBy(Item $item) public function isSatisfiedBy(Item $item)
{ {
return $this->left->isSatisfiedBy($item) && $this->right->isSatisfiedBy($item); return $this->left->isSatisfiedBy($item) && $this->right->isSatisfiedBy($item);
} }
} }

@@ -31,17 +31,17 @@ class PriceSpecification extends AbstractSpecification
/** /**
* Checks if Item price falls between bounds * Checks if Item price falls between bounds
* *
* @param Item $item * @param Item $item
* *
* @return bool * @return bool
*/ */
public function isSatisfiedBy(Item $item) public function isSatisfiedBy(Item $item)
{ {
if ( !empty($this->maxPrice) && $item->getPrice() > $this->maxPrice) { if (!empty($this->maxPrice) && $item->getPrice() > $this->maxPrice) {
return false; return false;
} }
if ( !empty($this->minPrice) && $item->getPrice() < $this->minPrice) { if (!empty($this->minPrice) && $item->getPrice() < $this->minPrice) {
return false; return false;
} }

@@ -8,26 +8,24 @@ interface SpecificationInterface
{ {
/** /**
* A boolean evaluation indicating if the object meets the specification * A boolean evaluation indicating if the object meets the specification
* *
* @param Item $item * @param Item $item
* *
* @return bool * @return bool
*/ */
public function isSatisfiedBy(Item $item); public function isSatisfiedBy(Item $item);
/** /**
* Creates a logical AND specification * Creates a logical AND specification
* *
* @param SpecificationInterface $spec * @param SpecificationInterface $spec
*/ */
public function plus(SpecificationInterface $spec); public function plus(SpecificationInterface $spec);
/** /**
* Creates a logical OR specification * Creates a logical OR specification
* *
* @param SpecificationInterface $spec * @param SpecificationInterface $spec
*/ */
public function either(SpecificationInterface $spec); public function either(SpecificationInterface $spec);
@@ -35,4 +33,4 @@ interface SpecificationInterface
* Creates a logical not specification * Creates a logical not specification
*/ */
public function not(); public function not();
} }

@@ -30,10 +30,10 @@ class ObjectCollection
*/ */
public function sort() public function sort()
{ {
if (!$this->comparator){ if (!$this->comparator) {
throw new \LogicException("Comparator is not set"); throw new \LogicException("Comparator is not set");
} }
$callback = array($this->comparator, 'compare'); $callback = array($this->comparator, 'compare');
uasort($this->elements, $callback); uasort($this->elements, $callback);

@@ -31,8 +31,8 @@ class JourneyTest extends \PHPUnit_Framework_TestCase
{ {
$journey = $this->getMockForAbstractClass('DesignPatterns\Behavioral\TemplateMethod\Journey'); $journey = $this->getMockForAbstractClass('DesignPatterns\Behavioral\TemplateMethod\Journey');
$journey->expects($this->once()) $journey->expects($this->once())
->method('enjoyVacation') ->method('enjoyVacation')
->will($this->returnCallback(array($this, 'mockUpVacation'))); ->will($this->returnCallback(array($this, 'mockUpVacation')));
$this->expectOutputRegex('#Las Vegas#'); $this->expectOutputRegex('#Las Vegas#');
$journey->takeATrip(); $journey->takeATrip();
} }
@@ -41,5 +41,4 @@ class JourneyTest extends \PHPUnit_Framework_TestCase
{ {
echo "Fear and loathing in Las Vegas\n"; echo "Fear and loathing in Las Vegas\n";
} }
}
}

@@ -43,5 +43,4 @@ class VisitorTest extends \PHPUnit_Framework_TestCase
$mock = $this->getMockForAbstractClass('DesignPatterns\Behavioral\Visitor\Role'); $mock = $this->getMockForAbstractClass('DesignPatterns\Behavioral\Visitor\Role');
$mock->accept($this->visitor); $mock->accept($this->visitor);
} }
}
}

@@ -7,7 +7,7 @@ class Pool
private $instances = array(); private $instances = array();
private $class; private $class;
public function __construct($class) public function __construct($class)
{ {
$this->class = $class; $this->class = $class;
@@ -26,5 +26,4 @@ class Pool
{ {
$this->instances[] = $instance; $this->instances[] = $instance;
} }
} }

@@ -2,8 +2,6 @@
namespace DesignPatterns\Creational\Pool; namespace DesignPatterns\Creational\Pool;
use DesignPatterns\Creational\Pool\Pool;
class TestWorker class TestWorker
{ {
@@ -27,4 +25,3 @@ class PoolTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(1, $pool->get()->id); $this->assertEquals(1, $pool->get()->id);
} }
} }

@@ -49,5 +49,4 @@ class Processor
{ {
return array_pop($this->waitingQueue); return array_pop($this->waitingQueue);
} }
} }

@@ -17,5 +17,4 @@ class Worker
// and when it's done, execute callback // and when it's done, execute callback
call_user_func($callback, $this); call_user_func($callback, $this);
} }
} }

@@ -41,5 +41,4 @@ class SimpleFactoryTest extends \PHPUnit_Framework_TestCase
{ {
$this->factory->createVehicle('car'); $this->factory->createVehicle('car');
} }
}
}

@@ -33,7 +33,7 @@ class AdapterTest extends \PHPUnit_Framework_TestCase
* *
* @dataProvider getBook * @dataProvider getBook
*/ */
public function test_I_am_an_old_Client(PaperBookInterface $book) public function testIAmAnOldClient(PaperBookInterface $book)
{ {
$book->open(); $book->open();
$book->turnPage(); $book->turnPage();

@@ -29,6 +29,8 @@ class CompositeTest extends \PHPUnit_Framework_TestCase
*/ */
public function testFormImplementsFormEelement() public function testFormImplementsFormEelement()
{ {
$this->assertTrue(is_subclass_of('DesignPatterns\Structural\Composite\Form', 'DesignPatterns\Structural\Composite\FormElement')); $className = 'DesignPatterns\Structural\Composite\Form';
$abstractName = 'DesignPatterns\Structural\Composite\FormElement';
$this->assertTrue(is_subclass_of($className, $abstractName));
} }
} }

@@ -65,7 +65,12 @@ class DataMapperTest extends \PHPUnit_Framework_TestCase
*/ */
public function testRestoreOne(User $existing) public function testRestoreOne(User $existing)
{ {
$rows = new \ArrayIterator(array(array('userid' => 1, 'username' => 'Odysseus', 'email' => 'Odysseus@ithaca.gr'))); $row = array(
'userid' => 1,
'username' => 'Odysseus',
'email' => 'Odysseus@ithaca.gr'
);
$rows = new \ArrayIterator(array($row));
$this->dbal->expects($this->once()) $this->dbal->expects($this->once())
->method('find') ->method('find')
->with(1) ->with(1)

@@ -30,7 +30,8 @@ class DecoratorTest extends \PHPUnit_Framework_TestCase
// Wrap service with a JSON decorator for renderers // Wrap service with a JSON decorator for renderers
$service = new Decorator\RenderInXml($this->service); $service = new Decorator\RenderInXml($this->service);
// Our Renderer will now output XML instead of an array // Our Renderer will now output XML instead of an array
$this->assertXmlStringEqualsXmlString('<?xml version="1.0"?><foo>bar</foo>', $service->renderData()); $xml = '<?xml version="1.0"?><foo>bar</foo>';
$this->assertXmlStringEqualsXmlString($xml, $service->renderData());
} }
/** /**
@@ -38,7 +39,9 @@ class DecoratorTest extends \PHPUnit_Framework_TestCase
*/ */
public function testDecoratorMustImplementsRenderer() public function testDecoratorMustImplementsRenderer()
{ {
$this->assertTrue(is_subclass_of('DesignPatterns\Structural\Decorator\Decorator', 'DesignPatterns\Structural\Decorator\RendererInterface')); $className = 'DesignPatterns\Structural\Decorator\Decorator';
$interfaceName = 'DesignPatterns\Structural\Decorator\RendererInterface';
$this->assertTrue(is_subclass_of($className, $interfaceName));
} }
/** /**

@@ -13,10 +13,11 @@ class ArrayConfig extends AbstractConfig implements Parameters
* Get parameter * Get parameter
* *
* @param string|int $key * @param string|int $key
* * @param null $default
* @return mixed * @return mixed
*/ */
public function get($key, $default = null) { public function get($key, $default = null)
{
if (isset($this->storage[$key])) { if (isset($this->storage[$key])) {
return $this->storage[$key]; return $this->storage[$key];
} }
@@ -27,7 +28,7 @@ class ArrayConfig extends AbstractConfig implements Parameters
* Set parameter * Set parameter
* *
* @param string|int $key * @param string|int $key
* @param mixed $value * @param mixed $value
*/ */
public function set($key, $value) public function set($key, $value)
{ {

@@ -5,13 +5,13 @@ namespace DesignPatterns\Structural\DependencyInjection;
/** /**
* Parameters interface * Parameters interface
*/ */
interface Parameters interface Parameters
{ {
/** /**
* Get parameter * Get parameter
* *
* @param string|int $key * @param string|int $key
* *
* @return mixed * @return mixed
*/ */
public function get($key); public function get($key);