Skip to content

Commit e5c0872

Browse files
committed
Added tests
1 parent e2b9d7a commit e5c0872

30 files changed

+6113
-0
lines changed

test/Callback.php

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

test/TModule.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
namespace Dazzle\Promise\Test;
4+
5+
class TModule extends TUnit
6+
{}

test/TModule/PromiseTest.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Dazzle\Promise\Test\TModule;
4+
5+
use Dazzle\Promise\Deferred;
6+
use Dazzle\Promise\Test\TModule;
7+
8+
/**
9+
* @runTestsInSeparateProcesses
10+
* @preserveGlobalState disabled
11+
*/
12+
class PromiseTest extends TModule
13+
{
14+
/**
15+
*
16+
*/
17+
public function testPromise_SupportsVeryDeepNesting()
18+
{
19+
ini_set('xdebug.max_nesting_level', 8192);
20+
21+
$deferreds = [];
22+
23+
for ($i = 0; $i < 10; $i++)
24+
{
25+
$deferreds[] = $d = new Deferred();
26+
$p = $d->getPromise();
27+
$last = $p;
28+
for ($j = 0; $j < 500; $j++)
29+
{
30+
$last = $last->then(function($result) {
31+
return $result;
32+
});
33+
}
34+
}
35+
36+
$p = null;
37+
38+
foreach ($deferreds as $d)
39+
{
40+
if ($p) {
41+
$d->resolve($p);
42+
}
43+
$p = $d->getPromise();
44+
}
45+
46+
$deferreds[0]->resolve(true);
47+
48+
$mock = $this->createCallableMock();
49+
$mock
50+
->expects($this->once())
51+
->method('__invoke')
52+
->with($this->identicalTo(true));
53+
54+
$deferreds[0]->getPromise()->then($mock);
55+
}
56+
}

test/TUnit.php

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
3+
namespace Dazzle\Promise\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+
}

test/TUnit/DeferredTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Dazzle\Promise\Test\TUnit;
4+
5+
use Dazzle\Promise\Test\TUnit\_Bridge\DeferredBridge;
6+
use Dazzle\Promise\Test\TUnit\_Partial\FullTestPartial;
7+
use Dazzle\Promise\Deferred;
8+
use Dazzle\Promise\DeferredInterface;
9+
use Dazzle\Promise\Promise;
10+
use Dazzle\Promise\PromiseInterface;
11+
use Dazzle\Promise\Test\TUnit;
12+
13+
class DeferredTest extends TUnit
14+
{
15+
use FullTestPartial;
16+
17+
/**
18+
* @return DeferredInterface
19+
*/
20+
public function createDeferred()
21+
{
22+
$d = new Deferred();
23+
24+
return new DeferredBridge([
25+
'getPromise' => [ $d, 'getPromise' ],
26+
'resolve' => [ $d, 'resolve' ],
27+
'reject' => [ $d, 'reject' ],
28+
'cancel' => [ $d, 'cancel' ],
29+
]);
30+
}
31+
32+
/**
33+
* @param string[] $methods
34+
* @return PromiseInterface|\PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
public function createPromiseMock($methods = [])
37+
{
38+
return $this->getMock(Promise::class, $methods);
39+
}
40+
}

0 commit comments

Comments
 (0)