Skip to content

Commit

Permalink
Merge pull request #31 from facile-it/feature/advanced-stub
Browse files Browse the repository at this point in the history
Remove StubFactory from Proxy
  • Loading branch information
Giuffre authored Jun 27, 2017
2 parents 06c27a1 + 2a15c34 commit bd1c8f3
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 121 deletions.
18 changes: 9 additions & 9 deletions src/Moka/Factory/ProxyBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ public static function get(MockingStrategyInterface $mockingStrategy): ProxyBuil
}

/**
* @return void
* @param MockingStrategyInterface $mockingStrategy
* @return string
*/
public static function reset()
private static function key(MockingStrategyInterface $mockingStrategy): string
{
foreach (self::$mockBuilders as $mockBuilder) {
$mockBuilder->reset();
}
return get_class($mockingStrategy);
}

/**
Expand All @@ -51,11 +50,12 @@ protected static function build(MockingStrategyInterface $mockingStrategy): Prox
}

/**
* @param MockingStrategyInterface $mockingStrategy
* @return string
* @return void
*/
private static function key(MockingStrategyInterface $mockingStrategy): string
public static function reset()
{
return get_class($mockingStrategy);
foreach (self::$mockBuilders as $mockBuilder) {
$mockBuilder->reset();
}
}
}
34 changes: 1 addition & 33 deletions src/Moka/Proxy/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
use Moka\Exception\InvalidArgumentException;
use Moka\Exception\MockNotCreatedException;
use Moka\Exception\MockNotServedException;
use Moka\Factory\StubFactory;
use Moka\Strategy\MockingStrategyInterface;
use Moka\Stub\StubSet;

/**
* Class Proxy
Expand All @@ -21,11 +19,6 @@ class Proxy
*/
private $fqcn;

/**
* @var StubSet
*/
private $stubs;

/**
* @var MockingStrategyInterface
*/
Expand All @@ -45,15 +38,6 @@ public function __construct(string $fqcn, MockingStrategyInterface $mockingStrat
{
$this->fqcn = $fqcn;
$this->mockingStrategy = $mockingStrategy;
$this->resetStubs();
}

/**
* @return void
*/
private function resetStubs()
{
$this->stubs = new StubSet();
}

/**
Expand Down Expand Up @@ -91,27 +75,11 @@ private function getMock()
*/
public function stub(array $methodsWithValues): self
{
$this->stubs->addAll(
StubFactory::fromArray($methodsWithValues)->all()
);

$this->decorateMock();
$this->mockingStrategy->decorate($this->getMock(), $methodsWithValues);

return $this;
}

/**
* @return void
*
* @throws InvalidArgumentException
* @throws MockNotCreatedException
*/
private function decorateMock()
{
$this->mockingStrategy->decorate($this->getMock(), $this->stubs);
$this->resetStubs();
}

/**
* @return object
*
Expand Down
95 changes: 47 additions & 48 deletions src/Moka/Strategy/AbstractMockingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Moka\Exception\MissingDependencyException;
use Moka\Exception\MockNotCreatedException;
use Moka\Exception\NotImplementedException;
use Moka\Factory\StubFactory;
use Moka\Stub\Stub;
use Moka\Stub\StubSet;

/**
* Class AbstractMockingStrategy
Expand Down Expand Up @@ -61,58 +61,29 @@ public function build(string $fqcn)
}
}

/**
* @param string $fqcn
* @return object
*/
abstract protected function doBuild(string $fqcn);

/**
* @param object $mock
* @param StubSet $stubs
* @param array $stubs
* @return void
*
* @throws NotImplementedException
* @throws InvalidArgumentException
*/
public function decorate($mock, StubSet $stubs)
public function decorate($mock, array $stubs)
{
$this->checkMockType($mock);

$stubs = StubFactory::fromArray($stubs);

/** @var Stub $stub */
foreach ($stubs as $stub) {
$this->doDecorate($mock, $stub);
}
}

/**
* @param object $mock
* @return object
*
* @throws NotImplementedException
* @throws InvalidArgumentException
*/
public function get($mock)
{
$this->checkMockType($mock);

return $this->doGet($mock);
}

/**
* @return string
*
* @throws NotImplementedException
*/
public function getMockType(): string
{
$this->verifyMockType();

return $this->mockType;
}

/**
* @param string $fqcn
*/
final protected function setMockType(string $fqcn)
{
$this->mockType = $fqcn;
}

/**
* @param object $mock
*
Expand All @@ -135,10 +106,16 @@ final protected function checkMockType($mock)
}

/**
* @param string $fqcn
* @return object
* @return void
*
* @throws NotImplementedException
*/
abstract protected function doBuild(string $fqcn);
private function verifyMockType()
{
if (!$this->mockType) {
throw new NotImplementedException('Mock type was not defined');
}
}

/**
* @param object $mock
Expand All @@ -147,21 +124,43 @@ abstract protected function doBuild(string $fqcn);
*/
abstract protected function doDecorate($mock, Stub $stub);

/**
* @param object $mock
* @return object
*
* @throws NotImplementedException
* @throws InvalidArgumentException
*/
public function get($mock)
{
$this->checkMockType($mock);

return $this->doGet($mock);
}

/**
* @param object $mock
* @return object
*/
abstract protected function doGet($mock);

/**
* @return void
* @return string
*
* @throws NotImplementedException
*/
private function verifyMockType()
public function getMockType(): string
{
if (!$this->mockType) {
throw new NotImplementedException('Mock type was not defined');
}
$this->verifyMockType();

return $this->mockType;
}

/**
* @param string $fqcn
*/
final protected function setMockType(string $fqcn)
{
$this->mockType = $fqcn;
}
}
7 changes: 2 additions & 5 deletions src/Moka/Strategy/MockingStrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Moka\Exception\InvalidArgumentException;
use Moka\Exception\MockNotCreatedException;
use Moka\Exception\NotImplementedException;
use Moka\Stub\StubSet;

