Skip to content

Commit

Permalink
Add a fluent interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dancras committed Sep 2, 2012
1 parent 1d1e980 commit dde9ad1
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 36 deletions.
28 changes: 14 additions & 14 deletions src/Doubles/Core/FailureException.php
@@ -1,14 +1,14 @@
<?php
/* Copyright (c) 2011, Daniel Howlett. All rights reserved.
* Released under a new BSD license.
* https://github.com/dancras/Doubles/blob/master/LICENSE */

namespace Doubles\Core;

/**
* Use this exception to indicate that the current test has failed.
*/
class FailureException extends DoublesException
{
}
<?php
/* Copyright (c) 2011, Daniel Howlett. All rights reserved.
* Released under a new BSD license.
* https://github.com/dancras/Doubles/blob/master/LICENSE */

namespace Doubles\Core;

/**
* Use this exception to indicate that the current test has failed.
*/
class FailureException extends DoublesException
{
}

15 changes: 15 additions & 0 deletions src/Doubles/Core/FluentInterface.php
@@ -0,0 +1,15 @@
<?php
/* Copyright (c) 2011, Daniel Howlett. All rights reserved.
* Released under a new BSD license.
* https://github.com/dancras/Doubles/blob/master/LICENSE */

namespace Doubles\Core;

/**
* Used to indicate to the generated test double that a fluent interface should
* be returned.
*/
class FluentInterface
{
}

22 changes: 15 additions & 7 deletions src/Doubles/Core/TestDouble.php
Expand Up @@ -6,6 +6,7 @@
namespace Doubles\Core;

use \SplStack;
use Doubles\Core\FluentInterface;

/**
* Acts as a bridge between the generated class and the functionality that
Expand Down Expand Up @@ -63,7 +64,14 @@ public function whenUndefinedMethodCalled($methodName, $arguments)
{
foreach ($this->components as $component) {
if (method_exists($component, $methodName)) {
return call_user_func_array(array($component, $methodName), $arguments);

$return = call_user_func_array(array($component, $methodName), $arguments);

if ($return !== null) {
return $return;
}

return new FluentInterface;
}
}

Expand All @@ -82,12 +90,12 @@ public function addComponent(IComponent $component)
$this->components->push($component);
}

/**
* Set to true when the subject class or interface of this test double
* has not been implemented yet.
*
* @param boolean $setting
*/
/**
* Set to true when the subject class or interface of this test double
* has not been implemented yet.
*
* @param boolean $setting
*/
public function subjectIsUndefined($setting)
{
$this->isSubjectUndefined = $setting;
Expand Down
7 changes: 6 additions & 1 deletion src/Doubles/Core/TestDoubleFactory.php
Expand Up @@ -15,8 +15,13 @@ class TestDoubleFactory
public function __call($methodName, $arguments)
{
return $this->testDouble->whenUndefinedMethodCalled($methodName, $arguments);
$return = $this->testDouble->whenUndefinedMethodCalled($methodName, $arguments);
if (is_a($return, "\Doubles\Core\FluentInterface")) {
return $this;
}
return $return;
}
';

Expand Down
28 changes: 14 additions & 14 deletions src/Doubles/Core/UsageException.php
@@ -1,14 +1,14 @@
<?php
/* Copyright (c) 2011, Daniel Howlett. All rights reserved.
* Released under a new BSD license.
* https://github.com/dancras/Doubles/blob/master/LICENSE */

namespace Doubles\Core;

/**
* Use this exception to indicate that functionality is being misused.
*/
class UsageException extends DoublesException
{
}
<?php
/* Copyright (c) 2011, Daniel Howlett. All rights reserved.
* Released under a new BSD license.
* https://github.com/dancras/Doubles/blob/master/LICENSE */

namespace Doubles\Core;

/**
* Use this exception to indicate that functionality is being misused.
*/
class UsageException extends DoublesException
{
}

10 changes: 10 additions & 0 deletions tests/MockTest.php
Expand Up @@ -101,5 +101,15 @@ function ($methodName, $arguments) use (&$m, &$a) {
$this->assertSame('method', $m);
$this->assertEquals(array('someArg'), $a);
}

public function testFluentInterface()
{
$double = Doubles::fromClass('\SomeClass');
$double->stub('foo', 'bar')
->stub('hello', 'world');

$this->assertSame('bar', $double->foo());
$this->assertSame('world', $double->hello());
}
}

20 changes: 20 additions & 0 deletions tests/src/Doubles/Test/DummyComponent.php
@@ -0,0 +1,20 @@
<?php
/* Copyright (c) 2011, Daniel Howlett. All rights reserved.
* Released under a new BSD license.
* https://github.com/dancras/Doubles/blob/master/LICENSE */

namespace Doubles\Test;

use Doubles\Core\IComponent;

class DummyComponent implements IComponent
{
public function whenMethodCalled($methodName, array $arguments)
{
}

public function dummy()#
{
}
}

27 changes: 27 additions & 0 deletions tests/unit/TestDoubleTest.php
@@ -0,0 +1,27 @@
<?php
/* Copyright (c) 2011, Daniel Howlett. All rights reserved.
* Released under a new BSD license.
* https://github.com/dancras/Doubles/blob/master/LICENSE */

namespace Doubles;

use PHPUnit_Framework_TestCase;
use Doubles\Core\TestDouble;
use Doubles\Test\DummyComponent;

class TestDoubleTest extends PHPUnit_Framework_TestCase
{
public function testWhenUndefinedMethodCalledReturnsGeneratedDoubleWhenComponentMethodReturnsNull()
{
$sut = new TestDouble;

$component = new DummyComponent;
$sut->addComponent($component);

$this->assertInstanceOf(
'\Doubles\Core\FluentInterface',
$sut->whenUndefinedMethodCalled('dummy', array())
);
}
}

0 comments on commit dde9ad1

Please sign in to comment.