-
-
Save anonymous/d6c88f05558f08585bff to your computer and use it in GitHub Desktop.
Factory Method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Pattern\FactoryMethod; | |
use Pattern\FactoryMethod\Car\Galaxy; | |
use PHPUnit_Framework_TestCase; | |
class CarFactoryTest extends PHPUnit_Framework_TestCase { | |
private $factory; | |
public function setUp() | |
{ | |
$this->factory = $this->getMockForAbstractClass(CarFactory::class, [ | |
['galaxy' => Galaxy::class] | |
]); | |
} | |
/** | |
* @covers Pattern\FactoryMethod\CarFactory::__construct | |
* @covers Pattern\FactoryMethod\CarFactory::make | |
* @expectedException \InvalidArgumentException | |
* @expectedExceptionMessage You tried to make a nonexistent car | |
*/ | |
public function testShouldThrowAnExceptionWhenTryingToMakeANonexistentCar() | |
{ | |
$this->factory->make('nonexistent'); | |
} | |
/** | |
* @covers Pattern\FactoryMethod\CarFactory::__construct | |
* @covers Pattern\FactoryMethod\CarFactory::make | |
* @uses Pattern\FactoryMethod\Car | |
*/ | |
public function testShouldMakeAndReturnTheExpectedCar() | |
{ | |
$car = $this->factory->make('galaxy'); | |
$this->assertInstanceOf(Car::class, $car); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Pattern\FactoryMethod; | |
use PHPUnit_Framework_TestCase; | |
class CarStoreTest extends PHPUnit_Framework_TestCase { | |
/** | |
* @covers Pattern\FactoryMethod\CarStore::__construct | |
* @covers Pattern\FactoryMethod\CarStore::order | |
* @uses Pattern\FactoryMethod\CarFactory | |
*/ | |
public function testShouldCallFactoriesMakeMethod() | |
{ | |
$factory = $this->getMockBuilder(CarFactory::class) | |
->setConstructorArgs([[]]) | |
->setMethods(['make']) | |
->getMockForAbstractClass(); | |
$factory->expects($this->once())->method('make')->with('galaxy'); | |
$store = new CarStore($factory); | |
$store->order('galaxy'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment