Skip to content

Commit

Permalink
Merge pull request #4 from facile-it/refactor/proxy-interface
Browse files Browse the repository at this point in the history
Define new Stub object for methods and relative return values
  • Loading branch information
xzhayon committed Jun 15, 2017
2 parents 29a402c + 3f3521b commit dad3342
Show file tree
Hide file tree
Showing 21 changed files with 384 additions and 18 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -35,7 +35,8 @@
"require": {
"php": ">=7.0",
"phpunit/phpunit": "^5.7",
"psr/container": "^1.0"
"psr/container": "^1.0",
"phpcollection/phpcollection": "^0.5.0"
},
"require-dev": {
"facile-it/facile-coding-standard": "dev-master",
Expand Down
100 changes: 99 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/Moka/Exception/InvalidArgumentException.php
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace Moka\Exception;

class InvalidArgumentException extends \InvalidArgumentException
{
}
35 changes: 35 additions & 0 deletions src/Moka/Factory/StubFactory.php
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);

namespace Moka\Factory;

use Moka\Exception\InvalidArgumentException;
use Moka\Stub\Stub;
use Moka\Stub\StubSet;

/**
* Class StubFactory
* @package Moka\Factory
*/
class StubFactory
{
/**
* @param array $methodsWithValues
* @return StubSet|Stub[]
*
* @throws InvalidArgumentException
*/
public static function fromArray(array $methodsWithValues): StubSet
{
$stubSet = new StubSet();
foreach ($methodsWithValues as $methodName => $methodValue) {
try {
$stubSet->add(new Stub($methodName, $methodValue));
} catch (\Error $error) {
throw new InvalidArgumentException($error->getMessage());
}
}

return $stubSet;
}
}
16 changes: 10 additions & 6 deletions src/Moka/Proxy/Proxy.php
Expand Up @@ -3,6 +3,8 @@

namespace Moka\Proxy;

use Moka\Factory\StubFactory;
use Moka\Stub\Stub;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

/**
Expand Down Expand Up @@ -39,20 +41,22 @@ public function serve(): MockObject
*/
public function stub(array $methodsWithValues): self
{
foreach ($methodsWithValues as $methodName => $methodValue) {
$this->addMethod($methodName, $methodValue);
$stubSet = StubFactory::fromArray($methodsWithValues);
foreach ($stubSet as $stub) {
$this->addMethod($stub);
}

return $this;
}

/**
* @param string $methodName
* @param $methodValue
* @param Stub $stub
*/
protected function addMethod(string $methodName, $methodValue)
protected function addMethod(Stub $stub)
{
$partial = $this->mock->method($methodName);
$methodValue = $stub->getMethodValue();

$partial = $this->mock->method($stub->getMethodName());

if ($methodValue instanceof \Exception) {
$partial
Expand Down
45 changes: 45 additions & 0 deletions src/Moka/Stub/Stub.php
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);

namespace Moka\Stub;

class Stub
{
/**
* @var string
*/
private $methodName;

/**
* @var mixed
*/
private $methodValue;

/**
* Stub constructor.
* @param string $methodName
* @param mixed $methodValue
*/
public function __construct(string $methodName, $methodValue)
{
$this->methodName = $methodName;
$this->methodValue = $methodValue;
}


/**
* @return string
*/
public function getMethodName(): string
{
return $this->methodName;
}

/**
* @return mixed
*/
public function getMethodValue()
{
return $this->methodValue;
}
}
35 changes: 35 additions & 0 deletions src/Moka/Stub/StubSet.php
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);

namespace Moka\Stub;

use Moka\Exception\InvalidArgumentException;
use PhpCollection\Set;

/**
* Class StubSet
* @package Moka\Stub
*/
final class StubSet extends Set
{
/**
* @param Stub $elem
* @return void
*
* @throws InvalidArgumentException
*/
public function add($elem)
{
if (!$elem instanceof Stub) {
throw new InvalidArgumentException(
sprintf(
'The first parameter must be an instance of %s, %s given',
Stub::class,
gettype($elem)
)
);
}

parent::add($elem);
}
}
4 changes: 3 additions & 1 deletion tests/Builder/ProxyBuilderSelfTest.php
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Tests\Builder;

Expand All @@ -7,9 +8,10 @@
use Moka\Proxy\Proxy;
use Moka\Traits\MokaCleanerTrait;
use Moka\Traits\MokaTrait;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_Generator as MockGenerator;

class ProxyBuilderSelfTest extends \PHPUnit_Framework_TestCase
class ProxyBuilderSelfTest extends TestCase
{
use MokaTrait;
use MokaCleanerTrait;
Expand Down
4 changes: 3 additions & 1 deletion tests/Builder/ProxyBuilderTest.php
@@ -1,14 +1,16 @@
<?php
declare(strict_types=1);

namespace Tests\Builder;

use Moka\Builder\ProxyBuilder;
use Moka\Exception\MockNotCreatedException;
use Moka\Proxy\Proxy;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_Generator as MockGenerator;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

class ProxyBuilderTest extends \PHPUnit_Framework_TestCase
class ProxyBuilderTest extends TestCase
{
/**
* @var ProxyBuilder
Expand Down
4 changes: 3 additions & 1 deletion tests/Factory/ProxyBuilderFactoryTest.php
@@ -1,11 +1,13 @@
<?php
declare(strict_types=1);

namespace Tests\Factory;

use Moka\Builder\ProxyBuilder;
use Moka\Factory\ProxyBuilderFactory;
use PHPUnit\Framework\TestCase;

class ProxyBuilderFactoryTest extends \PHPUnit_Framework_TestCase
class ProxyBuilderFactoryTest extends TestCase
{
public function testGet()
{
Expand Down
4 changes: 3 additions & 1 deletion tests/Factory/ProxyFactorySelfTest.php
@@ -1,13 +1,15 @@
<?php
declare(strict_types=1);

namespace Tests\Factory;

use Moka\Factory\ProxyFactory;
use Moka\Proxy\Proxy;
use Moka\Traits\MokaCleanerTrait;
use Moka\Traits\MokaTrait;
use PHPUnit\Framework\TestCase;

class ProxyFactorySelfTest extends \PHPUnit_Framework_TestCase
class ProxyFactorySelfTest extends TestCase
{
use MokaTrait;
use MokaCleanerTrait;
Expand Down
4 changes: 3 additions & 1 deletion tests/Factory/ProxyFactoryTest.php
@@ -1,11 +1,13 @@
<?php
declare(strict_types=1);

namespace Tests\Factory;

use Moka\Factory\ProxyFactory;
use Moka\Proxy\Proxy;
use PHPUnit\Framework\TestCase;

class ProxyFactoryTest extends \PHPUnit_Framework_TestCase
class ProxyFactoryTest extends TestCase
{
private $mock;

Expand Down

0 comments on commit dad3342

Please sign in to comment.