Skip to content

Commit

Permalink
Merge pull request #3 from facile-it/feature/self-tests
Browse files Browse the repository at this point in the history
Add self tests to ensure Moka can test itself
  • Loading branch information
Giuffre committed Jun 14, 2017
2 parents 77dcd46 + 9205b39 commit 29a402c
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 63 deletions.
71 changes: 71 additions & 0 deletions tests/Builder/ProxyBuilderSelfTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Tests\Builder;

use Moka\Builder\ProxyBuilder;
use Moka\Exception\MockNotCreatedException;
use Moka\Proxy\Proxy;
use Moka\Traits\MokaCleanerTrait;
use Moka\Traits\MokaTrait;
use PHPUnit_Framework_MockObject_Generator as MockGenerator;

class ProxyBuilderSelfTest extends \PHPUnit_Framework_TestCase
{
use MokaTrait;
use MokaCleanerTrait;

/**
* @var ProxyBuilder
*/
private $proxyBuilder;

public function setUp()
{
$this->proxyBuilder = new ProxyBuilder(
$this->mock(MockGenerator::class)->serve()
);
}

public function testGetProxySuccess()
{
$this->decorateSuccessfulMockGenerator();

$proxy1 = $this->proxyBuilder->getProxy(\stdClass::class);
$proxy2 = $this->proxyBuilder->getProxy(\stdClass::class);

$this->assertInstanceOf(Proxy::class, $proxy1);

$this->assertSame($proxy1, $proxy2);
}

public function testGetProxySuccessWithAlias()
{
$this->decorateSuccessfulMockGenerator();

$proxy1 = $this->proxyBuilder->getProxy(\stdClass::class, 'bar');
$proxy2 = $this->proxyBuilder->getProxy(\stdClass::class, 'foo');

$this->assertInstanceOf(Proxy::class, $proxy1);
$this->assertInstanceOf(Proxy::class, $proxy2);

$this->assertNotSame($proxy1, $proxy2);
}

public function testGetProxyThrowException()
{
$this->expectException(MockNotCreatedException::class);

$this->mock(MockGenerator::class)->stub([
'getMock' => new \Exception()
]);

$this->proxyBuilder->getProxy(\stdClass::class, 'acme');
}

private function decorateSuccessfulMockGenerator()
{
$this->mock(MockGenerator::class)->stub([
'getMock' => $this->mock(\stdClass::class)->serve()
]);
}
}
11 changes: 1 addition & 10 deletions tests/Builder/ProxyBuilderTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
<?php
/**
* Created by PhpStorm.
* User: angelogiuffredi
* Date: 14/06/2017
* Time: 14:12
*/

namespace Tests\Builder;


use Moka\Builder\ProxyBuilder;
use Moka\Exception\MockNotCreatedException;
use Moka\Proxy\Proxy;
Expand Down Expand Up @@ -47,7 +40,7 @@ public function testGetProxySuccess()
$proxy1 = $this->proxyBuilder->getProxy(\stdClass::class);
$proxy2 = $this->proxyBuilder->getProxy(\stdClass::class);

$this->assertInstanceOf(Proxy::class, $proxy2);
$this->assertInstanceOf(Proxy::class, $proxy1);

$this->assertSame($proxy1, $proxy2);
}
Expand All @@ -74,6 +67,4 @@ public function testGetProxyThrowException()

$this->proxyBuilder->getProxy(\stdClass::class, 'acme');
}


}
16 changes: 0 additions & 16 deletions tests/Factory/ProxyBuilderFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,12 @@
<?php
/**
* Created by PhpStorm.
* User: angelogiuffredi
* Date: 14/06/2017
* Time: 13:44
*/

namespace Tests\Factory;


use Moka\Builder\ProxyBuilder;
use Moka\Factory\ProxyBuilderFactory;

