PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/codeception/codeception/tests/unit/Codeception/Util/StubTest.php

https://gitlab.com/jhonn/rest
PHP | 316 lines | 240 code | 51 blank | 25 comment | 8 complexity | 1f234fed362b4efa284b5da64d9a1f02 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. use \Codeception\Util\Stub as Stub;
  3. class StubTest extends \PHPUnit_Framework_TestCase
  4. {
  5. /**
  6. * @var DummyClass
  7. */
  8. protected $dummy;
  9. public function setUp() {
  10. $conf = \Codeception\Configuration::config();
  11. require_once $file = \Codeception\Configuration::dataDir().'DummyClass.php';
  12. $this->dummy = new DummyClass(true);
  13. }
  14. public function testMakeEmpty() {
  15. $dummy = Stub::makeEmpty('DummyClass');
  16. $this->assertInstanceOf('DummyClass', $dummy);
  17. $this->assertTrue(method_exists($dummy,'helloWorld'));
  18. $this->assertNull($dummy->helloWorld());
  19. }
  20. public function testMakeEmptyMethodReplaced() {
  21. $dummy = Stub::makeEmpty('DummyClass', array('helloWorld' => function () { return 'good bye world'; }));
  22. $this->assertMethodReplaced($dummy);
  23. }
  24. public function testMakeEmptyMethodSimplyReplaced()
  25. {
  26. $dummy = Stub::makeEmpty('DummyClass', array('helloWorld' => 'good bye world'));
  27. $this->assertMethodReplaced($dummy);
  28. }
  29. public function testMakeEmptyExcept() {
  30. $dummy = Stub::makeEmptyExcept('DummyClass', 'helloWorld');
  31. $this->assertEquals($this->dummy->helloWorld(), $dummy->helloWorld());
  32. $this->assertNull($dummy->goodByeWorld());
  33. }
  34. public function testMakeEmptyExceptPropertyReplaced() {
  35. $dummy = Stub::makeEmptyExcept('DummyClass', 'getCheckMe', array('checkMe' => 'checked!'));
  36. $this->assertEquals('checked!', $dummy->getCheckMe());
  37. }
  38. public function testMakeEmptyExceptMagicalPropertyReplaced() {
  39. $dummy = Stub::makeEmptyExcept('DummyClass', 'getCheckMeToo', array('checkMeToo' => 'checked!'));
  40. $this->assertEquals('checked!', $dummy->getCheckMeToo());
  41. }
  42. public function testFactory() {
  43. $dummies = Stub::factory('DummyClass',2);
  44. $this->assertCount(2, $dummies);
  45. $this->assertInstanceOf('DummyClass', $dummies[0]);
  46. }
  47. public function testMake() {
  48. $dummy = Stub::make('DummyClass', array('goodByeWorld' => function () { return 'hello world'; }));
  49. $this->assertEquals($this->dummy->helloWorld(), $dummy->helloWorld());
  50. $this->assertEquals("hello world", $dummy->goodByeWorld());
  51. }
  52. public function testMakeMethodReplaced() {
  53. $dummy = Stub::make('DummyClass', array('helloWorld' => function () { return 'good bye world'; }));
  54. $this->assertMethodReplaced($dummy);
  55. }
  56. public function testMakeWithMagicalPropertiesReplaced() {
  57. $dummy = Stub::make('DummyClass', array('checkMeToo' => 'checked!'));
  58. $this->assertEquals('checked!', $dummy->checkMeToo);
  59. }
  60. public function testMakeMethodSimplyReplaced()
  61. {
  62. $dummy = Stub::make('DummyClass', array('helloWorld' => 'good bye world'));
  63. $this->assertMethodReplaced($dummy);
  64. }
  65. public function testCopy() {
  66. $dummy = Stub::copy($this->dummy, array('checkMe' => 'checked!'));
  67. $this->assertEquals('checked!', $dummy->getCheckMe());
  68. $dummy = Stub::copy($this->dummy, array('checkMeToo' => 'checked!'));
  69. $this->assertEquals('checked!', $dummy->getCheckMeToo());
  70. }
  71. public function testConstruct()
  72. {
  73. $dummy = Stub::construct('DummyClass', array('checkMe' => 'checked!'));
  74. $this->assertEquals('constructed: checked!', $dummy->getCheckMe());
  75. $dummy = Stub::construct('DummyClass', array('checkMe' => 'checked!'), array('targetMethod' => function () { return false; }));
  76. $this->assertEquals('constructed: checked!', $dummy->getCheckMe());
  77. $this->assertEquals(false, $dummy->targetMethod());
  78. }
  79. public function testConstructMethodReplaced() {
  80. $dummy = Stub::construct('DummyClass', array(), array('helloWorld' => function () { return 'good bye world'; }));
  81. $this->assertMethodReplaced($dummy);
  82. }
  83. public function testConstructMethodSimplyReplaced()
  84. {
  85. $dummy = Stub::make('DummyClass', array('helloWorld' => 'good bye world'));
  86. $this->assertMethodReplaced($dummy);
  87. }
  88. public function testConstructEmpty()
  89. {
  90. $dummy = Stub::constructEmpty('DummyClass', array('checkMe' => 'checked!'));
  91. $this->assertNull($dummy->getCheckMe());
  92. }
  93. public function testConstructEmptyExcept()
  94. {
  95. $dummy = Stub::constructEmptyExcept('DummyClass', 'getCheckMe', array('checkMe' => 'checked!'));
  96. $this->assertNull($dummy->targetMethod());
  97. $this->assertEquals('constructed: checked!', $dummy->getCheckMe());
  98. }
  99. public function testUpdate()
  100. {
  101. $dummy = Stub::construct('DummyClass');
  102. Stub::update($dummy, array('checkMe' => 'done'));
  103. $this->assertEquals('done', $dummy->getCheckMe());
  104. Stub::update($dummy, array('checkMeToo' => 'done'));
  105. $this->assertEquals('done', $dummy->getCheckMeToo());
  106. }
  107. public function testStubsFromObject()
  108. {
  109. $dummy = Stub::make(new \DummyClass());
  110. $this->assertTrue(isset($dummy->__mocked));
  111. $dummy = Stub::makeEmpty(new \DummyClass());
  112. $this->assertTrue(isset($dummy->__mocked));
  113. $dummy = Stub::makeEmptyExcept(new \DummyClass(),'helloWorld');
  114. $this->assertTrue(isset($dummy->__mocked));
  115. $dummy = Stub::construct(new \DummyClass());
  116. $this->assertTrue(isset($dummy->__mocked));
  117. $dummy = Stub::constructEmpty(new \DummyClass());
  118. $this->assertTrue(isset($dummy->__mocked));
  119. $dummy = Stub::constructEmptyExcept(new \DummyClass(),'helloWorld');
  120. $this->assertTrue(isset($dummy->__mocked));
  121. }
  122. protected function assertMethodReplaced($dummy)
  123. {
  124. $this->assertTrue(method_exists($dummy,'helloWorld'));
  125. $this->assertNotEquals($this->dummy->helloWorld(),$dummy->helloWorld());
  126. $this->assertEquals($dummy->helloWorld(),'good bye world');
  127. }
  128. public static function matcherAndFailMessageProvider()
  129. {
  130. return array(
  131. array(Stub::never(),
  132. "DummyClass::targetMethod() was not expected to be called."
  133. ),
  134. array(Stub::atLeastOnce(),
  135. "Expectation failed for method name is equal to <string:targetMethod> when invoked at least once.\n"
  136. . 'Expected invocation at least once but it never occured.'
  137. ),
  138. array(Stub::once(),
  139. "Expectation failed for method name is equal to <string:targetMethod> when invoked 1 time(s).\n"
  140. . 'Method was expected to be called 1 times, actually called 0 times.'
  141. ),
  142. array(Stub::exactly(1),
  143. "Expectation failed for method name is equal to <string:targetMethod> when invoked 3 time(s).\n"
  144. . 'Method was expected to be called 3 times, actually called 0 times.'
  145. ),
  146. array(Stub::exactly(3),
  147. "Expectation failed for method name is equal to <string:targetMethod> when invoked 3 time(s).\n"
  148. . 'Method was expected to be called 3 times, actually called 0 times.'
  149. ),
  150. );
  151. }
  152. /**
  153. * @dataProvider matcherAndFailMessageProvider
  154. */
  155. public function testMockedMethodIsCalledFail($stubMarshaler, $failMessage) {
  156. $mock = Stub::makeEmptyExcept('DummyClass', 'call', array('targetMethod' => $stubMarshaler), $this);
  157. $mock->goodByeWorld();
  158. try {
  159. if ($this->thereAreNeverMatcher($stubMarshaler))
  160. $this->thenWeMustCallMethodForException($mock);
  161. else
  162. $this->thenWeDontCallAnyMethodForExceptionJustVerify($mock);
  163. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  164. $this->assertSame( $failMessage, $e->getMessage() );
  165. }
  166. $this->resetMockObjects();
  167. }
  168. private function thenWeMustCallMethodForException($mock) {
  169. $mock->call();
  170. }
  171. private function thenWeDontCallAnyMethodForExceptionJustVerify($mock) {
  172. $mock->__phpunit_verify();
  173. $this->fail('Expected exception');
  174. }
  175. private function thereAreNeverMatcher($stubMarshaler) {
  176. $matcher = $stubMarshaler->getMatcher();
  177. return 0 == $matcher->getInvocationCount();
  178. }
  179. private function resetMockObjects()
  180. {
  181. $refl = new ReflectionObject($this);
  182. $refl = $refl->getParentClass();
  183. $prop = $refl->getProperty('mockObjects');
  184. $prop->setAccessible(true);
  185. $prop->setValue($this, array());
  186. }
  187. public static function matcherProvider()
  188. {
  189. return array(
  190. array(0, Stub::never()),
  191. array(1, Stub::once()),
  192. array(2, Stub::atLeastOnce()),
  193. array(3, Stub::exactly(3)),
  194. array(1, Stub::once(function() {return true;}), true),
  195. array(2, Stub::atLeastOnce(function() {return array();}), array()),
  196. array(1, Stub::exactly(1, function() {return null;}), null),
  197. array(1, Stub::exactly(1, function() {return 'hello world!';}), 'hello world!'),
  198. );
  199. }
  200. /**
  201. * @dataProvider matcherProvider
  202. */
  203. public function testMethodMatcherWithMake($count, $matcher, $expected = false)
  204. {
  205. $dummy = Stub::make('DummyClass', array('goodByeWorld' => $matcher), $this);
  206. $this->repeatCall($count, array($dummy, 'goodByeWorld'), $expected);
  207. }
  208. /**
  209. * @dataProvider matcherProvider
  210. */
  211. public function testMethodMatcherWithMakeEmpty($count, $matcher)
  212. {
  213. $dummy = Stub::makeEmpty('DummyClass', array('goodByeWorld' => $matcher), $this);
  214. $this->repeatCall($count, array($dummy, 'goodByeWorld'));
  215. }
  216. /**
  217. * @dataProvider matcherProvider
  218. */
  219. public function testMethodMatcherWithMakeEmptyExcept($count, $matcher)
  220. {
  221. $dummy = Stub::makeEmptyExcept('DummyClass', 'getCheckMe', array('goodByeWorld' => $matcher), $this);
  222. $this->repeatCall($count, array($dummy, 'goodByeWorld'));
  223. }
  224. /**
  225. * @dataProvider matcherProvider
  226. */
  227. public function testMethodMatcherWithConstruct($count, $matcher)
  228. {
  229. $dummy = Stub::construct('DummyClass', array(), array('goodByeWorld' => $matcher), $this);
  230. $this->repeatCall($count, array($dummy, 'goodByeWorld'));
  231. }
  232. /**
  233. * @dataProvider matcherProvider
  234. */
  235. public function testMethodMatcherWithConstructEmpty($count, $matcher)
  236. {
  237. $dummy = Stub::constructEmpty('DummyClass', array(), array('goodByeWorld' => $matcher), $this);
  238. $this->repeatCall($count, array($dummy, 'goodByeWorld'));
  239. }
  240. /**
  241. * @dataProvider matcherProvider
  242. */
  243. public function testMethodMatcherWithConstructEmptyExcept($count, $matcher)
  244. {
  245. $dummy = Stub::constructEmptyExcept('DummyClass', 'getCheckMe', array(), array('goodByeWorld' => $matcher), $this);
  246. $this->repeatCall($count, array($dummy, 'goodByeWorld'));
  247. }
  248. private function repeatCall($count, $callable, $expected = false)
  249. {
  250. for ($i = 0; $i < $count; $i++) {
  251. $actual = call_user_func($callable);
  252. if ($expected) $this->assertEquals($expected, $actual);
  253. }
  254. }
  255. public function testConsecutive()
  256. {
  257. $dummy = Stub::make('DummyClass', array('helloWorld' => Stub::consecutive('david', 'emma', 'sam', 'amy')));
  258. $this->assertEquals('david', $dummy->helloWorld());
  259. $this->assertEquals('emma', $dummy->helloWorld());
  260. $this->assertEquals('sam', $dummy->helloWorld());
  261. $this->assertEquals('amy', $dummy->helloWorld());
  262. // Expected null value when no more values
  263. $this->assertNull($dummy->helloWorld());
  264. }
  265. }