Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
khelle committed Jun 19, 2017
1 parent cda286c commit b459993
Show file tree
Hide file tree
Showing 36 changed files with 2,897 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
],
"require": {
"php": ">=5.6.7",
"dazzle-php/throwable": "0.4.*"
"dazzle-php/throwable": "0.5.*"
},
"require-dev": {
"phpunit/phpunit": ">=4.8.0 <5.4.0"
Expand Down
2 changes: 1 addition & 1 deletion src/Util/Factory/FactoryPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ protected function unregister(FactoryInterface $factory)
*/
private function throwException($ex)
{
throw new ExecutionException("FactoryPlugin [" . get_class($this) . "] raised an error.", $ex);
throw new ExecutionException("FactoryPlugin [" . get_class($this) . "] raised an error.", 0, $ex);
}
}
2 changes: 1 addition & 1 deletion src/Util/Factory/SimpleFactoryPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ protected function unregister(SimpleFactoryInterface $factory)
*/
private function throwException($ex)
{
throw new ExecutionException("SimpleFactoryPlugin [" . get_class($this) . "] raised an error.", $ex);
throw new ExecutionException("SimpleFactoryPlugin [" . get_class($this) . "] raised an error.", 0, $ex);
}
}
9 changes: 9 additions & 0 deletions test/Callback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Dazzle\Util\Test;

class Callback
{
public function __invoke()
{}
}
6 changes: 6 additions & 0 deletions test/TModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

namespace Dazzle\Util\Test;

class TModule extends TUnit
{}
170 changes: 170 additions & 0 deletions test/TUnit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php

namespace Dazzle\Util\Test;

use ReflectionClass;

class TUnit extends \PHPUnit_Framework_TestCase
{
/**
* Return project root.
*
* @return string
*/
public function basePath()
{
return realpath(__DIR__ . '/..');
}

/**
* @return TUnit
*/
public function getTest()
{
return $this;
}

/**
* @return \PHPUnit_Framework_MockObject_Matcher_InvokedCount
*/
public function twice()
{
return $this->exactly(2);
}

/**
* Creates a callback that must be called $amount times or the test will fail.
*
* @param $amount
* @return callable|\PHPUnit_Framework_MockObject_MockObject
*/
public function expectCallableExactly($amount)
{
$mock = $this->createCallableMock();
$mock
->expects($this->exactly($amount))
->method('__invoke');

return $mock;
}

/**
* Creates a callback that must be called once.
*
* @return callable|\PHPUnit_Framework_MockObject_MockObject
*/
public function expectCallableOnce()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke');

return $mock;
}

/**
* Creates a callback that must be called twice.
*
* @return callable|\PHPUnit_Framework_MockObject_MockObject
*/
public function expectCallableTwice()
{
$mock = $this->createCallableMock();
$mock
->expects($this->exactly(2))
->method('__invoke');

return $mock;
}

/**
* Creates a callable that must not be called once.
*
* @return callable|\PHPUnit_Framework_MockObject_MockObject
*/
public function expectCallableNever()
{
$mock = $this->createCallableMock();
$mock
->expects($this->never())
->method('__invoke');

return $mock;
}

/**
* Creates a callable mock.
*
* @return callable|\PHPUnit_Framework_MockObject_MockObject
*/
public function createCallableMock()
{
return $this->getMock(Callback::class);
}

/**
* Check if protected property exists.
*
* @param object $object
* @param string $property
* @return bool
*/
public function existsProtectedProperty($object, $property)
{
$reflection = new ReflectionClass($object);
return $reflection->hasProperty($property);
}

/**
* Get protected property from given object via reflection.
*
* @param object $object
* @param string $property
* @return mixed
*/
public function getProtectedProperty($object, $property)
{
$reflection = new ReflectionClass($object);
$reflection_property = $reflection->getProperty($property);
$reflection_property->setAccessible(true);

return $reflection_property->getValue($object);
}

/**
* Set protected property on a given object via reflection.
*
* @param object $object
* @param string $property
* @param mixed $value
* @return object
*/
public function setProtectedProperty($object, $property, $value)
{
$reflection = new ReflectionClass($object);
$reflection_property = $reflection->getProperty($property);
$reflection_property->setAccessible(true);
$reflection_property->setValue($object, $value);

return $object;
}

/**
* Call protected method on a given object via reflection.
*
* @param object|string $objectOrClass
* @param string $method
* @param mixed[] $args
* @return mixed
*/
public function callProtectedMethod($objectOrClass, $method, $args = [])
{
$reflection = new ReflectionClass($objectOrClass);
$reflectionMethod = $reflection->getMethod($method);
$reflectionMethod->setAccessible(true);
$reflectionTarget = is_object($objectOrClass) ? $objectOrClass : null;

return $reflectionMethod->invokeArgs($reflectionTarget, $args);
}
}
104 changes: 104 additions & 0 deletions test/TUnit/Buffer/BufferIteratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Dazzle\Util\Test\TUnit\Buffer;

use Dazzle\Util\Test\TUnit;
use Dazzle\Throwable\Exception\Runtime\OutOfBoundsException;
use Dazzle\Util\Buffer\Buffer;
use Dazzle\Util\Buffer\BufferInterface;
use Dazzle\Util\Buffer\BufferIterator;

class BufferIteratorTest extends TUnit
{
/**
* @var string
*/
protected $initialString = 'abcdefghijklmnopqrstuvwxyz';

/**
* @var string
*/
protected $appendString = '1234567890';

/**
* @var BufferInterface
*/
protected $buffer;

/**
* @var BufferIterator
*/
protected $iterator;

public function setUp()
{
$this->buffer = new Buffer($this->initialString);
$this->iterator = $this->buffer->getIterator();
}

public function testApiSeek()
{
$this->iterator->seek($this->buffer->length()-1);

$this->assertSame($this->buffer->length()-1, $this->iterator->key());
$this->assertSame(substr($this->buffer, -1), $this->iterator->current());
}

public function testApiSeek_WhenInvalidPositionIsPassed()
{
$this->iterator->seek(-1);

$this->assertSame(0, $this->iterator->key());
}

public function testApiInsert()
{
$this->iterator->next();
$this->iterator->insert($this->appendString);

$this->assertSame(substr($this->initialString, 0, 1) . $this->appendString . substr($this->initialString, 1), (string) $this->buffer);
}

public function testApiInsert_ThrowsException_OnInvalidIterator()
{
$this->setExpectedException(OutOfBoundsException::class);
for ($this->iterator->rewind(); $this->iterator->valid(); $this->iterator->next());

$this->iterator->insert($this->appendString);
}

public function testApiReplace()
{
$this->iterator->replace($this->appendString);

$this->assertSame($this->appendString . substr($this->initialString, 1), (string) $this->buffer);
}

public function testApiReplace_ThrowsException_OnInvalidIterator()
{
$this->setExpectedException(OutOfBoundsException::class);
for ($this->iterator->rewind(); $this->iterator->valid(); $this->iterator->next());

$this->iterator->replace($this->appendString);
}

public function testApiRemove()
{
$this->iterator->remove();

$this->assertSame(substr($this->initialString, 1), (string) $this->buffer);

$this->iterator->next();
$this->iterator->remove();

$this->assertSame(substr($this->initialString, 2), (string) $this->buffer);
}

public function testApiRemove_ThrowsException_OnInvalidIterator()
{
$this->setExpectedException(OutOfBoundsException::class);
for ($this->iterator->rewind(); $this->iterator->valid(); $this->iterator->next());

$this->iterator->remove();
}
}
Loading

0 comments on commit b459993

Please sign in to comment.