-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
2,897 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Dazzle\Util\Test; | ||
|
||
class Callback | ||
{ | ||
public function __invoke() | ||
{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
namespace Dazzle\Util\Test; | ||
|
||
class TModule extends TUnit | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.