Skip to content

Instantly share code, notes, and snippets.

@awei01
Created February 26, 2014 20:51
Show Gist options
  • Select an option

  • Save awei01/9238255 to your computer and use it in GitHub Desktop.

Select an option

Save awei01/9238255 to your computer and use it in GitHub Desktop.
some fake testing code that is not dry because i'm faking methods
<?php
// class definitions
abstract class AbstractFactory {
protected function makeWidget()
{
return new Widget;
}
}
class FactoryOne extends AbstractFactory {
function doFooOnWidget()
{
$widget = $this->makeWidget();
$widget->doFoo();
}
}
class FactoryTwo extends AbstractFactory {
function doBarOnWidget()
{
$widget = $this->makeWidget();
$widget->doBar();
}
}
?>
<?php
// testing file
class FactoryOneTest extends TestCase {
function test_doFooOnWidget_NoParams_CallsDoFooOnWidgetInstance {
$factory = new FakeFactoryOne();
$factory->fakeWidget = $fakeWidget;
$fakeWidget->shouldReceive('doFoo')->once();
$factory->doFooOnWidget();
}
}
class FakeFactoryOne extends FactoryOne {
public $fakeWidget;
// how can i abstract the following function?
protected function makeWidget()
{
if ($fakeWidget)
{
return $fakeWidget;
}
return parent::makeWidget();
}
}
class FactoryTwoTest extends TestCase {
function test_doBarOnWidget_NoParams_CallsDoFooOnWidgetInstance {
$factory = new FakeFactoryTwo();
$factory->fakeWidget = $fakeWidget;
$fakeWidget->shouldReceive('doBar')->once();
$factory->doBarOnWidget();
}
}
class FakeFactoryTwo extends FactoryTwo {
public $fakeWidget;
// how can i abstract the following function?
protected function makeWidget()
{
if ($fakeWidget)
{
return $fakeWidget;
}
return parent::makeWidget();
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment