diff --git a/ChainOfResponsibilities/Handler.php b/Behavioral/ChainOfResponsibilities/Handler.php similarity index 97% rename from ChainOfResponsibilities/Handler.php rename to Behavioral/ChainOfResponsibilities/Handler.php index 35ec769..74a89f8 100644 --- a/ChainOfResponsibilities/Handler.php +++ b/Behavioral/ChainOfResponsibilities/Handler.php @@ -1,6 +1,6 @@ chain = new Responsible\FastStorage(array('bar' => 'baz')); - $this->chain->append(new Responsible\SlowStorage(array('bar' => 'baz', 'foo' => 'bar'))); + $this->chain = new FastStorage(array('bar' => 'baz')); + $this->chain->append(new SlowStorage(array('bar' => 'baz', 'foo' => 'bar'))); } public function makeRequest() @@ -40,7 +45,8 @@ class ChainTest extends \PHPUnit_Framework_TestCase $this->assertObjectHasAttribute('response', $request); $this->assertEquals('baz', $request->response); // despite both handle owns the 'bar' key, the FastStorage is responding first - $this->assertEquals('DesignPatterns\ChainOfResponsibilities\Responsible\FastStorage', $request->forDebugOnly); + $className = 'DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\FastStorage'; + $this->assertEquals($className, $request->forDebugOnly); } /** @@ -55,7 +61,8 @@ class ChainTest extends \PHPUnit_Framework_TestCase $this->assertObjectHasAttribute('response', $request); $this->assertEquals('bar', $request->response); // FastStorage has no 'foo' key, the SlowStorage is responding - $this->assertEquals('DesignPatterns\ChainOfResponsibilities\Responsible\SlowStorage', $request->forDebugOnly); + $className = 'DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowStorage'; + $this->assertEquals($className, $request->forDebugOnly); } /** @@ -68,6 +75,7 @@ class ChainTest extends \PHPUnit_Framework_TestCase $this->assertFalse($ret); // the last responsible : - $this->assertEquals('DesignPatterns\ChainOfResponsibilities\Responsible\SlowStorage', $request->forDebugOnly); + $className = 'DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowStorage'; + $this->assertEquals($className, $request->forDebugOnly); } } diff --git a/Command/CommandInterface.php b/Behavioral/Command/CommandInterface.php similarity index 83% rename from Command/CommandInterface.php rename to Behavioral/Command/CommandInterface.php index ebdbb85..ad9117b 100644 --- a/Command/CommandInterface.php +++ b/Behavioral/Command/CommandInterface.php @@ -1,6 +1,6 @@ invoker = new Invoker(); $this->receiver = new Receiver(); } diff --git a/Behavioral/Iterator/Book.php b/Behavioral/Iterator/Book.php new file mode 100644 index 0000000..a0d1114 --- /dev/null +++ b/Behavioral/Iterator/Book.php @@ -0,0 +1,32 @@ +author = $author; + $this->title = $title; + } + + public function getAuthor() + { + return $this->author; + } + + public function getTitle() + { + return $this->title; + } + + public function getAuthorAndTitle() + { + return $this->getTitle() . ' by ' . $this->getAuthor(); + } +} diff --git a/Behavioral/Iterator/BookList.php b/Behavioral/Iterator/BookList.php new file mode 100644 index 0000000..0dda925 --- /dev/null +++ b/Behavioral/Iterator/BookList.php @@ -0,0 +1,42 @@ +count()) { + return $this->books[$bookNumberToGet]; + } + + return null; + } + + public function addBook(Book $book) + { + $this->books[] = $book; + + return $this->count(); + } + + public function removeBook(Book $bookToRemove) + { + foreach ($this as $key => $book) { + /** @var Book $book */ + if ($book->getAuthorAndTitle() === $bookToRemove->getAuthorAndTitle()) { + unset($this->books[$key]); + } + } + + return $this->count(); + } + + public function count() + { + return count($this->books); + } +} diff --git a/Behavioral/Iterator/BookListIterator.php b/Behavioral/Iterator/BookListIterator.php new file mode 100644 index 0000000..bc15ffb --- /dev/null +++ b/Behavioral/Iterator/BookListIterator.php @@ -0,0 +1,77 @@ +bookList = $bookList; + } + + /** + * Return the current book + * @link http://php.net/manual/en/iterator.current.php + * @return Book Can return any type. + */ + public function current() + { + return $this->bookList->getBook($this->currentBook); + } + + /** + * (PHP 5 >= 5.0.0)
+ * Move forward to next element + * @link http://php.net/manual/en/iterator.next.php + * @return void Any returned value is ignored. + */ + public function next() + { + $this->currentBook++; + } + + /** + * (PHP 5 >= 5.0.0)
+ * Return the key of the current element + * @link http://php.net/manual/en/iterator.key.php + * @return mixed scalar on success, or null on failure. + */ + public function key() + { + return $this->currentBook; + } + + /** + * (PHP 5 >= 5.0.0)
+ * Checks if current position is valid + * @link http://php.net/manual/en/iterator.valid.php + * @return boolean The return value will be casted to boolean and then evaluated. + * Returns true on success or false on failure. + */ + public function valid() + { + return $this->currentBook < $this->bookList->count(); + } + + /** + * (PHP 5 >= 5.0.0)
+ * Rewind the Iterator to the first element + * @link http://php.net/manual/en/iterator.rewind.php + * @return void Any returned value is ignored. + */ + public function rewind() + { + $this->currentBook = 0; + } +} diff --git a/Behavioral/Iterator/BookListReverseIterator.php b/Behavioral/Iterator/BookListReverseIterator.php new file mode 100644 index 0000000..973bd0b --- /dev/null +++ b/Behavioral/Iterator/BookListReverseIterator.php @@ -0,0 +1,23 @@ +bookList = $bookList; + $this->currentBook = $this->bookList->count() - 1; + } + + public function next() + { + $this->currentBook--; + } + + public function valid() + { + return 0 <= $this->currentBook; + } +} diff --git a/Iterator/README.md b/Behavioral/Iterator/README.md similarity index 100% rename from Iterator/README.md rename to Behavioral/Iterator/README.md diff --git a/Behavioral/Iterator/Tests/IteratorTest.php b/Behavioral/Iterator/Tests/IteratorTest.php new file mode 100644 index 0000000..2e117d8 --- /dev/null +++ b/Behavioral/Iterator/Tests/IteratorTest.php @@ -0,0 +1,66 @@ +bookList = new BookList(); + $this->bookList->addBook(new Book('Learning PHP Design Patterns', 'William Sanders')); + $this->bookList->addBook(new Book('Professional Php Design Patterns', 'Aaron Saray')); + $this->bookList->addBook(new Book('Clean Code', 'Robert C. Martin')); + } + + public function expectedAuthors() + { + return array( + array( + array( + 'Learning PHP Design Patterns by William Sanders', + 'Professional Php Design Patterns by Aaron Saray', + 'Clean Code by Robert C. Martin' + ) + ), + ); + } + + /** + * @dataProvider expectedAuthors + */ + public function testUseAIteratorAndValidateAuthors($expected) + { + $iterator = new BookListIterator($this->bookList); + + while ($iterator->valid()) { + $expectedBook = array_shift($expected); + $this->assertEquals($expectedBook, $iterator->current()->getAuthorAndTitle()); + $iterator->next(); + } + } + + /** + * @dataProvider expectedAuthors + */ + public function testUseAReverseIteratorAndValidateAuthors($expected) + { + $iterator = new BookListReverseIterator($this->bookList); + + while ($iterator->valid()) { + $expectedBook = array_pop($expected); + $this->assertEquals($expectedBook, $iterator->current()->getAuthorAndTitle()); + $iterator->next(); + } + } +} diff --git a/Mediator/Colleague.php b/Behavioral/Mediator/Colleague.php similarity index 93% rename from Mediator/Colleague.php rename to Behavioral/Mediator/Colleague.php index c6aae18..36dea5d 100644 --- a/Mediator/Colleague.php +++ b/Behavioral/Mediator/Colleague.php @@ -1,6 +1,6 @@ setState("State1"); + //Setting state to State2 + $originator->setState("State2"); + //Saving State2 to Memento + $savedStates[] = $originator->saveToMemento(); + //Setting state to State3 + $originator->setState("State3"); + + // We can request multiple mementos, and choose which one to roll back to. + // Saving State3 to Memento + $savedStates[] = $originator->saveToMemento(); + //Setting state to State4 + $originator->setState("State4"); + + $originator->restoreFromMemento($savedStates[1]); + //State after restoring from Memento: State3 + } +} diff --git a/Behavioral/Memento/Memento.php b/Behavioral/Memento/Memento.php new file mode 100644 index 0000000..4dd2fc8 --- /dev/null +++ b/Behavioral/Memento/Memento.php @@ -0,0 +1,25 @@ +state = $stateToSave; + } + + /** + * @return mixed + */ + public function getState() + { + return $this->state; + } +} diff --git a/Behavioral/Memento/Originator.php b/Behavioral/Memento/Originator.php new file mode 100644 index 0000000..fec4e45 --- /dev/null +++ b/Behavioral/Memento/Originator.php @@ -0,0 +1,35 @@ +state = $state; + } + + /** + * @return Memento + */ + public function saveToMemento() + { + $state = is_object($this->state) ? clone $this->state : $this->state; + + return new Memento($state); + } + + public function restoreFromMemento(Memento $memento) + { + $this->state = $memento->getState(); + } +} diff --git a/Behavioral/Memento/README.md b/Behavioral/Memento/README.md new file mode 100644 index 0000000..83ca2c0 --- /dev/null +++ b/Behavioral/Memento/README.md @@ -0,0 +1,18 @@ +# Memento + +## Purpose + +Provide the ability to restore an object to its previous state (undo via rollback). + +The memento pattern is implemented with three objects: the originator, a caretaker and a memento. +The originator is some object that has an internal state. +The caretaker is going to do something to the originator, but wants to be able to undo the change. +The caretaker first asks the originator for a memento object. Then it does whatever operation (or sequence of operations) it was going to do. +To roll back to the state before the operations, it returns the memento object to the originator. +The memento object itself is an opaque object (one which the caretaker cannot, or should not, change). +When using this pattern, care should be taken if the originator may change other objects or resources - the memento pattern operates on a single object. + +## Examples + +* The seed of a pseudorandom number generator +* The state in a finite state machine diff --git a/Behavioral/Memento/Tests/MementoTest.php b/Behavioral/Memento/Tests/MementoTest.php new file mode 100644 index 0000000..88110a6 --- /dev/null +++ b/Behavioral/Memento/Tests/MementoTest.php @@ -0,0 +1,69 @@ +setState("State1"); + + $this->assertAttributeEquals("State1", "state", $originator); + + $originator->setState("State2"); + + $this->assertAttributeEquals("State2", "state", $originator); + + $savedState = $originator->saveToMemento(); + + $this->assertAttributeEquals("State2", "state", $savedState); + + $originator->setState("State3"); + + $this->assertAttributeEquals("State3", "state", $originator); + + $originator->restoreFromMemento($savedState); + + $this->assertAttributeEquals("State2", "state", $originator); + } + + public function testObjectState() + { + $originator = new Originator(); + + $foo = new \stdClass(); + $foo->data = "foo"; + + $originator->setState($foo); + + $this->assertAttributeEquals($foo, "state", $originator); + + $savedState = $originator->saveToMemento(); + + $this->assertAttributeEquals($foo, "state", $savedState); + + $bar = new \stdClass(); + $bar->data = "bar"; + + $originator->setState($bar); + + $this->assertAttributeEquals($bar, "state", $originator); + + $originator->restoreFromMemento($savedState); + + $this->assertAttributeEquals($foo, "state", $originator); + + $foo->data = null; + + $this->assertAttributeNotEquals($foo, "state", $savedState); + + $this->assertAttributeNotEquals($foo, "state", $originator); + } +} diff --git a/NullObject/LoggerInterface.php b/Behavioral/NullObject/LoggerInterface.php similarity index 85% rename from NullObject/LoggerInterface.php rename to Behavioral/NullObject/LoggerInterface.php index 9acf855..250c5a3 100644 --- a/NullObject/LoggerInterface.php +++ b/Behavioral/NullObject/LoggerInterface.php @@ -1,6 +1,6 @@ expectOutputString('We are in DesignPatterns\NullObject\Service::doSomething'); + $this->expectOutputString('We are in DesignPatterns\Behavioral\NullObject\Service::doSomething'); $service->doSomething(); } } diff --git a/Observer/README.md b/Behavioral/Observer/README.md similarity index 100% rename from Observer/README.md rename to Behavioral/Observer/README.md diff --git a/Tests/Observer/ObserverTest.php b/Behavioral/Observer/Tests/ObserverTest.php similarity index 78% rename from Tests/Observer/ObserverTest.php rename to Behavioral/Observer/Tests/ObserverTest.php index 9508ac9..233de28 100644 --- a/Tests/Observer/ObserverTest.php +++ b/Behavioral/Observer/Tests/ObserverTest.php @@ -1,9 +1,9 @@ expectOutputString('DesignPatterns\Observer\User has been updated'); + $this->expectOutputString('DesignPatterns\Behavioral\Observer\User has been updated'); $subject = new User(); $subject->attach($this->observer); @@ -53,10 +53,9 @@ class ObserverTest extends \PHPUnit_Framework_TestCase $subject->attach($observer); $observer->expects($this->once()) - ->method('update') - ->with($subject); + ->method('update') + ->with($subject); $subject->notify(); } - -} \ No newline at end of file +} diff --git a/Observer/User.php b/Behavioral/Observer/User.php similarity index 97% rename from Observer/User.php rename to Behavioral/Observer/User.php index 3ee3fba..5b6fb4c 100644 --- a/Observer/User.php +++ b/Behavioral/Observer/User.php @@ -1,6 +1,6 @@ spec->isSatisfiedBy($item); } -} \ No newline at end of file +} diff --git a/Specification/Plus.php b/Behavioral/Specification/Plus.php old mode 100755 new mode 100644 similarity index 90% rename from Specification/Plus.php rename to Behavioral/Specification/Plus.php index 32c3801..c4ad546 --- a/Specification/Plus.php +++ b/Behavioral/Specification/Plus.php @@ -1,5 +1,5 @@ left->isSatisfiedBy($item) && $this->right->isSatisfiedBy($item); } -} \ No newline at end of file +} diff --git a/Specification/PriceSpecification.php b/Behavioral/Specification/PriceSpecification.php similarity index 78% rename from Specification/PriceSpecification.php rename to Behavioral/Specification/PriceSpecification.php index fd8387f..747efdf 100644 --- a/Specification/PriceSpecification.php +++ b/Behavioral/Specification/PriceSpecification.php @@ -1,5 +1,5 @@ maxPrice) && $item->getPrice() > $this->maxPrice) { + if (!empty($this->maxPrice) && $item->getPrice() > $this->maxPrice) { return false; } - if ( !empty($this->minPrice) && $item->getPrice() < $this->minPrice) { + if (!empty($this->minPrice) && $item->getPrice() < $this->minPrice) { return false; } diff --git a/Specification/README.md b/Behavioral/Specification/README.md similarity index 100% rename from Specification/README.md rename to Behavioral/Specification/README.md diff --git a/Specification/SpecificationInterface.php b/Behavioral/Specification/SpecificationInterface.php old mode 100755 new mode 100644 similarity index 89% rename from Specification/SpecificationInterface.php rename to Behavioral/Specification/SpecificationInterface.php index b8c1e7c..e4e15a2 --- a/Specification/SpecificationInterface.php +++ b/Behavioral/Specification/SpecificationInterface.php @@ -1,5 +1,5 @@ comparator){ - throw new \LogicException("Comparator is not set"); + if (!$this->comparator) { + throw new \LogicException("Comparator is not set"); } - + $callback = array($this->comparator, 'compare'); uasort($this->elements, $callback); diff --git a/Strategy/README.md b/Behavioral/Strategy/README.md similarity index 100% rename from Strategy/README.md rename to Behavioral/Strategy/README.md diff --git a/Tests/Strategy/StrategyTest.php b/Behavioral/Strategy/Tests/StrategyTest.php similarity index 86% rename from Tests/Strategy/StrategyTest.php rename to Behavioral/Strategy/Tests/StrategyTest.php index 7e587b1..927ae19 100644 --- a/Tests/Strategy/StrategyTest.php +++ b/Behavioral/Strategy/Tests/StrategyTest.php @@ -1,11 +1,11 @@ getMockForAbstractClass('DesignPatterns\TemplateMethod\Journey'); + $journey = $this->getMockForAbstractClass('DesignPatterns\Behavioral\TemplateMethod\Journey'); $journey->expects($this->once()) - ->method('enjoyVacation') - ->will($this->returnCallback(array($this, 'mockUpVacation'))); + ->method('enjoyVacation') + ->will($this->returnCallback(array($this, 'mockUpVacation'))); $this->expectOutputRegex('#Las Vegas#'); $journey->takeATrip(); } @@ -41,5 +41,4 @@ class JourneyTest extends \PHPUnit_Framework_TestCase { echo "Fear and loathing in Las Vegas\n"; } - -} \ No newline at end of file +} diff --git a/Visitor/Group.php b/Behavioral/Visitor/Group.php similarity index 89% rename from Visitor/Group.php rename to Behavioral/Visitor/Group.php index 5d47d6a..3990f9c 100644 --- a/Visitor/Group.php +++ b/Behavioral/Visitor/Group.php @@ -1,6 +1,6 @@ getMockForAbstractClass('DesignPatterns\Visitor\Role'); + $mock = $this->getMockForAbstractClass('DesignPatterns\Behavioral\Visitor\Role'); $mock->accept($this->visitor); } - -} \ No newline at end of file +} diff --git a/Visitor/User.php b/Behavioral/Visitor/User.php similarity index 90% rename from Visitor/User.php rename to Behavioral/Visitor/User.php index f9291fa..e0cc4c7 100644 --- a/Visitor/User.php +++ b/Behavioral/Visitor/User.php @@ -1,6 +1,6 @@ createText('footnotes') ); - $this->assertContainsOnly('DesignPatterns\AbstractFactory\MediaInterface', $article); + $this->assertContainsOnly('DesignPatterns\Creational\AbstractFactory\MediaInterface', $article); /* this is the time to look at the Builder pattern. This pattern * helps you to create complex object like that article above with diff --git a/AbstractFactory/Text.php b/Creational/AbstractFactory/Text.php similarity index 83% rename from AbstractFactory/Text.php rename to Creational/AbstractFactory/Text.php index 248f5c6..5d2da89 100644 --- a/AbstractFactory/Text.php +++ b/Creational/AbstractFactory/Text.php @@ -1,6 +1,6 @@ director->build($builder); - $this->assertInstanceOf('DesignPatterns\Builder\Parts\Vehicle', $newVehicle); + $this->assertInstanceOf('DesignPatterns\Creational\Builder\Parts\Vehicle', $newVehicle); } } diff --git a/FactoryMethod/Bicycle.php b/Creational/FactoryMethod/Bicycle.php similarity index 85% rename from FactoryMethod/Bicycle.php rename to Creational/FactoryMethod/Bicycle.php index 76b4a53..01fa8a0 100644 --- a/FactoryMethod/Bicycle.php +++ b/Creational/FactoryMethod/Bicycle.php @@ -1,6 +1,6 @@ type as $oneType) { $vehicle = $shop->create($oneType); - $this->assertInstanceOf('DesignPatterns\FactoryMethod\VehicleInterface', $vehicle); + $this->assertInstanceOf('DesignPatterns\Creational\FactoryMethod\VehicleInterface', $vehicle); } } diff --git a/FactoryMethod/VehicleInterface.php b/Creational/FactoryMethod/VehicleInterface.php similarity index 80% rename from FactoryMethod/VehicleInterface.php rename to Creational/FactoryMethod/VehicleInterface.php index 5f6d756..a734b61 100644 --- a/FactoryMethod/VehicleInterface.php +++ b/Creational/FactoryMethod/VehicleInterface.php @@ -1,6 +1,6 @@ class = $class; @@ -26,5 +26,4 @@ class Pool { $this->instances[] = $instance; } - } diff --git a/Pool/Processor.php b/Creational/Pool/Processor.php similarity index 96% rename from Pool/Processor.php rename to Creational/Pool/Processor.php index eabd51d..0bb5e67 100644 --- a/Pool/Processor.php +++ b/Creational/Pool/Processor.php @@ -1,6 +1,6 @@ waitingQueue); } - } diff --git a/Pool/README.md b/Creational/Pool/README.md similarity index 100% rename from Pool/README.md rename to Creational/Pool/README.md diff --git a/Tests/Pool/PoolTest.php b/Creational/Pool/Tests/PoolTest.php similarity index 70% rename from Tests/Pool/PoolTest.php rename to Creational/Pool/Tests/PoolTest.php index 0b86c5b..91fde01 100644 --- a/Tests/Pool/PoolTest.php +++ b/Creational/Pool/Tests/PoolTest.php @@ -1,14 +1,13 @@ get(); $this->assertEquals(1, $worker->id); @@ -27,6 +26,4 @@ class PoolTest extends \PHPUnit_Framework_TestCase $this->assertEquals(5, $pool->get()->id); $this->assertEquals(1, $pool->get()->id); } - } - diff --git a/Pool/Worker.php b/Creational/Pool/Worker.php similarity index 89% rename from Pool/Worker.php rename to Creational/Pool/Worker.php index 4d34a0d..acdc1ba 100644 --- a/Pool/Worker.php +++ b/Creational/Pool/Worker.php @@ -1,6 +1,6 @@ factory->createVehicle($type); - $this->assertInstanceOf('DesignPatterns\SimpleFactory\VehicleInterface', $obj); + $this->assertInstanceOf('DesignPatterns\Creational\SimpleFactory\VehicleInterface', $obj); } /** @@ -41,5 +41,4 @@ class SimpleFactoryTest extends \PHPUnit_Framework_TestCase { $this->factory->createVehicle('car'); } - -} \ No newline at end of file +} diff --git a/SimpleFactory/VehicleInterface.php b/Creational/SimpleFactory/VehicleInterface.php similarity index 80% rename from SimpleFactory/VehicleInterface.php rename to Creational/SimpleFactory/VehicleInterface.php index 66dc3fd..eb13a66 100644 --- a/SimpleFactory/VehicleInterface.php +++ b/Creational/SimpleFactory/VehicleInterface.php @@ -1,6 +1,6 @@ assertInstanceOf('DesignPatterns\Singleton\Singleton', $firstCall); + $this->assertInstanceOf('DesignPatterns\Creational\Singleton\Singleton', $firstCall); $secondCall = Singleton::getInstance(); $this->assertSame($firstCall, $secondCall); } diff --git a/StaticFactory/FormatNumber.php b/Creational/StaticFactory/FormatNumber.php similarity index 64% rename from StaticFactory/FormatNumber.php rename to Creational/StaticFactory/FormatNumber.php index 8b20db2..93ac7c7 100644 --- a/StaticFactory/FormatNumber.php +++ b/Creational/StaticFactory/FormatNumber.php @@ -1,6 +1,6 @@ global => evil diff --git a/Tests/StaticFactory/StaticFactoryTest.php b/Creational/StaticFactory/Tests/StaticFactoryTest.php similarity index 65% rename from Tests/StaticFactory/StaticFactoryTest.php rename to Creational/StaticFactory/Tests/StaticFactoryTest.php index d85f266..e8bb7a5 100644 --- a/Tests/StaticFactory/StaticFactoryTest.php +++ b/Creational/StaticFactory/Tests/StaticFactoryTest.php @@ -1,8 +1,8 @@ assertInstanceOf('DesignPatterns\StaticFactory\FormatterInterface', $obj); + $this->assertInstanceOf('DesignPatterns\Creational\StaticFactory\FormatterInterface', $obj); } } diff --git a/Iterator/File.php b/Iterator/File.php deleted file mode 100644 index e2e6a3d..0000000 --- a/Iterator/File.php +++ /dev/null @@ -1,39 +0,0 @@ -rowSet = new Rowset($this); - } - - /** - * processes the rowSet - */ - public function process() - { - // this is the place to show how using an iterator, with foreach - // See the CardGame.php file - $this->rowSet->process(); - } -} diff --git a/Iterator/Row.php b/Iterator/Row.php deleted file mode 100644 index df4002d..0000000 --- a/Iterator/Row.php +++ /dev/null @@ -1,27 +0,0 @@ -data = $data; - } - - /** - * {@inheritdoc} - */ - public function process() - { - // do some fancy things here ... - } -} diff --git a/Iterator/RowSet.php b/Iterator/RowSet.php deleted file mode 100644 index f1ef5d7..0000000 --- a/Iterator/RowSet.php +++ /dev/null @@ -1,100 +0,0 @@ -file = $file; - } - - /** - * composite pattern: run through all rows and process them - * - * @return void - */ - public function process() - { - // this actually calls rewind(), { next(), valid(), key() and current() :} - /** - * THE key feature of the Iterator Pattern is to provide a *public contract* - * to iterate on a collection without knowing how items are handled inside - * the collection. It is not just an easy way to use "foreach" - * - * One cannot see the point of iterator pattern if you iterate on $this. - * This example is unclear and mixed with some Composite pattern ideas. - */ - foreach ($this as $line => $row) { - $row->process(); - } - } - - /** - * {@inheritdoc} - */ - public function rewind() - { - // seek to first line from $this->file - } - - /** - * {@inheritdoc} - */ - public function next() - { - // read the next line from $this->file - if (!$eof) { - $data = ''; // get the line - $this->currentRow = new Row($data); - } else { - $this->currentRow = null; - } - } - - /** - * {@inheritdoc} - */ - public function current() - { - return $this->currentRow; - } - - /** - * {@inheritdoc} - */ - public function valid() - { - return null !== $this->currentRow; - } - - /** - * {@inheritdoc} - */ - public function key() - { - // you would want to increment this in next() or whatsoever - return $this->lineNumber; - } -} diff --git a/Delegation/JuniorDeveloper.php b/More/Delegation/JuniorDeveloper.php similarity index 83% rename from Delegation/JuniorDeveloper.php rename to More/Delegation/JuniorDeveloper.php index ca80033..7506181 100644 --- a/Delegation/JuniorDeveloper.php +++ b/More/Delegation/JuniorDeveloper.php @@ -1,6 +1,6 @@ services = array(); + $this->services = array(); $this->instantiated = array(); - $this->shared = array(); + $this->shared = array(); } /** * Registers a service with specific interface. * - * @param string $interface + * @param string $interface * @param string|object $service - * @param bool $share + * @param bool $share */ public function add($interface, $service, $share = true) { @@ -47,11 +47,11 @@ class ServiceLocator implements ServiceLocatorInterface * in the future even if you will change the service implementation. */ - if(is_object($service) && $share) + if (is_object($service) && $share) { $this->instantiated[$interface] = $service; - + } $this->services[$interface] = (is_object($service) ? get_class($service) : $service); - $this->shared[$interface] = $share; + $this->shared[$interface] = $share; } /** @@ -76,8 +76,9 @@ class ServiceLocator implements ServiceLocatorInterface public function get($interface) { // Retrieves the instance if it exists and it is shared - if(isset($this->instantiated[$interface]) && $this->shared[$interface]) + if (isset($this->instantiated[$interface]) && $this->shared[$interface]) { return $this->instantiated[$interface]; + } // otherwise gets the service registered. $service = $this->services[$interface]; @@ -97,9 +98,9 @@ class ServiceLocator implements ServiceLocatorInterface $object = new $service(); // and saves it if the service must be shared. - if($this->shared[$interface]) + if ($this->shared[$interface]) { $this->instantiated[$interface] = $object; - + } return $object; } -} \ No newline at end of file +} diff --git a/ServiceLocator/ServiceLocatorInterface.php b/More/ServiceLocator/ServiceLocatorInterface.php similarity index 80% rename from ServiceLocator/ServiceLocatorInterface.php rename to More/ServiceLocator/ServiceLocatorInterface.php index ffd4006..3738624 100644 --- a/ServiceLocator/ServiceLocatorInterface.php +++ b/More/ServiceLocator/ServiceLocatorInterface.php @@ -1,8 +1,8 @@ serviceLocator = new ServiceLocator(); + $this->logService = new LogService(); + $this->databaseService = new DatabaseService(); + } + + public function testHasServices() + { + $this->serviceLocator->add('DesignPatterns\More\ServiceLocator\LogServiceInterface', $this->logService); + $this->serviceLocator->add('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', $this->databaseService); + + $this->assertTrue($this->serviceLocator->has('DesignPatterns\More\ServiceLocator\LogServiceInterface')); + $this->assertTrue($this->serviceLocator->has('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface')); + + $this->assertFalse($this->serviceLocator->has('DesignPatterns\More\ServiceLocator\FakeServiceInterface')); + } + + public function testServicesWithObject() + { + $this->serviceLocator->add('DesignPatterns\More\ServiceLocator\LogServiceInterface', $this->logService); + $this->serviceLocator->add('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', $this->databaseService); + + $this->assertSame($this->logService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface')); + $this->assertSame($this->databaseService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface')); + } + + public function testServicesWithClass() + { + $this->serviceLocator + ->add('DesignPatterns\More\ServiceLocator\LogServiceInterface', get_class($this->logService)); + $this->serviceLocator->add('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', get_class($this->databaseService)); + + $this->assertNotSame($this->logService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface')); + $this->assertInstanceOf('DesignPatterns\More\ServiceLocator\LogServiceInterface', $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface')); + + $this->assertNotSame($this->databaseService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface')); + $this->assertInstanceOf('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface')); + } + + public function testServicesNotShared() + { + $this->serviceLocator->add('DesignPatterns\More\ServiceLocator\LogServiceInterface', $this->logService, false); + $this->serviceLocator->add('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', $this->databaseService, false); + + $this->assertNotSame($this->logService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface')); + $this->assertInstanceOf('DesignPatterns\More\ServiceLocator\LogServiceInterface', $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface')); + + $this->assertNotSame($this->databaseService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface')); + $this->assertInstanceOf('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface')); + } +} diff --git a/Pool/index.php b/Pool/index.php deleted file mode 100644 index 3059efe..0000000 --- a/Pool/index.php +++ /dev/null @@ -1,16 +0,0 @@ -process('image1.jpg'); -$processor->process('image2.jpg'); -$processor->process('image3.jpg'); -$processor->process('image4.jpg'); -$processor->process('image5.jpg'); -$processor->process('image6.jpg'); -$processor->process('image7.jpg'); -$processor->process('image8.jpg'); diff --git a/README.md b/README.md index dc9c5eb..d835586 100755 --- a/README.md +++ b/README.md @@ -10,48 +10,49 @@ I think the problem with patterns is that often people do know them but don't kn The patterns can be structured in roughly three different categories. Please click on the [:notebook:](http://en.wikipedia.org/wiki/Software_design_pattern) for a full explanation of the pattern on Wikipedia. -### Creational +### [Creational](Creational) -* [AbstractFactory](AbstractFactory) [:notebook:](http://en.wikipedia.org/wiki/Abstract_factory_pattern) -* [Builder](Builder) [:notebook:](http://en.wikipedia.org/wiki/Builder_pattern) -* [SimpleFactory](SimpleFactory) -* [FactoryMethod](FactoryMethod) [:notebook:](http://en.wikipedia.org/wiki/Factory_method_pattern) -* [StaticFactory](StaticFactory) -* [Prototype](Prototype) [:notebook:](http://en.wikipedia.org/wiki/Prototype_pattern) -* [Pool](Pool) [:notebook:](http://en.wikipedia.org/wiki/Object_pool_pattern) -* [Singleton](Singleton) [:notebook:](http://en.wikipedia.org/wiki/Singleton_pattern) (is considered an anti-pattern! :no_entry:) -* [Multiton](Multiton) (is considered an anti-pattern! :no_entry:) +* [AbstractFactory](Creational/AbstractFactory) [:notebook:](http://en.wikipedia.org/wiki/Abstract_factory_pattern) +* [Builder](Creational/Builder) [:notebook:](http://en.wikipedia.org/wiki/Builder_pattern) +* [FactoryMethod](Creational/FactoryMethod) [:notebook:](http://en.wikipedia.org/wiki/Factory_method_pattern) +* [Multiton](Creational/Multiton) (is considered an anti-pattern! :no_entry:) +* [Pool](Creational/Pool) [:notebook:](http://en.wikipedia.org/wiki/Object_pool_pattern) +* [Prototype](Creational/Prototype) [:notebook:](http://en.wikipedia.org/wiki/Prototype_pattern) +* [SimpleFactory](Creational/SimpleFactory) +* [Singleton](Creational/Singleton) [:notebook:](http://en.wikipedia.org/wiki/Singleton_pattern) (is considered an anti-pattern! :no_entry:) +* [StaticFactory](Creational/StaticFactory) -### Structural +### [Structural](Structural) -* [Adapter](Adapter) [:notebook:](http://en.wikipedia.org/wiki/Adapter_pattern) -* [Bridge](Bridge) [:notebook:](http://en.wikipedia.org/wiki/Bridge_pattern) -* [Composite](Composite) [:notebook:](http://en.wikipedia.org/wiki/Composite_pattern) -* [Decorator](Decorator) [:notebook:](http://en.wikipedia.org/wiki/Decorator_pattern) -* [Facade](Facade) [:notebook:](http://en.wikipedia.org/wiki/Facade_pattern) -* [Proxy](Proxy) [:notebook:](http://en.wikipedia.org/wiki/Proxy_pattern) -* [Registry](Registry) [:notebook:](http://en.wikipedia.org/wiki/Service_locator_pattern) -* [FluentInterface](FluentInterface) [:notebook:](http://en.wikipedia.org/wiki/Fluent_interface) -* [DataMapper](DataMapper) [:notebook:](http://en.wikipedia.org/wiki/Data_mapper_pattern) -* [DependencyInjection](DependencyInjection) [:notebook:](http://en.wikipedia.org/wiki/Dependency_injection) +* [Adapter](Structural/Adapter) [:notebook:](http://en.wikipedia.org/wiki/Adapter_pattern) +* [Bridge](Structural/Bridge) [:notebook:](http://en.wikipedia.org/wiki/Bridge_pattern) +* [Composite](Structural/Composite) [:notebook:](http://en.wikipedia.org/wiki/Composite_pattern) +* [DataMapper](Structural/DataMapper) [:notebook:](http://en.wikipedia.org/wiki/Data_mapper_pattern) +* [Decorator](Structural/Decorator) [:notebook:](http://en.wikipedia.org/wiki/Decorator_pattern) +* [DependencyInjection](Structural/DependencyInjection) [:notebook:](http://en.wikipedia.org/wiki/Dependency_injection) +* [Facade](Structural/Facade) [:notebook:](http://en.wikipedia.org/wiki/Facade_pattern) +* [FluentInterface](Structural/FluentInterface) [:notebook:](http://en.wikipedia.org/wiki/Fluent_interface) +* [Proxy](Structural/Proxy) [:notebook:](http://en.wikipedia.org/wiki/Proxy_pattern) +* [Registry](Structural/Registry) [:notebook:](http://en.wikipedia.org/wiki/Service_locator_pattern) -### Behavioral +### [Behavioral](Behavioral) -* [ChainOfResponsibilities](ChainOfResponsibilities) [:notebook:](http://en.wikipedia.org/wiki/Chain_of_responsibility_pattern) -* [Command](Command) [:notebook:](http://en.wikipedia.org/wiki/Command_pattern) -* [Iterator](Iterator) [:notebook:](http://en.wikipedia.org/wiki/Iterator_pattern) -* [Mediator](Mediator) [:notebook:](http://en.wikipedia.org/wiki/Mediator_pattern) +* [ChainOfResponsibilities](Behavioral/ChainOfResponsibilities) [:notebook:](http://en.wikipedia.org/wiki/Chain_of_responsibility_pattern) +* [Command](Behavioral/Command) [:notebook:](http://en.wikipedia.org/wiki/Command_pattern) +* [Iterator](Behavioral/Iterator) [:notebook:](http://en.wikipedia.org/wiki/Iterator_pattern) +* [Mediator](Behavioral/Mediator) [:notebook:](http://en.wikipedia.org/wiki/Mediator_pattern) * [Memento](Memento) [:notebook:](http://en.wikipedia.org/wiki/Memento_pattern) -* [NullObject](NullObject) [:notebook:](http://en.wikipedia.org/wiki/Null_Object_pattern) -* [Observer](Observer) [:notebook:](http://en.wikipedia.org/wiki/Observer_pattern) -* [Specification](Specification) [:notebook:](http://en.wikipedia.org/wiki/Specification_pattern) -* [State](State) [:notebook:](http://en.wikipedia.org/wiki/State_pattern) -* [Strategy](Strategy) [:notebook:](http://en.wikipedia.org/wiki/Strategy_pattern) -* [TemplateMethod](TemplateMethod) [:notebook:](http://en.wikipedia.org/wiki/Template_method_pattern) -* [Visitor](Visitor) [:notebook:](http://en.wikipedia.org/wiki/Visitor_pattern) +* [NullObject](Behavioral/NullObject) [:notebook:](http://en.wikipedia.org/wiki/Null_Object_pattern) +* [Observer](Behavioral/Observer) [:notebook:](http://en.wikipedia.org/wiki/Observer_pattern) +* [Specification](Behavioral/Specification) [:notebook:](http://en.wikipedia.org/wiki/Specification_pattern) +* [State](Behavioral/State) [:notebook:](http://en.wikipedia.org/wiki/State_pattern) +* [Strategy](Behavioral/Strategy) [:notebook:](http://en.wikipedia.org/wiki/Strategy_pattern) +* [TemplateMethod](Behavioral/TemplateMethod) [:notebook:](http://en.wikipedia.org/wiki/Template_method_pattern) +* [Visitor](Behavioral/Visitor) [:notebook:](http://en.wikipedia.org/wiki/Visitor_pattern) -### More -* [Delegation](Delegation) [:notebook:](http://en.wikipedia.org/wiki/Delegation_pattern) +### [More](More) +* [Delegation](More/Delegation) [:notebook:](http://en.wikipedia.org/wiki/Delegation_pattern) +* [ServiceLocator](More/ServiceLocator) [:notebook:](http://en.wikipedia.org/wiki/Service_locator_pattern) ## Contribute @@ -82,4 +83,3 @@ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - diff --git a/Registry/index.php b/Registry/index.php deleted file mode 100644 index 2b0ecba..0000000 --- a/Registry/index.php +++ /dev/null @@ -1,9 +0,0 @@ -log('foo'); diff --git a/ServiceLocator/DatabaseServiceInterface.php b/ServiceLocator/DatabaseServiceInterface.php deleted file mode 100644 index 9c7d2d7..0000000 --- a/ServiceLocator/DatabaseServiceInterface.php +++ /dev/null @@ -1,8 +0,0 @@ -open(); $book->turnPage(); diff --git a/Structural/Bridge/Assemble.php b/Structural/Bridge/Assemble.php new file mode 100644 index 0000000..2b60889 --- /dev/null +++ b/Structural/Bridge/Assemble.php @@ -0,0 +1,12 @@ +workShop1->work(); + $this->workShop2->work(); + } +} diff --git a/Structural/Bridge/Motorcycle.php b/Structural/Bridge/Motorcycle.php new file mode 100644 index 0000000..b021ce6 --- /dev/null +++ b/Structural/Bridge/Motorcycle.php @@ -0,0 +1,22 @@ +workShop1->work(); + $this->workShop2->work(); + } +} diff --git a/Structural/Bridge/Produce.php b/Structural/Bridge/Produce.php new file mode 100644 index 0000000..b2b0505 --- /dev/null +++ b/Structural/Bridge/Produce.php @@ -0,0 +1,15 @@ +expectOutputString('Car Produced Assembled'); + $vehicle->manufacture(); + } + + public function testMotorcycle() + { + $vehicle = new Motorcycle(new Produce(), new Assemble()); + $this->expectOutputString('Motorcycle Produced Assembled'); + $vehicle->manufacture(); + } +} diff --git a/Structural/Bridge/Vehicle.php b/Structural/Bridge/Vehicle.php new file mode 100644 index 0000000..10b0d03 --- /dev/null +++ b/Structural/Bridge/Vehicle.php @@ -0,0 +1,23 @@ +workShop1 = $workShop1; + $this->workShop2 = $workShop2; + } + + public function manufacture() + { + } +} diff --git a/Structural/Bridge/Workshop.php b/Structural/Bridge/Workshop.php new file mode 100644 index 0000000..dc62886 --- /dev/null +++ b/Structural/Bridge/Workshop.php @@ -0,0 +1,12 @@ +assertTrue(is_subclass_of('DesignPatterns\Composite\Form', 'DesignPatterns\Composite\FormElement')); + $className = 'DesignPatterns\Structural\Composite\Form'; + $abstractName = 'DesignPatterns\Structural\Composite\FormElement'; + $this->assertTrue(is_subclass_of($className, $abstractName)); } } diff --git a/Composite/TextElement.php b/Structural/Composite/TextElement.php similarity index 87% rename from Composite/TextElement.php rename to Structural/Composite/TextElement.php index 9ea4a4e..ed50294 100644 --- a/Composite/TextElement.php +++ b/Structural/Composite/TextElement.php @@ -1,6 +1,6 @@ dbal = $this->getMockBuilder('DesignPatterns\DataMapper\DBAL') + $this->dbal = $this->getMockBuilder('DesignPatterns\Structural\DataMapper\DBAL') ->disableAutoload() ->setMethods(array('insert', 'update', 'find', 'findAll')) ->getMock(); @@ -65,7 +65,12 @@ class UserMapperTest extends \PHPUnit_Framework_TestCase */ 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()) ->method('find') ->with(1) @@ -90,7 +95,7 @@ class UserMapperTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException InvalidArgumentException + * @expectedException \InvalidArgumentException * @expectedExceptionMessage User #404 not found */ public function testNotFound() diff --git a/DataMapper/User.php b/Structural/DataMapper/User.php similarity index 96% rename from DataMapper/User.php rename to Structural/DataMapper/User.php index 12f8779..78e60a8 100644 --- a/DataMapper/User.php +++ b/Structural/DataMapper/User.php @@ -1,6 +1,6 @@ service); // Our Renderer will now output XML instead of an array - $this->assertXmlStringEqualsXmlString('bar', $service->renderData()); + $xml = 'bar'; + $this->assertXmlStringEqualsXmlString($xml, $service->renderData()); } /** @@ -38,7 +39,9 @@ class DecoratorTest extends \PHPUnit_Framework_TestCase */ public function testDecoratorMustImplementsRenderer() { - $this->assertTrue(is_subclass_of('DesignPatterns\Decorator\Decorator', 'DesignPatterns\Decorator\RendererInterface')); + $className = 'DesignPatterns\Structural\Decorator\Decorator'; + $interfaceName = 'DesignPatterns\Structural\Decorator\RendererInterface'; + $this->assertTrue(is_subclass_of($className, $interfaceName)); } /** @@ -48,7 +51,7 @@ class DecoratorTest extends \PHPUnit_Framework_TestCase */ public function testDecoratorTypeHinted() { - $this->getMockForAbstractClass('DesignPatterns\Decorator\Decorator', array(new \stdClass())); + $this->getMockForAbstractClass('DesignPatterns\Structural\Decorator\Decorator', array(new \stdClass())); } /** @@ -56,8 +59,8 @@ class DecoratorTest extends \PHPUnit_Framework_TestCase */ public function testDecoratorOnlyAcceptRenderer() { - $mock = $this->getMock('DesignPatterns\Decorator\RendererInterface'); - $dec = $this->getMockForAbstractClass('DesignPatterns\Decorator\Decorator', array($mock)); + $mock = $this->getMock('DesignPatterns\Structural\Decorator\RendererInterface'); + $dec = $this->getMockForAbstractClass('DesignPatterns\Structural\Decorator\Decorator', array($mock)); $this->assertNotNull($dec); } } diff --git a/Decorator/Webservice.php b/Structural/Decorator/Webservice.php similarity index 88% rename from Decorator/Webservice.php rename to Structural/Decorator/Webservice.php index a23f42c..e6bf306 100644 --- a/Decorator/Webservice.php +++ b/Structural/Decorator/Webservice.php @@ -1,6 +1,6 @@ storage[$key])) { return $this->storage[$key]; } @@ -27,7 +28,7 @@ class ArrayConfig extends AbstractConfig implements Parameters * Set parameter * * @param string|int $key - * @param mixed $value + * @param mixed $value */ public function set($key, $value) { diff --git a/DependencyInjection/Connection.php b/Structural/DependencyInjection/Connection.php similarity index 93% rename from DependencyInjection/Connection.php rename to Structural/DependencyInjection/Connection.php index 573de0a..9e131c2 100644 --- a/DependencyInjection/Connection.php +++ b/Structural/DependencyInjection/Connection.php @@ -1,6 +1,6 @@ getMockBuilder('DesignPatterns\Facade\BiosInterface') + $bios = $this->getMockBuilder('DesignPatterns\Structural\Facade\BiosInterface') ->setMethods(array('launch', 'execute', 'waitForKeyPress')) ->disableAutoload() ->getMock(); - $operatingSys = $this->getMockBuilder('DesignPatterns\Facade\OsInterface') + $operatingSys = $this->getMockBuilder('DesignPatterns\Structural\Facade\OsInterface') ->setMethods(array('getName')) ->disableAutoload() ->getMock(); diff --git a/FluentInterface/README.md b/Structural/FluentInterface/README.md similarity index 100% rename from FluentInterface/README.md rename to Structural/FluentInterface/README.md diff --git a/FluentInterface/SQL.php b/Structural/FluentInterface/SQL.php similarity index 95% rename from FluentInterface/SQL.php rename to Structural/FluentInterface/SQL.php index 6b7d82d..3afd300 100644 --- a/FluentInterface/SQL.php +++ b/Structural/FluentInterface/SQL.php @@ -1,11 +1,11 @@ select(array('foo', 'bar')) ->from('foobar', 'f') ->where('f.bar = ?') diff --git a/Proxy/README.md b/Structural/Proxy/README.md similarity index 100% rename from Proxy/README.md rename to Structural/Proxy/README.md diff --git a/Proxy/Record.php b/Structural/Proxy/Record.php similarity index 94% rename from Proxy/Record.php rename to Structural/Proxy/Record.php index 28d861c..38481aa 100644 --- a/Proxy/Record.php +++ b/Structural/Proxy/Record.php @@ -1,6 +1,6 @@ assertInstanceOf('StdClass', $logger); + } +} diff --git a/Tests/ServiceLocator/ServiceLocatorTest.php b/Tests/ServiceLocator/ServiceLocatorTest.php deleted file mode 100644 index 8fc0670..0000000 --- a/Tests/ServiceLocator/ServiceLocatorTest.php +++ /dev/null @@ -1,79 +0,0 @@ -serviceLocator = new ServiceLocator(); - - $this->logService = new LogService(); - $this->databaseService = new DatabaseService(); - } - - public function testHasServices() - { - $this->serviceLocator->add('DesignPatterns\ServiceLocator\LogServiceInterface', $this->logService); - $this->serviceLocator->add('DesignPatterns\ServiceLocator\DatabaseServiceInterface', $this->databaseService); - - $this->assertTrue($this->serviceLocator->has('DesignPatterns\ServiceLocator\LogServiceInterface')); - $this->assertTrue($this->serviceLocator->has('DesignPatterns\ServiceLocator\DatabaseServiceInterface')); - - $this->assertFalse($this->serviceLocator->has('DesignPatterns\ServiceLocator\FakeServiceInterface')); - } - - public function testServicesWithObject() - { - $this->serviceLocator->add('DesignPatterns\ServiceLocator\LogServiceInterface', $this->logService); - $this->serviceLocator->add('DesignPatterns\ServiceLocator\DatabaseServiceInterface', $this->databaseService); - - $this->assertSame($this->logService, $this->serviceLocator->get('DesignPatterns\ServiceLocator\LogServiceInterface')); - $this->assertSame($this->databaseService, $this->serviceLocator->get('DesignPatterns\ServiceLocator\DatabaseServiceInterface')); - } - - public function testServicesWithClass() - { - $this->serviceLocator->add('DesignPatterns\ServiceLocator\LogServiceInterface', get_class($this->logService)); - $this->serviceLocator->add('DesignPatterns\ServiceLocator\DatabaseServiceInterface', get_class($this->databaseService)); - - $this->assertNotSame($this->logService, $this->serviceLocator->get('DesignPatterns\ServiceLocator\LogServiceInterface')); - $this->assertInstanceOf('DesignPatterns\ServiceLocator\LogServiceInterface', $this->serviceLocator->get('DesignPatterns\ServiceLocator\LogServiceInterface')); - - $this->assertNotSame($this->databaseService, $this->serviceLocator->get('DesignPatterns\ServiceLocator\DatabaseServiceInterface')); - $this->assertInstanceOf('DesignPatterns\ServiceLocator\DatabaseServiceInterface', $this->serviceLocator->get('DesignPatterns\ServiceLocator\DatabaseServiceInterface')); - } - - public function testServicesNotShared() - { - $this->serviceLocator->add('DesignPatterns\ServiceLocator\LogServiceInterface', $this->logService, false); - $this->serviceLocator->add('DesignPatterns\ServiceLocator\DatabaseServiceInterface', $this->databaseService, false); - - $this->assertNotSame($this->logService, $this->serviceLocator->get('DesignPatterns\ServiceLocator\LogServiceInterface')); - $this->assertInstanceOf('DesignPatterns\ServiceLocator\LogServiceInterface', $this->serviceLocator->get('DesignPatterns\ServiceLocator\LogServiceInterface')); - - $this->assertNotSame($this->databaseService, $this->serviceLocator->get('DesignPatterns\ServiceLocator\DatabaseServiceInterface')); - $this->assertInstanceOf('DesignPatterns\ServiceLocator\DatabaseServiceInterface', $this->serviceLocator->get('DesignPatterns\ServiceLocator\DatabaseServiceInterface')); - } -} - \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist index c69c7f5..f08bbb2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -3,8 +3,15 @@ - ./Tests + Behavioral/*/Tests + Creational/*/Tests + More/*/Tests + Structural/*/Tests - + + + ./vendor + +