the REAL factory method pattern

This commit is contained in:
Trismegiste
2013-05-10 21:09:55 +02:00
parent 9b7330795d
commit cc765bde41
8 changed files with 235 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\Tests\FactoryMethod;
use DesignPatterns\FactoryMethod;
/**
* FactoryMethodTest tests the factory method pattern
*/
class FactoryMethodTest extends \PHPUnit_Framework_TestCase
{
protected $type = array('cheap', 'fast');
public function getShop()
{
return array(
array(new FactoryMethod\GermanFactory()),
array(new FactoryMethod\ItalianFactory())
);
}
/**
* @dataProvider getShop
*/
public function testCreation(FactoryMethod\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\FactoryMethod\Vehicle', $vehicle);
}
}
/**
* @dataProvider getShop
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage spaceship is not a valid vehicle
*/
public function testUnknownType(FactoryMethod\FactoryMethod $shop)
{
$shop->create('spaceship');
}
}