PHP7 FactoryMethod

This commit is contained in:
Dominik Liebler
2016-09-22 13:28:46 +02:00
parent 6a98bcb73b
commit 4760d898a0
11 changed files with 94 additions and 159 deletions

View File

@@ -6,44 +6,46 @@ use DesignPatterns\Creational\FactoryMethod\FactoryMethod;
use DesignPatterns\Creational\FactoryMethod\GermanFactory;
use DesignPatterns\Creational\FactoryMethod\ItalianFactory;
/**
* FactoryMethodTest tests the factory method pattern.
*/
class FactoryMethodTest extends \PHPUnit_Framework_TestCase
{
protected $type = array(
FactoryMethod::CHEAP,
FactoryMethod::FAST,
);
public function getShop()
public function testCanCreateCheapVehicleInGermany()
{
return array(
array(new GermanFactory()),
array(new ItalianFactory()),
);
$factory = new GermanFactory();
$result = $factory->create(FactoryMethod::CHEAP);
$this->assertInstanceOf('DesignPatterns\Creational\FactoryMethod\Bicycle', $result);
}
public function testCanCreateFastVehicleInGermany()
{
$factory = new GermanFactory();
$result = $factory->create(FactoryMethod::FAST);
$this->assertInstanceOf('DesignPatterns\Creational\FactoryMethod\CarMercedes', $result);
}
public function testCanCreateCheapVehicleInItaly()
{
$factory = new ItalianFactory();
$result = $factory->create(FactoryMethod::CHEAP);
$this->assertInstanceOf('DesignPatterns\Creational\FactoryMethod\Bicycle', $result);
}
public function testCanCreateFastVehicleInItaly()
{
$factory = new ItalianFactory();
$result = $factory->create(FactoryMethod::FAST);
$this->assertInstanceOf('DesignPatterns\Creational\FactoryMethod\CarFerrari', $result);
}
/**
* @dataProvider getShop
*/
public function testCreation(FactoryMethod $shop)
{
// this test method acts as a client for the factory. We don't care
// about the factory, all we know is it can produce vehicle
foreach ($this->type as $oneType) {
$vehicle = $shop->create($oneType);
$this->assertInstanceOf('DesignPatterns\Creational\FactoryMethod\VehicleInterface', $vehicle);
}
}
/**
* @dataProvider getShop
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage spaceship is not a valid vehicle
*/
public function testUnknownType(FactoryMethod $shop)
public function testUnknownType()
{
$shop->create('spaceship');
(new ItalianFactory())->create('spaceship');
}
}