Skip to content

Commit b459993

Browse files
committed
Added tests
1 parent cda286c commit b459993

36 files changed

+2897
-3
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
],
2222
"require": {
2323
"php": ">=5.6.7",
24-
"dazzle-php/throwable": "0.4.*"
24+
"dazzle-php/throwable": "0.5.*"
2525
},
2626
"require-dev": {
2727
"phpunit/phpunit": ">=4.8.0 <5.4.0"

src/Util/Factory/FactoryPlugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ protected function unregister(FactoryInterface $factory)
7373
*/
7474
private function throwException($ex)
7575
{
76-
throw new ExecutionException("FactoryPlugin [" . get_class($this) . "] raised an error.", $ex);
76+
throw new ExecutionException("FactoryPlugin [" . get_class($this) . "] raised an error.", 0, $ex);
7777
}
7878
}

src/Util/Factory/SimpleFactoryPlugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ protected function unregister(SimpleFactoryInterface $factory)
7373
*/
7474
private function throwException($ex)
7575
{
76-
throw new ExecutionException("SimpleFactoryPlugin [" . get_class($this) . "] raised an error.", $ex);
76+
throw new ExecutionException("SimpleFactoryPlugin [" . get_class($this) . "] raised an error.", 0, $ex);
7777
}
7878
}

test/Callback.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Dazzle\Util\Test;
4+
5+
class Callback
6+
{
7+
public function __invoke()
8+
{}
9+
}

test/TModule.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
namespace Dazzle\Util\Test;
4+
5+
class TModule extends TUnit
6+
{}