class ProxyBuilderFactoryTest extends \PHPUnit_Framework_TestCase
{
private $mock;

public function setUp()
{
$this->mock = $this->getMockBuilder(\stdClass::class)
->disableOriginalConstructor()
->getMock();
}

public function testGet()
{
$proxyBuilder1 = ProxyBuilderFactory::get();
Expand Down
25 changes: 25 additions & 0 deletions tests/Factory/ProxyFactorySelfTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Tests\Factory;

use Moka\Factory\ProxyFactory;
use Moka\Proxy\Proxy;
use Moka\Traits\MokaCleanerTrait;
use Moka\Traits\MokaTrait;

class ProxyFactorySelfTest extends \PHPUnit_Framework_TestCase
{
use MokaTrait;
use MokaCleanerTrait;

public function testGet()
{
$mock1 = ProxyFactory::get($this->mock(\stdClass::class)->serve());
$mock2 = ProxyFactory::get($this->mock(\stdClass::class)->serve());

$this->assertInstanceOf(Proxy::class, $mock1);
$this->assertInstanceOf(Proxy::class, $mock2);

$this->assertNotSame($mock1, $mock2);
}
}
7 changes: 0 additions & 7 deletions tests/Factory/ProxyFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
<?php
/**
* Created by PhpStorm.
* User: angelogiuffredi
* Date: 14/06/2017
* Time: 13:40
*/

namespace Tests\Factory;


use Moka\Factory\ProxyFactory;
use Moka\Proxy\Proxy;

Expand Down
8 changes: 0 additions & 8 deletions tests/MokaTest.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
<?php
/**
* Created by PhpStorm.
* User: angelogiuffredi
* Date: 14/06/2017
* Time: 15:02
*/

namespace Tests;


use Moka\Moka;
use Moka\Proxy\Proxy;

class MokaTest extends \PHPUnit_Framework_TestCase
{

public function testGetSuccess()
{
$this->assertInstanceOf(
Expand Down
8 changes: 0 additions & 8 deletions tests/Proxy/MockFakeClass.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
<?php
/**
* Created by PhpStorm.
* User: angelogiuffredi
* Date: 14/06/2017
* Time: 13:56
*/

namespace Tests\Proxy;


class MockFakeClass
{
public function isValid(): bool
Expand All @@ -18,6 +11,5 @@ public function isValid(): bool

public function throwException()
{

}
}
66 changes: 66 additions & 0 deletions tests/Proxy/ProxyContainerSelfTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Tests\Proxy;

use Moka\Exception\InvalidIdentifierException;
use Moka\Proxy\Proxy;
use Moka\Proxy\ProxyContainer;
use Moka\Traits\MokaCleanerTrait;
use Moka\Traits\MokaTrait;

class ProxyContainerSelfTest extends \PHPUnit_Framework_TestCase
{
use MokaTrait;
use MokaCleanerTrait;

/**
* @var ProxyContainer
*/
private $proxyContainer;

public function setUp()
{
$this->proxyContainer = new ProxyContainer();

$this->proxyContainer->set(
'foo',
$this->mock(Proxy::class)->serve()
);
}

public function testGetSuccess()
{
$proxy = $this->proxyContainer->get('foo');

$this->assertInstanceOf(Proxy::class, $proxy);
}

public function testGetFailure()
{
$this->expectException(InvalidIdentifierException::class);
$this->proxyContainer->get('bar');
}

public function testHasSuccess()
{
$this->assertTrue(
$this->proxyContainer->has('foo')
);
}

public function testHasFailure()
{
$this->assertFalse(
$this->proxyContainer->has('bar')
);
}

public function testSet()
{
$this->proxyContainer->set('acme', $this->mock(Proxy::class)->serve());
$this->assertInstanceOf(
Proxy::class,
$this->proxyContainer->get('acme')
);
}
}
7 changes: 0 additions & 7 deletions tests/Proxy/ProxyContainerTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
<?php
/**
* Created by PhpStorm.
* User: angelogiuffredi
* Date: 14/06/2017
* Time: 13:17
*/

namespace Tests\Proxy;


use Moka\Exception\InvalidIdentifierException;
use Moka\Proxy\Proxy;
use Moka\Proxy\ProxyContainer;
Expand Down
51 changes: 51 additions & 0 deletions tests/Proxy/ProxySelfTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Tests\Proxy;

use Moka\Proxy\Proxy;
use Moka\Traits\MokaCleanerTrait;
use Moka\Traits\MokaTrait;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

class ProxySelfTest extends \PHPUnit_Framework_TestCase
{
use MokaTrait;
use MokaCleanerTrait;

/**
* @var Proxy
*/
private $proxy;

public function setUp()
{
$this->proxy = new Proxy(
$this->mock(MockFakeClass::class)->serve()
);
}

public function testServe()
{
$this->assertInstanceOf(MockObject::class, $this->proxy->serve());
}

public function testStubScalarValue()
{
$this->proxy->stub([
'isValid' => true
]);

$this->assertTrue($this->proxy->serve()->isValid());
}

public function testStubThrowException()
{
$this->expectException(\Exception::class);

$this->proxy->stub([
'throwException' => new \Exception()
]);

$this->proxy->serve()->throwException();
}
}
7 changes: 0 additions & 7 deletions tests/Proxy/ProxyTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
<?php
/**
* Created by PhpStorm.
* User: angelogiuffredi
* Date: 14/06/2017
* Time: 13:48
*/

namespace Tests\Proxy;


use Moka\Proxy\Proxy;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

Expand Down

0 comments on commit 29a402c

Please sign in to comment.