Skip to content

Instantly share code, notes, and snippets.

Created February 8, 2015 22:27
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/1747807767260682e4a9 to your computer and use it in GitHub Desktop.
Save anonymous/1747807767260682e4a9 to your computer and use it in GitHub Desktop.
Simple Factory Test
<?php namespace Pattern\SimpleFactory;
use PHPUnit_Framework_TestCase;
class CarFactoryTest extends PHPUnit_Framework_TestCase {
private $factory;
public function setUp()
{
$this->factory = new CarFactory();
}
/**
* @covers Pattern\SimpleFactory\CarFactory::make
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage You tried to make a nonexistent car
*/
public function testShouldThrowAnExceptionWhenTryingToMakeANonexistentCar()
{
$this->factory->make('nonexistent');
}
/**
* @covers Pattern\SimpleFactory\CarFactory::make
* @uses Pattern\SimpleFactory\Car
*/
public function testShouldMakeAndReturnTheExpectedCar()
{
$car = $this->factory->make('galaxy');
$this->assertInstanceOf(Car::class, $car);
}
}
<?php namespace Pattern\SimpleFactory;
use PHPUnit_Framework_TestCase;
class CarStoreTest extends PHPUnit_Framework_TestCase {
/**
* @covers Pattern\SimpleFactory\CarStore::__construct
* @covers Pattern\SimpleFactory\CarStore::order
* @uses Pattern\SimpleFactory\CarFactory
*/
public function testShouldCallFactoriesMakeMethod()
{
$factory = $this->getMock(CarFactory::class);
$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