├── LICENSE ├── README.md ├── composer.json ├── src └── Whizark │ ├── DesignPatterns │ ├── DependencyInjection │ │ ├── ConstructorInjection │ │ │ ├── Dependency.php │ │ │ ├── DependencyInterface.php │ │ │ ├── Dependent.php │ │ │ └── Injector.php │ │ ├── InterfaceInjection │ │ │ ├── Dependency.php │ │ │ ├── DependencyAwareInterface.php │ │ │ ├── DependencyInterface.php │ │ │ ├── Dependent.php │ │ │ └── Injector.php │ │ ├── SetterInjection │ │ │ ├── Dependency.php │ │ │ ├── DependencyInterface.php │ │ │ ├── Dependent.php │ │ │ └── Injector.php │ │ └── TraitInjection │ │ │ ├── Dependency.php │ │ │ ├── DependencyAwareTrait.php │ │ │ ├── DependencyInterface.php │ │ │ ├── Dependent.php │ │ │ └── Injector.php │ ├── GoF │ │ ├── Behavioral │ │ │ ├── Strategy │ │ │ │ ├── Client.php │ │ │ │ ├── Context.php │ │ │ │ ├── StrategyA.php │ │ │ │ ├── StrategyB.php │ │ │ │ └── StrategyInterface.php │ │ │ └── TemplateMethod │ │ │ │ ├── AbstractClass.php │ │ │ │ ├── Client.php │ │ │ │ └── ConcreteClass.php │ │ ├── Creational │ │ │ ├── FactoryMethod │ │ │ │ ├── Client.php │ │ │ │ ├── Creator.php │ │ │ │ ├── CreatorInterface.php │ │ │ │ ├── Product.php │ │ │ │ └── ProductInterface.php │ │ │ ├── Prototype │ │ │ │ ├── Client.php │ │ │ │ ├── Prototype.php │ │ │ │ └── PrototypeInterface.php │ │ │ └── Singleton │ │ │ │ ├── Client.php │ │ │ │ └── Singleton.php │ │ └── Structural │ │ │ ├── Adapter │ │ │ ├── ClassAdapter │ │ │ │ ├── Adaptee.php │ │ │ │ ├── Adapter.php │ │ │ │ ├── Client.php │ │ │ │ └── TargetInterface.php │ │ │ └── ObjectAdapter │ │ │ │ ├── AdapteeA.php │ │ │ │ ├── AdapteeB.php │ │ │ │ ├── Adapter.php │ │ │ │ ├── Client.php │ │ │ │ └── TargetInterface.php │ │ │ ├── Bridge │ │ │ ├── Abstraction.php │ │ │ ├── Client.php │ │ │ ├── ConcreteImplementorA.php │ │ │ ├── ConcreteImplementorB.php │ │ │ ├── ImplementorInterface.php │ │ │ ├── RefinedAbstractionA.php │ │ │ └── RefinedAbstractionB.php │ │ │ ├── Composite │ │ │ ├── Client.php │ │ │ ├── ComponentInterface.php │ │ │ ├── Composite.php │ │ │ └── Leaf.php │ │ │ ├── Decorator │ │ │ ├── Client.php │ │ │ ├── ComponentInterface.php │ │ │ ├── ConcreteComponent.php │ │ │ ├── ConcreteDecoratorA.php │ │ │ ├── ConcreteDecoratorB.php │ │ │ └── Decorator.php │ │ │ └── Facade │ │ │ ├── ClassA.php │ │ │ ├── ClassB.php │ │ │ ├── ClassC.php │ │ │ ├── Client.php │ │ │ └── Facade.php │ └── Uncategorized │ │ ├── HookOperation │ │ ├── Client.php │ │ ├── Hook.php │ │ ├── HookInterface.php │ │ └── HookedClass.php │ │ ├── MethodChaining │ │ ├── ChainableClass.php │ │ └── Client.php │ │ └── NullObject │ │ ├── Client.php │ │ ├── Delegator.php │ │ ├── NullOperation.php │ │ ├── OperationInterface.php │ │ └── RealOperation.php │ └── Uncategorized │ ├── AutomaticPartialApplication │ └── AutomaticPartialApplication.php │ ├── Currying │ └── Currying.php │ ├── Memoization │ └── Memoization.php │ └── PartialApplication │ └── PartialApplication.php └── tests ├── Tests └── Whizark │ └── DesignPatterns │ ├── DependencyInjection │ ├── ConstructorInjection │ │ ├── DependencyTest.php │ │ ├── DependentTest.php │ │ └── InjectorTest.php │ ├── InterfaceInjection │ │ ├── DependencyTest.php │ │ ├── DependentTest.php │ │ └── InjectorTest.php │ ├── SetterInjection │ │ ├── DependencyTest.php │ │ ├── DependentTest.php │ │ └── InjectorTest.php │ └── TraitInjection │ │ ├── DependencyTest.php │ │ ├── DependentTest.php │ │ └── InjectorTest.php │ ├── GoF │ ├── Behavioral │ │ ├── Strategy │ │ │ ├── ClientTest.php │ │ │ ├── ContextTest.php │ │ │ ├── StrategyATest.php │ │ │ └── StrategyBTest.php │ │ └── TemplateMethod │ │ │ ├── AbstractClassTest.php │ │ │ ├── ClientTest.php │ │ │ └── ConcreteClassTest.php │ ├── Creational │ │ ├── FactoryMethod │ │ │ ├── ClientTest.php │ │ │ ├── CreatorTest.php │ │ │ └── ProductTest.php │ │ ├── Prototype │ │ │ ├── ClientTest.php │ │ │ └── PrototypeTest.php │ │ └── Singleton │ │ │ ├── ClientTest.php │ │ │ └── SingletonTest.php │ └── Structural │ │ ├── Bridge │ │ ├── ClientTest.php │ │ ├── ConcreteImplementorATest.php │ │ ├── ConcreteImplementorBTest.php │ │ ├── RefinedAbstractionATest.php │ │ └── RefinedAbstractionBTest.php │ │ ├── Composite │ │ ├── ClientTest.php │ │ ├── CompositeTest.php │ │ └── LeafTest.php │ │ ├── Decorator │ │ ├── ClientTest.php │ │ ├── ConcreteComponentTest.php │ │ ├── ConcreteDecoratorATest.php │ │ └── ConcreteDecoratorBTest.php │ │ └── Facade │ │ ├── ClassATest.php │ │ ├── ClassBTest.php │ │ ├── ClassCTest.php │ │ ├── ClientTest.php │ │ └── FacadeTest.php │ └── Uncategorized │ ├── HookOperation │ ├── ClientTest.php │ ├── HookTest.php │ └── HookedClassTest.php │ ├── MethodChaining │ ├── ChainableClassTest.php │ └── ClientTest.php │ └── NullObject │ ├── ClientTest.php │ ├── DelegatorTest.php │ ├── NullOperationTest.php │ └── RealOperationTest.php └── bootstrap.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Whizark 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PHP Patterns 2 | ============ 3 | 4 | A PHP 5.4+ pattern collection that covers design patterns, anti-patterns, closure patterns, refactoring patterns. 5 | 6 | [![Build Status](https://travis-ci.org/whizark/php-patterns.png?branch=master)](https://travis-ci.org/whizark/php-patterns) [![Coverage Status](https://coveralls.io/repos/whizark/php-patterns/badge.png)](https://coveralls.io/r/whizark/php-patterns) [![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/whizark/php-patterns/badges/quality-score.png?s=32a95feb6ea708c584efcbdbdd9000908ce9845b)](https://scrutinizer-ci.com/g/whizark/php-patterns/) 7 | 8 | The Concepts 9 | ------------ 10 | 11 | * **Minimal and Conceptual** (useful as a pattern catalog) 12 | * **Practical and Modern** (following [PSR-2](http://www.php-fig.org/psr/2/) as possible etc.) 13 | * **PHP-specific** (with built-in features and SPL etc.) 14 | * **Interface-oriented** (but as simple as possible) 15 | * **Testable** (including test cases as examples) 16 | 17 | (Since focusing on the core concept of each pattern, some patterns might not be the same as the originals.) 18 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "whizark/php-patterns", 3 | "description": "A minimal and conceptual PHP 5.4+ pattern collection.", 4 | "homepage": "https://github.com/whizark/php-patterns", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Whizark", 9 | "homepage": "https://github.com/whizark" 10 | } 11 | ], 12 | "support": { 13 | "issues": "https://github.com/whizark/php-patterns/issues", 14 | "source": "https://github.com/whizark/php-patterns" 15 | }, 16 | "autoload": { 17 | "psr-0": { 18 | "Whizark": "src/" 19 | } 20 | }, 21 | "require-dev": { 22 | "phpunit/phpunit": "3.7.*", 23 | "mockery/mockery": "0.8.*", 24 | "satooshi/php-coveralls": "dev-master" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/DependencyInjection/ConstructorInjection/Dependency.php: -------------------------------------------------------------------------------- 1 | dependency = $dependency; 23 | } 24 | 25 | /** 26 | * Do something with the dependency. 27 | */ 28 | public function doSomething() 29 | { 30 | $this->dependency->doSomething(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/DependencyInjection/ConstructorInjection/Injector.php: -------------------------------------------------------------------------------- 1 | doSomething(); 11 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/DependencyInjection/InterfaceInjection/Dependency.php: -------------------------------------------------------------------------------- 1 | dependency = $dependency; 23 | } 24 | 25 | /** 26 | * Do something with the dependency. 27 | */ 28 | public function doSomething() 29 | { 30 | $this->dependency->doSomething(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/DependencyInjection/InterfaceInjection/Injector.php: -------------------------------------------------------------------------------- 1 | setDependency(new Dependency()); 11 | $dependent->doSomething(); 12 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/DependencyInjection/SetterInjection/Dependency.php: -------------------------------------------------------------------------------- 1 | dependency = $dependency; 25 | } 26 | 27 | /** 28 | * Do something with the dependency. 29 | */ 30 | public function doSomething() 31 | { 32 | $this->dependency->doSomething(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/DependencyInjection/SetterInjection/Injector.php: -------------------------------------------------------------------------------- 1 | setDependency(new Dependency()); 11 | $dependent->doSomething(); 12 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/DependencyInjection/TraitInjection/Dependency.php: -------------------------------------------------------------------------------- 1 | dependency = $dependency; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/DependencyInjection/TraitInjection/DependencyInterface.php: -------------------------------------------------------------------------------- 1 | dependency->doSomething(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/DependencyInjection/TraitInjection/Injector.php: -------------------------------------------------------------------------------- 1 | setDependency(new Dependency()); 11 | $dependent->doSomething(); 12 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Behavioral/Strategy/Client.php: -------------------------------------------------------------------------------- 1 | doSomething(); 12 | 13 | $contextB = new Context(new StrategyB()); 14 | $contextB->doSomething(); 15 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Behavioral/Strategy/Context.php: -------------------------------------------------------------------------------- 1 | strategy = $strategy; 23 | } 24 | 25 | /** 26 | * Do something with the Strategy. 27 | */ 28 | public function doSomething() 29 | { 30 | $this->strategy->doSomething(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Behavioral/Strategy/StrategyA.php: -------------------------------------------------------------------------------- 1 | doSomethingA(); 30 | 31 | // Do something if needed. 32 | 33 | $this->doSomethingB(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Behavioral/TemplateMethod/Client.php: -------------------------------------------------------------------------------- 1 | templateMethod(); 10 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Behavioral/TemplateMethod/ConcreteClass.php: -------------------------------------------------------------------------------- 1 | factoryMethod(); 10 | $product->doSomething(); 11 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Creational/FactoryMethod/Creator.php: -------------------------------------------------------------------------------- 1 | $value) { 25 | $type = gettype($value); 26 | 27 | if ($type === 'object' || $type === 'array') { 28 | $this->{$key} = unserialize(serialize($value)); 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Creational/Prototype/PrototypeInterface.php: -------------------------------------------------------------------------------- 1 | adapteeMethod(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Structural/Adapter/ClassAdapter/Client.php: -------------------------------------------------------------------------------- 1 | targetMethod(); 10 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Structural/Adapter/ClassAdapter/TargetInterface.php: -------------------------------------------------------------------------------- 1 | adapteeA = $a; 33 | $this->adapteeB = $b; 34 | } 35 | 36 | /** 37 | * {@inheritDoc} 38 | */ 39 | public function targetMethod() 40 | { 41 | // Adapt adaptee method(s) to the target interface. 42 | $this->adapteeA->adapteeMethod(); 43 | $this->adapteeB->adapteeMethod(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Structural/Adapter/ObjectAdapter/Client.php: -------------------------------------------------------------------------------- 1 | targetMethod(); 10 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Structural/Adapter/ObjectAdapter/TargetInterface.php: -------------------------------------------------------------------------------- 1 | implementator = $implementator; 23 | } 24 | 25 | /** 26 | * Do something with the implementor. 27 | */ 28 | abstract public function doSomething(); 29 | } 30 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Structural/Bridge/Client.php: -------------------------------------------------------------------------------- 1 | doSomething(); 13 | 14 | $abstractionAWithImplementorB = new RefinedAbstractionA(new ConcreteImplementorB()); 15 | $abstractionAWithImplementorB->doSomething(); 16 | 17 | $abstractionBWithImplementorA = new RefinedAbstractionB(new ConcreteImplementorA()); 18 | $abstractionBWithImplementorA->doSomething(); 19 | 20 | $abstractionBWithImplementorB = new RefinedAbstractionB(new ConcreteImplementorB()); 21 | $abstractionBWithImplementorB->doSomething(); 22 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Structural/Bridge/ConcreteImplementorA.php: -------------------------------------------------------------------------------- 1 | implementator->implementation(); 21 | 22 | // Do class-specific something. 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Structural/Bridge/RefinedAbstractionB.php: -------------------------------------------------------------------------------- 1 | implementator->implementation(); 21 | 22 | // Do class-specific something. 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Structural/Composite/Client.php: -------------------------------------------------------------------------------- 1 | add($grandchildLeaf); 15 | $root->add($childLeaf); 16 | $root->add($childComposite); 17 | 18 | $root->doSomething(); 19 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Structural/Composite/ComponentInterface.php: -------------------------------------------------------------------------------- 1 | children = new \SplObjectStorage(); 23 | } 24 | 25 | /** 26 | * Add a child Component. 27 | * 28 | * @param ComponentInterface $component 29 | */ 30 | public function add(ComponentInterface $component) 31 | { 32 | $this->children->attach($component); 33 | } 34 | 35 | /** 36 | * Remove a child Component. 37 | * 38 | * @param ComponentInterface $component 39 | */ 40 | public function remove(ComponentInterface $component) 41 | { 42 | $this->children->detach($component); 43 | } 44 | 45 | /** 46 | * {inheritDoc} 47 | */ 48 | public function doSomething() 49 | { 50 | // Do something. 51 | // 52 | // For instance, recursively do something for all the children. 53 | foreach ($this->children as $child) { 54 | $child->doSomething(); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Structural/Composite/Leaf.php: -------------------------------------------------------------------------------- 1 | doSomething(); 10 | 11 | $decoratedComponentB = new ConcreteDecoratorB(new ConcreteComponent()); 12 | $decoratedComponentB->doSomething(); 13 | 14 | $decoratedComponentC = new ConcreteDecoratorA(new ConcreteDecoratorB(new ConcreteComponent())); 15 | $decoratedComponentC->doSomething(); 16 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Structural/Decorator/ComponentInterface.php: -------------------------------------------------------------------------------- 1 | component->doSomething(); 21 | 22 | // Do class-specific something 23 | // to attach additional responsibilities to the original Component. 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Structural/Decorator/ConcreteDecoratorB.php: -------------------------------------------------------------------------------- 1 | component->doSomething(); 21 | 22 | // Do class-specific something 23 | // to attach additional responsibilities to the original Component. 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Structural/Decorator/Decorator.php: -------------------------------------------------------------------------------- 1 | component = $component; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Structural/Facade/ClassA.php: -------------------------------------------------------------------------------- 1 | doSomething(); 10 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/GoF/Structural/Facade/Facade.php: -------------------------------------------------------------------------------- 1 | classA = new ClassA(); 33 | $this->classB = new ClassB(); 34 | $this->classC = new ClassC(); 35 | } 36 | 37 | /** 38 | * A simplified interface to do something with the underlying object(s). 39 | */ 40 | public function doSomething() 41 | { 42 | $this->classA->doSomething(); 43 | $this->classB->doSomething(); 44 | $this->classC->doSomething(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/Uncategorized/HookOperation/Client.php: -------------------------------------------------------------------------------- 1 | doSomething(); 11 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/Uncategorized/HookOperation/Hook.php: -------------------------------------------------------------------------------- 1 | hook = $hook; 23 | } 24 | 25 | /** 26 | * Do something with Hook Operation(s). 27 | */ 28 | public function doSomething() 29 | { 30 | $this->hook->hookOperationA(); 31 | 32 | // Do something. 33 | 34 | $this->hook->hookOperationB(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/Uncategorized/MethodChaining/ChainableClass.php: -------------------------------------------------------------------------------- 1 | propetyA = (int) $arg; 37 | 38 | return $this; 39 | } 40 | 41 | /** 42 | * Do something and return this object. 43 | * 44 | * @param $arg 45 | * @return $this 46 | */ 47 | public function doSomethingB($arg) 48 | { 49 | // Do something. 50 | // 51 | // For instance, set a property value. 52 | $this->propetyB = (int) $arg; 53 | 54 | return $this; 55 | } 56 | 57 | /** 58 | * Do something and return this object. 59 | * 60 | * @param $arg 61 | * @return $this 62 | */ 63 | public function doSomethingC($arg) 64 | { 65 | // Do something. 66 | // 67 | // For instance, set a property value. 68 | $this->propetyC = (int) $arg; 69 | 70 | return $this; 71 | } 72 | 73 | /** 74 | * Do something at the end of a chain. 75 | * 76 | * @return int 77 | */ 78 | public function execute() 79 | { 80 | // Do something. 81 | // 82 | // For instance, calculate and return the sum of the propety values. 83 | return (int) $this->propetyA + 84 | (int) $this->propetyB + 85 | (int) $this->propetyC; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/Uncategorized/MethodChaining/Client.php: -------------------------------------------------------------------------------- 1 | doSomethingA(1) 9 | ->doSomethingB(2) 10 | ->doSomethingC(3) 11 | ->execute(); 12 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/Uncategorized/NullObject/Client.php: -------------------------------------------------------------------------------- 1 | doSomething(); 14 | $delegatorB->doSomething(); // Actually do nothing, or do something with no side effects. 15 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/Uncategorized/NullObject/Delegator.php: -------------------------------------------------------------------------------- 1 | operation = $operation; 23 | } 24 | 25 | /** 26 | * Do something with the dependency. 27 | */ 28 | public function doSomething() 29 | { 30 | $this->operation->request(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Whizark/DesignPatterns/Uncategorized/NullObject/NullOperation.php: -------------------------------------------------------------------------------- 1 | doSomething(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/DependencyInjection/ConstructorInjection/DependentTest.php: -------------------------------------------------------------------------------- 1 | shouldReceive('doSomething') 27 | ->once(); 28 | 29 | $dependent = new Dependent($mockedDependency); 30 | $dependent->doSomething(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/DependencyInjection/ConstructorInjection/InjectorTest.php: -------------------------------------------------------------------------------- 1 | doSomething(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/DependencyInjection/InterfaceInjection/DependencyTest.php: -------------------------------------------------------------------------------- 1 | doSomething(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/DependencyInjection/InterfaceInjection/DependentTest.php: -------------------------------------------------------------------------------- 1 | shouldReceive('doSomething') 27 | ->once(); 28 | 29 | $dependent = new Dependent(); 30 | $dependent->setDependency($mockedDependency); 31 | $dependent->doSomething(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/DependencyInjection/InterfaceInjection/InjectorTest.php: -------------------------------------------------------------------------------- 1 | setDependency(new Dependency()); 20 | $dependent->doSomething(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/DependencyInjection/SetterInjection/DependencyTest.php: -------------------------------------------------------------------------------- 1 | doSomething(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/DependencyInjection/SetterInjection/DependentTest.php: -------------------------------------------------------------------------------- 1 | shouldReceive('doSomething') 27 | ->once(); 28 | 29 | $dependent = new Dependent(); 30 | $dependent->setDependency($mockedDependency); 31 | $dependent->doSomething(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/DependencyInjection/SetterInjection/InjectorTest.php: -------------------------------------------------------------------------------- 1 | setDependency(new Dependency()); 20 | $dependent->doSomething(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/DependencyInjection/TraitInjection/DependencyTest.php: -------------------------------------------------------------------------------- 1 | doSomething(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/DependencyInjection/TraitInjection/DependentTest.php: -------------------------------------------------------------------------------- 1 | shouldReceive('doSomething') 27 | ->once(); 28 | 29 | $dependent = new Dependent(); 30 | $dependent->setDependency($mockedDependency); 31 | $dependent->doSomething(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/DependencyInjection/TraitInjection/InjectorTest.php: -------------------------------------------------------------------------------- 1 | setDependency(new Dependency()); 20 | $dependent->doSomething(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Behavioral/Strategy/ClientTest.php: -------------------------------------------------------------------------------- 1 | doSomething(); 21 | } 22 | 23 | /** 24 | * Test an use case. 25 | */ 26 | public function testAnUseCaseB() 27 | { 28 | $contextB = new Context(new StrategyB()); 29 | $contextB->doSomething(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Behavioral/Strategy/ContextTest.php: -------------------------------------------------------------------------------- 1 | shouldReceive('doSomething') 28 | ->once(); 29 | 30 | $context = new Context($mockedStrategy); 31 | $context->doSomething(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Behavioral/Strategy/StrategyATest.php: -------------------------------------------------------------------------------- 1 | doSomething(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Behavioral/Strategy/StrategyBTest.php: -------------------------------------------------------------------------------- 1 | doSomething(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Behavioral/TemplateMethod/AbstractClassTest.php: -------------------------------------------------------------------------------- 1 | templateMethod(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Behavioral/TemplateMethod/ClientTest.php: -------------------------------------------------------------------------------- 1 | templateMethod(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Behavioral/TemplateMethod/ConcreteClassTest.php: -------------------------------------------------------------------------------- 1 | templateMethod(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Creational/FactoryMethod/ClientTest.php: -------------------------------------------------------------------------------- 1 | factoryMethod(); 19 | $product->doSomething(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Creational/FactoryMethod/CreatorTest.php: -------------------------------------------------------------------------------- 1 | factoryMethod(); 19 | $this->assertInstanceOf('Whizark\DesignPatterns\GoF\Creational\FactoryMethod\ProductInterface', $product); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Creational/FactoryMethod/ProductTest.php: -------------------------------------------------------------------------------- 1 | doSomething(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Creational/Prototype/ClientTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf('Whizark\DesignPatterns\GoF\Creational\Prototype\Prototype', $clonedPrototype); 22 | } 23 | 24 | /** 25 | * Test cloning. 26 | * The cloned object should be different from the prototype. 27 | */ 28 | public function testInstanceDifference() 29 | { 30 | $prototype = new Prototype(); 31 | $clonedPrototype = clone $prototype; 32 | 33 | $this->assertNotSame($prototype, $clonedPrototype); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Creational/Singleton/ClientTest.php: -------------------------------------------------------------------------------- 1 | assertFalse($reflectionClass->IsInstantiable()); 19 | } 20 | 21 | /** 22 | * Test getInstance(), 23 | * which should create an object & return the new instance at the first call. 24 | */ 25 | public function testInstantiation() 26 | { 27 | $instance = Singleton::getInstance(); 28 | 29 | $this->assertInstanceOf('Whizark\DesignPatterns\GoF\Creational\Singleton\Singleton', $instance); 30 | } 31 | 32 | /** 33 | * Test getInstance(), 34 | * which should return the same instance if an object has already been instantiated. 35 | */ 36 | public function testInstanceIdentity() 37 | { 38 | $instanceA = Singleton::getInstance(); 39 | $instanceB = Singleton::getInstance(); 40 | 41 | $this->assertSame($instanceA, $instanceB); 42 | } 43 | 44 | /** 45 | * Test cloning, which should not be allowed. 46 | * 47 | * @expectedException \RuntimeException 48 | */ 49 | public function testCloning() 50 | { 51 | $instance = Singleton::getInstance(); 52 | $clonedInstance = clone $instance; 53 | } 54 | 55 | /** 56 | * Test unserialization, which should not be allowd. 57 | * 58 | * @expectedException \RuntimeException 59 | */ 60 | public function testUnserialization() 61 | { 62 | $instance = Singleton::getInstance(); 63 | $serializedInstance = serialize($instance); 64 | $unserializedInstance = unserialize($serializedInstance); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Structural/Bridge/ClientTest.php: -------------------------------------------------------------------------------- 1 | doSomething(); 22 | } 23 | 24 | /** 25 | * Test an use case. 26 | */ 27 | public function testAnUseCaseB() 28 | { 29 | $abstraction = new RefinedAbstractionA(new ConcreteImplementorB()); 30 | $abstraction->doSomething(); 31 | } 32 | 33 | /** 34 | * Test an use case. 35 | */ 36 | public function testAnUseCaseC() 37 | { 38 | $abstraction = new RefinedAbstractionB(new ConcreteImplementorA()); 39 | $abstraction->doSomething(); 40 | } 41 | 42 | /** 43 | * Test an use case. 44 | */ 45 | public function testAnUseCaseD() 46 | { 47 | $abstraction = new RefinedAbstractionB(new ConcreteImplementorB()); 48 | $abstraction->doSomething(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Structural/Bridge/ConcreteImplementorATest.php: -------------------------------------------------------------------------------- 1 | implementation(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Structural/Bridge/ConcreteImplementorBTest.php: -------------------------------------------------------------------------------- 1 | implementation(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Structural/Bridge/RefinedAbstractionATest.php: -------------------------------------------------------------------------------- 1 | shouldReceive('implementation') 27 | ->once(); 28 | 29 | $abstraction = new RefinedAbstractionA($mockedImplementor); 30 | $abstraction->doSomething(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Structural/Bridge/RefinedAbstractionBTest.php: -------------------------------------------------------------------------------- 1 | shouldReceive('implementation') 27 | ->once(); 28 | 29 | $abstraction = new RefinedAbstractionB($mockedImplementor); 30 | $abstraction->doSomething(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Structural/Composite/ClientTest.php: -------------------------------------------------------------------------------- 1 | add($grandchildLeaf); 24 | $root->add($childLeaf); 25 | $root->add($childComposite); 26 | 27 | $root->doSomething(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Structural/Composite/CompositeTest.php: -------------------------------------------------------------------------------- 1 | add($mockedLeaf); 29 | } 30 | 31 | /** 32 | * Test remove(). 33 | */ 34 | public function testRemove() 35 | { 36 | $mockedLeaf = \Mockery::mock('Whizark\DesignPatterns\GoF\Structural\Composite\ComponentInterface'); 37 | 38 | $composite = new Composite(); 39 | $composite->remove($mockedLeaf); 40 | } 41 | 42 | /** 43 | * Test doSomething(). 44 | */ 45 | public function testDosomething() 46 | { 47 | $mockedLeaf = \Mockery::mock('Whizark\DesignPatterns\GoF\Structural\Composite\ComponentInterface'); 48 | $mockedLeaf->shouldReceive('doSomething') 49 | ->once(); 50 | 51 | $composite = new Composite(); 52 | $composite->add($mockedLeaf); 53 | $composite->doSomething(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Structural/Composite/LeafTest.php: -------------------------------------------------------------------------------- 1 | doSomething(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Structural/Decorator/ClientTest.php: -------------------------------------------------------------------------------- 1 | doSomething(); 21 | } 22 | 23 | /** 24 | * Test an use case. 25 | */ 26 | public function testAnUseCaseB() 27 | { 28 | $decoratedComponent = new ConcreteDecoratorB(new ConcreteComponent()); 29 | $decoratedComponent->doSomething(); 30 | } 31 | 32 | /** 33 | * Test an use case. 34 | */ 35 | public function testAnUseCaseC() 36 | { 37 | $decoratedComponent = new ConcreteDecoratorA(new ConcreteDecoratorB(new ConcreteComponent())); 38 | $decoratedComponent->doSomething(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Structural/Decorator/ConcreteComponentTest.php: -------------------------------------------------------------------------------- 1 | doSomething(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Structural/Decorator/ConcreteDecoratorATest.php: -------------------------------------------------------------------------------- 1 | shouldReceive('doSomething') 27 | ->once(); 28 | 29 | $decorator = new ConcreteDecoratorA($mockedComponent); 30 | $decorator->doSomething(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Structural/Decorator/ConcreteDecoratorBTest.php: -------------------------------------------------------------------------------- 1 | shouldReceive('doSomething') 27 | ->once(); 28 | 29 | $decorator = new ConcreteDecoratorB($mockedComponent); 30 | $decorator->doSomething(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Structural/Facade/ClassATest.php: -------------------------------------------------------------------------------- 1 | doSomething(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Structural/Facade/ClassBTest.php: -------------------------------------------------------------------------------- 1 | doSomething(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Structural/Facade/ClassCTest.php: -------------------------------------------------------------------------------- 1 | doSomething(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Structural/Facade/ClientTest.php: -------------------------------------------------------------------------------- 1 | doSomething(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/GoF/Structural/Facade/FacadeTest.php: -------------------------------------------------------------------------------- 1 | doSomething(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/Uncategorized/HookOperation/ClientTest.php: -------------------------------------------------------------------------------- 1 | doSomething(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/Uncategorized/HookOperation/HookTest.php: -------------------------------------------------------------------------------- 1 | hookOperationA(); 19 | } 20 | 21 | /** 22 | * Test hookOperationB(). 23 | */ 24 | public function testHookOperationB() 25 | { 26 | $hook = new Hook(); 27 | $hook->hookOperationB(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/Uncategorized/HookOperation/HookedClassTest.php: -------------------------------------------------------------------------------- 1 | shouldReceive('hookOperationA') 27 | ->once() 28 | ->shouldReceive('hookOperationB') 29 | ->once(); 30 | 31 | $hookedObject = new HookedClass($mockedHook); 32 | $hookedObject->doSomething(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/Uncategorized/MethodChaining/ChainableClassTest.php: -------------------------------------------------------------------------------- 1 | doSomethingA(1); 19 | $this->assertInstanceOf('Whizark\DesignPatterns\Uncategorized\MethodChaining\ChainableClass', $result); 20 | } 21 | 22 | /** 23 | * Test doSomethingB(). 24 | */ 25 | public function testDoSomethingB() 26 | { 27 | $chainableObject = new ChainableClass(); 28 | $result = $chainableObject->doSomethingB(1); 29 | $this->assertInstanceOf('Whizark\DesignPatterns\Uncategorized\MethodChaining\ChainableClass', $result); 30 | } 31 | 32 | /** 33 | * Test doSomethingC(). 34 | */ 35 | public function testDoSomethingC() 36 | { 37 | $chainableObject = new ChainableClass(); 38 | $result = $chainableObject->doSomethingC(1); 39 | $this->assertInstanceOf('Whizark\DesignPatterns\Uncategorized\MethodChaining\ChainableClass', $result); 40 | } 41 | 42 | /** 43 | * Test execute(). 44 | */ 45 | public function testExecute() 46 | { 47 | $chainableObject = new ChainableClass(); 48 | $result = $chainableObject->execute(); 49 | $this->assertSame(0, $result); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/Uncategorized/MethodChaining/ClientTest.php: -------------------------------------------------------------------------------- 1 | doSomethingA(1) 18 | ->doSomethingB(2) 19 | ->doSomethingC(3) 20 | ->execute(); 21 | $this->assertSame(6, $result); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/Uncategorized/NullObject/ClientTest.php: -------------------------------------------------------------------------------- 1 | doSomething(); 21 | } 22 | 23 | /** 24 | * Test an use case. 25 | */ 26 | public function testAnUseCaseB() 27 | { 28 | $delegator = new Delegator(new NullOperation()); 29 | $delegator->doSomething(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/Uncategorized/NullObject/DelegatorTest.php: -------------------------------------------------------------------------------- 1 | shouldReceive('request') 27 | ->once(); 28 | 29 | $delegator = new Delegator($mockedOperation); 30 | $delegator->doSomething(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/Uncategorized/NullObject/NullOperationTest.php: -------------------------------------------------------------------------------- 1 | request(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Tests/Whizark/DesignPatterns/Uncategorized/NullObject/RealOperationTest.php: -------------------------------------------------------------------------------- 1 | request(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | add('Tests', __DIR__); 14 | --------------------------------------------------------------------------------