/**
* Interface MockingStrategyInterface
Expand All @@ -24,12 +23,10 @@ public function build(string $fqcn);

/**
* @param object $mock
* @param StubSet $stubs
* @param array $stubs
* @return void
*
* @throws InvalidArgumentException
*/
public function decorate($mock, StubSet $stubs);
public function decorate($mock, array $stubs);

/**
* @param object $mock
Expand Down
26 changes: 8 additions & 18 deletions src/Moka/Tests/MokaMockingStrategyTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
namespace Moka\Tests;

use Moka\Exception\InvalidArgumentException;
use Moka\Factory\StubFactory;
use Moka\Strategy\MockingStrategyInterface;
use Moka\Stub\StubSet;
use PHPUnit\Framework\TestCase;
use Tests\AbstractTestClass;
use Tests\BarTestClass;
Expand All @@ -26,20 +24,15 @@ abstract class MokaMockingStrategyTestCase extends TestCase
protected $mock;

/**
* @var StubSet
* @var array
*/
protected $stubs;
protected $methodsWithValues;

/**
* @var string
*/
private $className;

/**
* @var array
*/
private $methodsWithValues = [];

final public function testGetMockTypeSuccess()
{
$this->assertInternalType('string', $this->strategy->getMockType());
Expand Down Expand Up @@ -68,14 +61,14 @@ final public function testDecorateFailure()
{
$this->expectException(InvalidArgumentException::class);

$this->strategy->decorate(new \stdClass(), $this->stubs);
$this->strategy->decorate(new \stdClass(), $this->methodsWithValues);
}

final public function testDecorateWrongTypeHintFailure()
{
$this->strategy->decorate($this->mock, StubFactory::fromArray([
$this->strategy->decorate($this->mock, [
'getSelf' => mt_rand()
]));
]);

$this->expectException(\TypeError::class);
$this->strategy->get($this->mock)->getSelf();
Expand All @@ -98,10 +91,10 @@ final public function testDecorateMultipleCallsSuccess()

final public function testDecorateOverriddenCallsFailure()
{
$this->strategy->decorate($this->mock, StubFactory::fromArray([
$this->strategy->decorate($this->mock, [
'getInt' => mt_rand(),
'throwException' => mt_rand()
]));
]);

$this->assertSame($this->methodsWithValues['getInt'], $this->strategy->get($this->mock)->getInt());
$this->assertSame($this->methodsWithValues['getInt'], $this->strategy->get($this->mock)->getInt());
Expand Down Expand Up @@ -158,10 +151,7 @@ protected function setUp()

$this->mock = $this->strategy->build($this->className);

// Mocking a StubSet is way too difficult.
$this->stubs = StubFactory::fromArray($this->methodsWithValues);

$this->strategy->decorate($this->mock, $this->stubs);
$this->strategy->decorate($this->mock, $this->methodsWithValues);
}

final protected function setStrategy(MockingStrategyInterface $strategy)
Expand Down
4 changes: 2 additions & 2 deletions tests/Plugin/Mockery/MockeryMockingStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public function testBuildMultipleFakeFQCNFailure()

public function testDecorateFakeMethodSuccess()
{
$this->strategy->decorate($this->mock, StubFactory::fromArray([
$this->strategy->decorate($this->mock, [
'fakeMethod' => true
]));
]);

$this->assertSame(true, $this->strategy->get($this->mock)->fakeMethod());
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Plugin/PHPUnit/PHPUnitMockingStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public function testDecorateFakeMethodFailure()
{
$this->expectException(\Exception::class);

$this->strategy->decorate($this->mock, StubFactory::fromArray([
$this->strategy->decorate($this->mock, [
'fakeMethod' => true
]));
]);
}

public function testCallMissingMethodSuccess()
Expand Down
4 changes: 2 additions & 2 deletions tests/Plugin/Phake/PhakeMockingStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public function testBuildMultipleFQCNFailure()

public function testDecorateFakeMethodFailure()
{
$this->strategy->decorate($this->mock, StubFactory::fromArray([
$this->strategy->decorate($this->mock, [
'fakeMethod' => true
]));
]);

$this->expectException(\Error::class);
$this->strategy->get($this->mock)->fakeMethod();
Expand Down
4 changes: 2 additions & 2 deletions tests/Plugin/Prophecy/ProphecyMockingStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public function testDecorateFakeMethodFailure()
{
$this->expectException(\Exception::class);

$this->strategy->decorate($this->mock, StubFactory::fromArray([
$this->strategy->decorate($this->mock, [
'fakeMethod' => true
]));
]);
}

public function testCallMissingMethodFailure()
Expand Down

0 comments on commit bd1c8f3

Please sign in to comment.