PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/panace/public
PHP | 447 lines | 365 code | 57 blank | 25 comment | 9 complexity | 3b80cba53af4c3d88476ff120693d6be MD5 | raw file
  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. {
  11. $conf = \Codeception\Configuration::config();
  12. require_once $file = \Codeception\Configuration::dataDir().'DummyClass.php';
  13. $this->dummy = new DummyClass(true);
  14. }
  15. public function testMakeEmpty()
  16. {
  17. $dummy = Stub::makeEmpty('DummyClass');
  18. $this->assertInstanceOf('DummyClass', $dummy);
  19. $this->assertTrue(method_exists($dummy, 'helloWorld'));
  20. $this->assertNull($dummy->helloWorld());
  21. }
  22. public function testMakeEmptyMethodReplaced()
  23. {
  24. $dummy = Stub::makeEmpty('DummyClass', array('helloWorld' => function () {
  25. return 'good bye world';
  26. }));
  27. $this->assertMethodReplaced($dummy);
  28. }
  29. public function testMakeEmptyMethodSimplyReplaced()
  30. {
  31. $dummy = Stub::makeEmpty('DummyClass', array('helloWorld' => 'good bye world'));
  32. $this->assertMethodReplaced($dummy);
  33. }
  34. public function testMakeEmptyExcept()
  35. {
  36. $dummy = Stub::makeEmptyExcept('DummyClass', 'helloWorld');
  37. $this->assertEquals($this->dummy->helloWorld(), $dummy->helloWorld());
  38. $this->assertNull($dummy->goodByeWorld());
  39. }
  40. public function testMakeEmptyExceptPropertyReplaced()
  41. {
  42. $dummy = Stub::makeEmptyExcept('DummyClass', 'getCheckMe', array('checkMe' => 'checked!'));
  43. $this->assertEquals('checked!', $dummy->getCheckMe());
  44. }
  45. public function testMakeEmptyExceptMagicalPropertyReplaced()
  46. {
  47. $dummy = Stub::makeEmptyExcept('DummyClass', 'getCheckMeToo', array('checkMeToo' => 'checked!'));
  48. $this->assertEquals('checked!', $dummy->getCheckMeToo());
  49. }
  50. public function testFactory()
  51. {
  52. $dummies = Stub::factory('DummyClass', 2);
  53. $this->assertCount(2, $dummies);
  54. $this->assertInstanceOf('DummyClass', $dummies[0]);
  55. }
  56. public function testMake()
  57. {
  58. $dummy = Stub::make('DummyClass', array('goodByeWorld' => function () {
  59. return 'hello world';
  60. }));
  61. $this->assertEquals($this->dummy->helloWorld(), $dummy->helloWorld());
  62. $this->assertEquals("hello world", $dummy->goodByeWorld());
  63. }
  64. public function testMakeMethodReplaced()
  65. {
  66. $dummy = Stub::make('DummyClass', array('helloWorld' => function () {
  67. return 'good bye world';
  68. }));
  69. $this->assertMethodReplaced($dummy);
  70. }
  71. public function testMakeWithMagicalPropertiesReplaced()
  72. {
  73. $dummy = Stub::make('DummyClass', array('checkMeToo' => 'checked!'));
  74. $this->assertEquals('checked!', $dummy->checkMeToo);
  75. }
  76. public function testMakeMethodSimplyReplaced()
  77. {
  78. $dummy = Stub::make('DummyClass', array('helloWorld' => 'good bye world'));
  79. $this->assertMethodReplaced($dummy);
  80. }
  81. public function testCopy()
  82. {
  83. $dummy = Stub::copy($this->dummy, array('checkMe' => 'checked!'));
  84. $this->assertEquals('checked!', $dummy->getCheckMe());
  85. $dummy = Stub::copy($this->dummy, array('checkMeToo' => 'checked!'));
  86. $this->assertEquals('checked!', $dummy->getCheckMeToo());
  87. }
  88. public function testConstruct()
  89. {
  90. $dummy = Stub::construct('DummyClass', array('checkMe' => 'checked!'));
  91. $this->assertEquals('constructed: checked!', $dummy->getCheckMe());
  92. $dummy = Stub::construct(
  93. 'DummyClass',
  94. array('checkMe' => 'checked!'),
  95. array('targetMethod' => function () {
  96. return false;
  97. })
  98. );
  99. $this->assertEquals('constructed: checked!', $dummy->getCheckMe());
  100. $this->assertEquals(false, $dummy->targetMethod());
  101. }
  102. public function testConstructMethodReplaced()
  103. {
  104. $dummy = Stub::construct(
  105. 'DummyClass',
  106. array(),
  107. array('helloWorld' => function () {
  108. return 'good bye world';
  109. })
  110. );
  111. $this->assertMethodReplaced($dummy);
  112. }
  113. public function testConstructMethodSimplyReplaced()
  114. {
  115. $dummy = Stub::make('DummyClass', array('helloWorld' => 'good bye world'));
  116. $this->assertMethodReplaced($dummy);
  117. }
  118. public function testConstructEmpty()
  119. {
  120. $dummy = Stub::constructEmpty('DummyClass', array('checkMe' => 'checked!'));
  121. $this->assertNull($dummy->getCheckMe());
  122. }
  123. public function testConstructEmptyExcept()
  124. {
  125. $dummy = Stub::constructEmptyExcept('DummyClass', 'getCheckMe', array('checkMe' => 'checked!'));
  126. $this->assertNull($dummy->targetMethod());
  127. $this->assertEquals('constructed: checked!', $dummy->getCheckMe());
  128. }
  129. public function testUpdate()
  130. {
  131. $dummy = Stub::construct('DummyClass');
  132. Stub::update($dummy, array('checkMe' => 'done'));
  133. $this->assertEquals('done', $dummy->getCheckMe());
  134. Stub::update($dummy, array('checkMeToo' => 'done'));
  135. $this->assertEquals('done', $dummy->getCheckMeToo());
  136. }
  137. public function testStubsFromObject()
  138. {
  139. $dummy = Stub::make(new \DummyClass());
  140. $this->assertInstanceOf(
  141. '\PHPUnit_Framework_MockObject_MockObject',
  142. $dummy
  143. );
  144. $dummy = Stub::make(new \DummyOverloadableClass());
  145. $this->assertTrue(isset($dummy->__mocked));
  146. $dummy = Stub::makeEmpty(new \DummyClass());
  147. $this->assertInstanceOf(
  148. '\PHPUnit_Framework_MockObject_MockObject',
  149. $dummy
  150. );
  151. $dummy = Stub::makeEmpty(new \DummyOverloadableClass());
  152. $this->assertTrue(isset($dummy->__mocked));
  153. $dummy = Stub::makeEmptyExcept(new \DummyClass(), 'helloWorld');
  154. $this->assertInstanceOf(
  155. '\PHPUnit_Framework_MockObject_MockObject',
  156. $dummy
  157. );
  158. $dummy = Stub::makeEmptyExcept(new \DummyOverloadableClass(), 'helloWorld');
  159. $this->assertTrue(isset($dummy->__mocked));
  160. $dummy = Stub::construct(new \DummyClass());
  161. $this->assertInstanceOf(
  162. '\PHPUnit_Framework_MockObject_MockObject',
  163. $dummy
  164. );
  165. $dummy = Stub::construct(new \DummyOverloadableClass());
  166. $this->assertTrue(isset($dummy->__mocked));
  167. $dummy = Stub::constructEmpty(new \DummyClass());
  168. $this->assertInstanceOf(
  169. '\PHPUnit_Framework_MockObject_MockObject',
  170. $dummy
  171. );
  172. $dummy = Stub::constructEmpty(new \DummyOverloadableClass());
  173. $this->assertTrue(isset($dummy->__mocked));
  174. $dummy = Stub::constructEmptyExcept(new \DummyClass(), 'helloWorld');
  175. $this->assertInstanceOf(
  176. '\PHPUnit_Framework_MockObject_MockObject',
  177. $dummy
  178. );
  179. $dummy = Stub::constructEmptyExcept(new \DummyOverloadableClass(), 'helloWorld');
  180. $this->assertTrue(isset($dummy->__mocked));
  181. }
  182. protected function assertMethodReplaced($dummy)
  183. {
  184. $this->assertTrue(method_exists($dummy, 'helloWorld'));
  185. $this->assertNotEquals($this->dummy->helloWorld(), $dummy->helloWorld());
  186. $this->assertEquals($dummy->helloWorld(), 'good bye world');
  187. }
  188. public static function matcherAndFailMessageProvider()
  189. {
  190. return array(
  191. array(Stub::never(),
  192. "DummyClass::targetMethod() was not expected to be called."
  193. ),
  194. array(Stub::atLeastOnce(),
  195. "Expectation failed for method name is equal to <string:targetMethod> when invoked at least once.\n"
  196. . 'Expected invocation at least once but it never occured.'
  197. ),
  198. array(Stub::once(),
  199. "Expectation failed for method name is equal to <string:targetMethod> when invoked 1 time(s).\n"
  200. . 'Method was expected to be called 1 times, actually called 0 times.'
  201. ),
  202. array(Stub::exactly(1),
  203. "Expectation failed for method name is equal to <string:targetMethod> when invoked 3 time(s).\n"
  204. . 'Method was expected to be called 3 times, actually called 0 times.'
  205. ),
  206. array(Stub::exactly(3),
  207. "Expectation failed for method name is equal to <string:targetMethod> when invoked 3 time(s).\n"
  208. . 'Method was expected to be called 3 times, actually called 0 times.'
  209. ),
  210. );
  211. }
  212. /**
  213. * @dataProvider matcherAndFailMessageProvider
  214. */
  215. public function testMockedMethodIsCalledFail($stubMarshaler, $failMessage)
  216. {
  217. $mock = Stub::makeEmptyExcept('DummyClass', 'call', array('targetMethod' => $stubMarshaler), $this);
  218. $mock->goodByeWorld();
  219. try {
  220. if ($this->thereAreNeverMatcher($stubMarshaler)) {
  221. $this->thenWeMustCallMethodForException($mock);
  222. } else {
  223. $this->thenWeDontCallAnyMethodForExceptionJustVerify($mock);
  224. }
  225. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  226. $this->assertSame($failMessage, $e->getMessage());
  227. }
  228. $this->resetMockObjects();
  229. }
  230. private function thenWeMustCallMethodForException($mock)
  231. {
  232. $mock->call();
  233. }
  234. private function thenWeDontCallAnyMethodForExceptionJustVerify($mock)
  235. {
  236. $mock->__phpunit_verify();
  237. $this->fail('Expected exception');
  238. }
  239. private function thereAreNeverMatcher($stubMarshaler)
  240. {
  241. $matcher = $stubMarshaler->getMatcher();
  242. return 0 == $matcher->getInvocationCount();
  243. }
  244. private function resetMockObjects()
  245. {
  246. $refl = new ReflectionObject($this);
  247. $refl = $refl->getParentClass();
  248. $prop = $refl->getProperty('mockObjects');
  249. $prop->setAccessible(true);
  250. $prop->setValue($this, array());
  251. }
  252. public static function matcherProvider()
  253. {
  254. return array(
  255. array(0, Stub::never()),
  256. array(1, Stub::once()),
  257. array(2, Stub::atLeastOnce()),
  258. array(3, Stub::exactly(3)),
  259. array(1, Stub::once(function () {
  260. return true;
  261. }), true),
  262. array(2, Stub::atLeastOnce(function () {
  263. return array();
  264. }), array()),
  265. array(1, Stub::exactly(1, function () {
  266. return null;
  267. }), null),
  268. array(1, Stub::exactly(1, function () {
  269. return 'hello world!';
  270. }), 'hello world!'),
  271. );
  272. }
  273. /**
  274. * @dataProvider matcherProvider
  275. */
  276. public function testMethodMatcherWithMake($count, $matcher, $expected = false)
  277. {
  278. $dummy = Stub::make('DummyClass', array('goodByeWorld' => $matcher), $this);
  279. $this->repeatCall($count, array($dummy, 'goodByeWorld'), $expected);
  280. }
  281. /**
  282. * @dataProvider matcherProvider
  283. */
  284. public function testMethodMatcherWithMakeEmpty($count, $matcher)
  285. {
  286. $dummy = Stub::makeEmpty('DummyClass', array('goodByeWorld' => $matcher), $this);
  287. $this->repeatCall($count, array($dummy, 'goodByeWorld'));
  288. }
  289. /**
  290. * @dataProvider matcherProvider
  291. */
  292. public function testMethodMatcherWithMakeEmptyExcept($count, $matcher)
  293. {
  294. $dummy = Stub::makeEmptyExcept('DummyClass', 'getCheckMe', array('goodByeWorld' => $matcher), $this);
  295. $this->repeatCall($count, array($dummy, 'goodByeWorld'));
  296. }
  297. /**
  298. * @dataProvider matcherProvider
  299. */
  300. public function testMethodMatcherWithConstruct($count, $matcher)
  301. {
  302. $dummy = Stub::construct('DummyClass', array(), array('goodByeWorld' => $matcher), $this);
  303. $this->repeatCall($count, array($dummy, 'goodByeWorld'));
  304. }
  305. /**
  306. * @dataProvider matcherProvider
  307. */
  308. public function testMethodMatcherWithConstructEmpty($count, $matcher)
  309. {
  310. $dummy = Stub::constructEmpty('DummyClass', array(), array('goodByeWorld' => $matcher), $this);
  311. $this->repeatCall($count, array($dummy, 'goodByeWorld'));
  312. }
  313. /**
  314. * @dataProvider matcherProvider
  315. */
  316. public function testMethodMatcherWithConstructEmptyExcept($count, $matcher)
  317. {
  318. $dummy = Stub::constructEmptyExcept(
  319. 'DummyClass',
  320. 'getCheckMe',
  321. array(),
  322. array('goodByeWorld' => $matcher),
  323. $this
  324. );
  325. $this->repeatCall($count, array($dummy, 'goodByeWorld'));
  326. }
  327. private function repeatCall($count, $callable, $expected = false)
  328. {
  329. for ($i = 0; $i < $count; $i++) {
  330. $actual = call_user_func($callable);
  331. if ($expected) {
  332. $this->assertEquals($expected, $actual);
  333. }
  334. }
  335. }
  336. public function testConsecutive()
  337. {
  338. $dummy = Stub::make('DummyClass', array('helloWorld' => Stub::consecutive('david', 'emma', 'sam', 'amy')));
  339. $this->assertEquals('david', $dummy->helloWorld());
  340. $this->assertEquals('emma', $dummy->helloWorld());
  341. $this->assertEquals('sam', $dummy->helloWorld());
  342. $this->assertEquals('amy', $dummy->helloWorld());
  343. // Expected null value when no more values
  344. $this->assertNull($dummy->helloWorld());
  345. }
  346. public function testStubPrivateProperties()
  347. {
  348. $tester = Stub::construct(
  349. 'MyClassWithPrivateProperties',
  350. ['name' => 'gamma'],
  351. [
  352. 'randomName' => 'chicken',
  353. 't' => 'ticky2',
  354. 'getRandomName' => function () {
  355. return "randomstuff";
  356. }
  357. ]
  358. );
  359. $this->assertEquals('gamma', $tester->getName());
  360. $this->assertEquals('randomstuff', $tester->getRandomName());
  361. $this->assertEquals('ticky2', $tester->getT());
  362. }
  363. public function testStubMakeEmptyInterface()
  364. {
  365. $stub = Stub::makeEmpty('\Countable', ['count' => 5]);
  366. $this->assertEquals(5, $stub->count());
  367. }
  368. }
  369. class MyClassWithPrivateProperties
  370. {
  371. private $name;
  372. private $randomName = "gaia";
  373. private $t = "ticky";
  374. public function __construct($name)
  375. {
  376. $this->name = $name;
  377. }
  378. public function getName()
  379. {
  380. return $this->name;
  381. }
  382. public function getRandomName()
  383. {
  384. return $this->randomName;
  385. }
  386. public function getT()
  387. {
  388. return $this->t;
  389. }
  390. }