test/TUnit.php

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
3+
namespace Dazzle\Util\Test;
4+
5+
use ReflectionClass;
6+
7+
class TUnit extends \PHPUnit_Framework_TestCase
8+
{
9+
/**
10+
* Return project root.
11+
*
12+
* @return string
13+
*/
14+
public function basePath()
15+
{
16+
return realpath(__DIR__ . '/..');
17+
}
18+
19+
/**
20+
* @return TUnit
21+
*/
22+
public function getTest()
23+
{
24+
return $this;
25+
}
26+
27+
/**
28+
* @return \PHPUnit_Framework_MockObject_Matcher_InvokedCount
29+
*/
30+
public function twice()
31+
{
32+
return $this->exactly(2);
33+
}
34+
35+
/**
36+
* Creates a callback that must be called $amount times or the test will fail.
37+
*
38+
* @param $amount
39+
* @return callable|\PHPUnit_Framework_MockObject_MockObject
40+
*/
41+
public function expectCallableExactly($amount)
42+
{
43+
$mock = $this->createCallableMock();
44+
$mock
45+
->expects($this->exactly($amount))
46+
->method('__invoke');
47+
48+
return $mock;
49+
}
50+
51+
/**
52+
* Creates a callback that must be called once.
53+
*
54+
* @return callable|\PHPUnit_Framework_MockObject_MockObject
55+
*/
56+
public function expectCallableOnce()
57+
{
58+
$mock = $this->createCallableMock();
59+
$mock
60+
->expects($this->once())
61+
->method('__invoke');
62+
63+
return $mock;
64+
}
65+
66+
/**
67+
* Creates a callback that must be called twice.
68+
*
69+
* @return callable|\PHPUnit_Framework_MockObject_MockObject
70+
*/
71+
public function expectCallableTwice()
72+
{
73+
$mock = $this->createCallableMock();
74+
$mock
75+
->expects($this->exactly(2))
76+
->method('__invoke');
77+
78+
return $mock;
79+
}
80+
81+
/**
82+
* Creates a callable that must not be called once.
83+
*
84+
* @return callable|\PHPUnit_Framework_MockObject_MockObject
85+
*/
86+
public function expectCallableNever()
87+
{
88+
$mock = $this->createCallableMock();
89+
$mock
90+
->expects($this->never())
91+
->method('__invoke');
92+
93+
return $mock;
94+
}
95+
96+
/**
97+
* Creates a callable mock.
98+
*
99+
* @return callable|\PHPUnit_Framework_MockObject_MockObject
100+
*/
101+
public function createCallableMock()
102+
{
103+
return $this->getMock(Callback::class);
104+
}
105+
106+
/**
107+
* Check if protected property exists.
108+
*
109+
* @param object $object
110+
* @param string $property
111+
* @return bool
112+
*/
113+
public function existsProtectedProperty($object, $property)
114+
{
115+
$reflection = new ReflectionClass($object);
116+
return $reflection->hasProperty($property);
117+
}
118+
119+
/**
120+
* Get protected property from given object via reflection.
121+
*
122+
* @param object $object
123+
* @param string $property
124+
* @return mixed
125+
*/
126+
public function getProtectedProperty($object, $property)
127+
{
128+
$reflection = new ReflectionClass($object);
129+
$reflection_property = $reflection->getProperty($property);
130+
$reflection_property->setAccessible(true);
131+
132+
return $reflection_property->getValue($object);
133+
}
134+
135+
/**
136+
* Set protected property on a given object via reflection.
137+
*
138+
* @param object $object
139+
* @param string $property
140+
* @param mixed $value
141+
* @return object
142+
*/
143+
public function setProtectedProperty($object, $property, $value)
144+
{
145+
$reflection = new ReflectionClass($object);
146+
$reflection_property = $reflection->getProperty($property);
147+
$reflection_property->setAccessible(true);
148+
$reflection_property->setValue($object, $value);
149+
150+
return $object;
151+
}
152+
153+
/**
154+
* Call protected method on a given object via reflection.
155+
*
156+
* @param object|string $objectOrClass
157+
* @param string $method
158+
* @param mixed[] $args
159+
* @return mixed
160+
*/
161+
public function callProtectedMethod($objectOrClass, $method, $args = [])
162+
{
163+
$reflection = new ReflectionClass($objectOrClass);
164+
$reflectionMethod = $reflection->getMethod($method);
165+
$reflectionMethod->setAccessible(true);
166+
$reflectionTarget = is_object($objectOrClass) ? $objectOrClass : null;
167+
168+
return $reflectionMethod->invokeArgs($reflectionTarget, $args);
169+
}
170+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace Dazzle\Util\Test\TUnit\Buffer;
4+
5+
use Dazzle\Util\Test\TUnit;
6+
use Dazzle\Throwable\Exception\Runtime\OutOfBoundsException;
7+
use Dazzle\Util\Buffer\Buffer;
8+
use Dazzle\Util\Buffer\BufferInterface;
9+
use Dazzle\Util\Buffer\BufferIterator;
10+
11+
class BufferIteratorTest extends TUnit
12+
{
13+
/**
14+
* @var string
15+
*/
16+
protected $initialString = 'abcdefghijklmnopqrstuvwxyz';
17+
18+
/**
19+
* @var string
20+
*/
21+
protected $appendString = '1234567890';
22+
23+
/**
24+
* @var BufferInterface
25+
*/
26+
protected $buffer;
27+
28+
/**
29+
* @var BufferIterator
30+
*/
31+
protected $iterator;
32+
33+
public function setUp()
34+
{
35+
$this->buffer = new Buffer($this->initialString);
36+
$this->iterator = $this->buffer->getIterator();
37+
}
38+
39+
public function testApiSeek()
40+
{
41+
$this->iterator->seek($this->buffer->length()-1);
42+
43+
$this->assertSame($this->buffer->length()-1, $this->iterator->key());
44+
$this->assertSame(substr($this->buffer, -1), $this->iterator->current());
45+
}
46+
47+
public function testApiSeek_WhenInvalidPositionIsPassed()
48+
{
49+
$this->iterator->seek(-1);
50+
51+
$this->assertSame(0, $this->iterator->key());
52+
}
53+
54+
public function testApiInsert()
55+
{
56+
$this->iterator->next();
57+
$this->iterator->insert($this->appendString);
58+
59+
$this->assertSame(substr($this->initialString, 0, 1) . $this->appendString . substr($this->initialString, 1), (string) $this->buffer);
60+
}
61+
62+
public function testApiInsert_ThrowsException_OnInvalidIterator()
63+
{
64+
$this->setExpectedException(OutOfBoundsException::class);
65+
for ($this->iterator->rewind(); $this->iterator->valid(); $this->iterator->next());
66+
67+
$this->iterator->insert($this->appendString);
68+
}
69+
70+
public function testApiReplace()
71+
{
72+
$this->iterator->replace($this->appendString);
73+
74+
$this->assertSame($this->appendString . substr($this->initialString, 1), (string) $this->buffer);
75+
}
76+
77+
public function testApiReplace_ThrowsException_OnInvalidIterator()
78+
{
79+
$this->setExpectedException(OutOfBoundsException::class);
80+
for ($this->iterator->rewind(); $this->iterator->valid(); $this->iterator->next());
81+
82+
$this->iterator->replace($this->appendString);
83+
}
84+
85+
public function testApiRemove()
86+
{
87+
$this->iterator->remove();
88+
89+
$this->assertSame(substr($this->initialString, 1), (string) $this->buffer);
90+
91+
$this->iterator->next();
92+
$this->iterator->remove();
93+
94+
$this->assertSame(substr($this->initialString, 2), (string) $this->buffer);
95+
}
96+
97+
public function testApiRemove_ThrowsException_OnInvalidIterator()
98+
{
99+
$this->setExpectedException(OutOfBoundsException::class);
100+
for ($this->iterator->rewind(); $this->iterator->valid(); $this->iterator->next());
101+
102+
$this->iterator->remove();
103+
}
104+
}

0 commit comments

Comments
 (0)