Skip to content

Instantly share code, notes, and snippets.

Created February 8, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d6c88f05558f08585bff to your computer and use it in GitHub Desktop.
Save anonymous/d6c88f05558f08585bff to your computer and use it in GitHub Desktop.
Factory Method
<?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);
}
}
<?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