|
| 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 | +} |
0 commit comments