PageRenderTime 37ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/units/classes/mock/generator.php

http://github.com/mageekguy/atoum
PHP | 2517 lines | 2493 code | 24 blank | 0 comment | 7 complexity | 527b1da235289d5029b5f1d5b0b4695c MD5 | raw file
  1. <?php
  2. namespace mageekguy\atoum\tests\units\mock;
  3. use
  4. mageekguy\atoum,
  5. mageekguy\atoum\mock,
  6. mageekguy\atoum\test\adapter\call\decorators,
  7. mageekguy\atoum\mock\generator as testedClass
  8. ;
  9. require_once __DIR__ . '/../../runner.php';
  10. class generator extends atoum\test
  11. {
  12. public function test__construct()
  13. {
  14. $this
  15. ->if($generator = new testedClass())
  16. ->then
  17. ->object($generator->getAdapter())->isEqualTo(new atoum\adapter())
  18. ->boolean($generator->callsToParentClassAreShunted())->isFalse()
  19. ;
  20. }
  21. public function testSetAdapter()
  22. {
  23. $this
  24. ->if($generator = new testedClass())
  25. ->then
  26. ->object($generator->setAdapter($adapter = new atoum\adapter()))->isIdenticalTo($generator)
  27. ->object($generator->getAdapter())->isIdenticalTo($adapter)
  28. ->object($generator->setAdapter())->isIdenticalTo($generator)
  29. ->object($generator->getAdapter())
  30. ->isInstanceOf('mageekguy\atoum\adapter')
  31. ->isNotIdenticalTo($adapter)
  32. ->isEqualTo(new atoum\adapter())
  33. ;
  34. }
  35. public function testSetReflectionClassFactory()
  36. {
  37. $this
  38. ->if($generator = new testedClass())
  39. ->then
  40. ->object($generator->setReflectionClassFactory($factory = function() {}))->isIdenticalTo($generator)
  41. ->object($generator->getReflectionClassFactory())->isIdenticalTo($factory)
  42. ->object($generator->setReflectionClassFactory())->isIdenticalTo($generator)
  43. ->object($defaultReflectionClassFactory = $generator->getReflectionClassFactory())
  44. ->isInstanceOf('closure')
  45. ->isNotIdenticalTo($factory)
  46. ->object($defaultReflectionClassFactory($this))->isEqualTo(new \reflectionClass($this))
  47. ;
  48. }
  49. public function testSetDefaultNamespace()
  50. {
  51. $this
  52. ->if($generator = new testedClass())
  53. ->then
  54. ->object($generator->setDefaultNamespace($namespace = uniqid()))->isIdenticalTo($generator)
  55. ->string($generator->getDefaultNamespace())->isEqualTo($namespace)
  56. ->object($generator->setDefaultNamespace('\\' . $namespace))->isIdenticalTo($generator)
  57. ->string($generator->getDefaultNamespace())->isEqualTo($namespace)
  58. ->object($generator->setDefaultNamespace('\\' . $namespace . '\\'))->isIdenticalTo($generator)
  59. ->string($generator->getDefaultNamespace())->isEqualTo($namespace)
  60. ->object($generator->setDefaultNamespace($namespace . '\\'))->isIdenticalTo($generator)
  61. ->string($generator->getDefaultNamespace())->isEqualTo($namespace)
  62. ;
  63. }
  64. public function testShuntCallsToParentClass()
  65. {
  66. $this
  67. ->if($generator = new testedClass())
  68. ->then
  69. ->object($generator->shuntParentClassCalls())->isIdenticalTo($generator)
  70. ->boolean($generator->callsToParentClassAreShunted())->isTrue()
  71. ;
  72. }
  73. public function testUnshuntParentClassCalls()
  74. {
  75. $this
  76. ->if($generator = new testedClass())
  77. ->then
  78. ->object($generator->unshuntParentClassCalls())->isIdenticalTo($generator)
  79. ->boolean($generator->callsToParentClassAreShunted())->isFalse()
  80. ->if($generator->shuntParentClassCalls())
  81. ->then
  82. ->object($generator->unshuntParentClassCalls())->isIdenticalTo($generator)
  83. ->boolean($generator->callsToParentClassAreShunted())->isFalse()
  84. ;
  85. }
  86. function testAllIsInterface()
  87. {
  88. $this
  89. ->if($generator = new testedClass())
  90. ->then
  91. ->object($generator->allIsInterface())->isIdenticalTo($generator)
  92. ;
  93. }
  94. function testTestedClassIs()
  95. {
  96. $this
  97. ->if($generator = new testedClass())
  98. ->then
  99. ->object($generator->testedClassIs(uniqid()))->isIdenticalTo($generator)
  100. ;
  101. }
  102. public function testOverload()
  103. {
  104. $this
  105. ->if($generator = new testedClass())
  106. ->then
  107. ->object($generator->overload(new mock\php\method($method = uniqid())))->isIdenticalTo($generator)
  108. ->boolean($generator->isOverloaded($method))->isTrue()
  109. ;
  110. }
  111. public function testIsOverloaded()
  112. {
  113. $this
  114. ->if($generator = new testedClass())
  115. ->then
  116. ->boolean($generator->isOverloaded(uniqid()))->isFalse()
  117. ->if($generator->overload(new mock\php\method($method = uniqid())))
  118. ->then
  119. ->boolean($generator->isOverloaded($method))->isTrue()
  120. ;
  121. }
  122. public function testGetOverload()
  123. {
  124. $this
  125. ->if($generator = new testedClass())
  126. ->then
  127. ->variable($generator->getOverload(uniqid()))->isNull()
  128. ->if($generator->overload($overload = new mock\php\method(uniqid())))
  129. ->then
  130. ->object($generator->getOverload($overload->getName()))->isIdenticalTo($overload)
  131. ;
  132. }
  133. public function testShunt()
  134. {
  135. $this
  136. ->if($generator = new testedClass())
  137. ->then
  138. ->object($generator->shunt($method = uniqid()))->isIdenticalTo($generator)
  139. ->boolean($generator->isShunted($method))->isTrue()
  140. ->boolean($generator->isShunted(strtoupper($method)))->isTrue()
  141. ->boolean($generator->isShunted(strtolower($method)))->isTrue()
  142. ->boolean($generator->isShunted(uniqid()))->isFalse()
  143. ;
  144. }
  145. public function testDisallowUndefinedMethodUsage()
  146. {
  147. $this
  148. ->if($generator = new testedClass())
  149. ->then
  150. ->object($generator->disallowUndefinedMethodUsage())->isIdenticalTo($generator)
  151. ;
  152. }
  153. public function testOrphanize()
  154. {
  155. $this
  156. ->if($generator = new testedClass())
  157. ->then
  158. ->object($generator->orphanize($method = uniqid()))->isIdenticalTo($generator)
  159. ->boolean($generator->isOrphanized($method))->isTrue()
  160. ->boolean($generator->isShunted($method))->isTrue()
  161. ;
  162. }
  163. public function testGetMockedClassCodeForUnknownClass()
  164. {
  165. $this
  166. ->if($generator = new testedClass())
  167. ->and($adapter = new atoum\test\adapter())
  168. ->and($adapter->class_exists = false)
  169. ->and($generator->setAdapter($adapter))
  170. ->then
  171. ->string($generator->getMockedClassCode($unknownClass = uniqid()))->isEqualTo(
  172. 'namespace mock {' . PHP_EOL .
  173. 'final class ' . $unknownClass . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  174. '{' . PHP_EOL .
  175. $this->getMockControllerMethods() .
  176. "\t" . 'public function __construct(\mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  177. "\t" . '{' . PHP_EOL .
  178. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  179. "\t\t" . '{' . PHP_EOL .
  180. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  181. "\t\t" . '}' . PHP_EOL .
  182. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  183. "\t\t" . '{' . PHP_EOL .
  184. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  185. "\t\t" . '}' . PHP_EOL .
  186. "\t\t" . '$this->getMockController()->disableMethodChecking();' . PHP_EOL .
  187. "\t\t" . 'if (isset($this->getMockController()->__construct) === true)' . PHP_EOL .
  188. "\t\t" . '{' . PHP_EOL .
  189. "\t\t\t" . '$this->getMockController()->invoke(\'__construct\', func_get_args());' . PHP_EOL .
  190. "\t\t" . '}' . PHP_EOL .
  191. "\t" . '}' . PHP_EOL .
  192. "\t" . 'public function __call($methodName, $arguments)' . PHP_EOL .
  193. "\t" . '{' . PHP_EOL .
  194. "\t\t" . 'if (isset($this->getMockController()->{$methodName}) === true)' . PHP_EOL .
  195. "\t\t" . '{' . PHP_EOL .
  196. "\t\t\t" . '$return = $this->getMockController()->invoke($methodName, $arguments);' . PHP_EOL .
  197. "\t\t\t" . 'return $return;' . PHP_EOL .
  198. "\t\t" . '}' . PHP_EOL .
  199. "\t\t" . 'else' . PHP_EOL .
  200. "\t\t" . '{' . PHP_EOL .
  201. "\t\t\t" . '$this->getMockController()->addCall($methodName, $arguments);' . PHP_EOL .
  202. "\t\t" . '}' . PHP_EOL .
  203. "\t" . '}' . PHP_EOL .
  204. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  205. "\t" . '{' . PHP_EOL .
  206. "\t\t" . 'return ' . var_export(array('__call'), true) . ';' . PHP_EOL .
  207. "\t" . '}' . PHP_EOL .
  208. '}' . PHP_EOL .
  209. '}'
  210. )
  211. ->if($unknownClass = __NAMESPACE__ . '\dummy')
  212. ->and($generator->generate($unknownClass))
  213. ->and($mockedUnknownClass = '\mock\\' . $unknownClass)
  214. ->and($dummy = new $mockedUnknownClass())
  215. ->and($dummyController = new atoum\mock\controller())
  216. ->and($dummyController->notControlNextNewMock())
  217. ->and($calls = new \mock\mageekguy\atoum\test\adapter\calls())
  218. ->and($dummyController->setCalls($calls))
  219. ->and($dummyController->control($dummy))
  220. ->then
  221. ->when(function() use ($dummy) { $dummy->bar(); })
  222. ->mock($calls)->call('addCall')->withArguments(new atoum\test\adapter\call('bar', array(), new decorators\addClass($dummy)))->once()
  223. ->when(function() use ($dummy) { $dummy->bar(); })
  224. ->mock($calls)->call('addCall')->withArguments(new atoum\test\adapter\call('bar', array(), new decorators\addClass($dummy)))->twice()
  225. ;
  226. }
  227. public function testGetMockedClassCodeForRealClass()
  228. {
  229. $this
  230. ->if($generator = new testedClass())
  231. ->and($reflectionMethodController = new mock\controller())
  232. ->and($reflectionMethodController->__construct = function() {})
  233. ->and($reflectionMethodController->getName = '__construct')
  234. ->and($reflectionMethodController->isConstructor = true)
  235. ->and($reflectionMethodController->getParameters = array())
  236. ->and($reflectionMethodController->isPublic = true)
  237. ->and($reflectionMethodController->isProtected = false)
  238. ->and($reflectionMethodController->isPrivate = false)
  239. ->and($reflectionMethodController->isFinal = false)
  240. ->and($reflectionMethodController->isStatic = false)
  241. ->and($reflectionMethodController->isAbstract = false)
  242. ->and($reflectionMethodController->returnsReference = false)
  243. ->and($reflectionMethod = new \mock\reflectionMethod(null, null))
  244. ->and($reflectionClassController = new mock\controller())
  245. ->and($reflectionClassController->__construct = function() {})
  246. ->and($reflectionClassController->getName = function() use (& $realClass) { return $realClass; })
  247. ->and($reflectionClassController->isFinal = false)
  248. ->and($reflectionClassController->isInterface = false)
  249. ->and($reflectionClassController->isAbstract = false)
  250. ->and($reflectionClassController->getMethods = array($reflectionMethod))
  251. ->and($reflectionClassController->getConstructor = $reflectionMethod)
  252. ->and($reflectionClass = new \mock\reflectionClass(null))
  253. ->and($generator->setReflectionClassFactory(function() use ($reflectionClass) { return $reflectionClass; }))
  254. ->and($adapter = new atoum\test\adapter())
  255. ->and($adapter->class_exists = function($class) use (& $realClass) { return ($class == '\\' . $realClass); })
  256. ->and($generator->setAdapter($adapter))
  257. ->then
  258. ->string($generator->getMockedClassCode($realClass = uniqid()))->isEqualTo(
  259. 'namespace mock {' . PHP_EOL .
  260. 'final class ' . $realClass . ' extends \\' . $realClass . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  261. '{' . PHP_EOL .
  262. $this->getMockControllerMethods() .
  263. "\t" . 'public function __construct(\mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  264. "\t" . '{' . PHP_EOL .
  265. "\t\t" . '$arguments = array_merge(array(), array_slice(func_get_args(), 0, -1));' . PHP_EOL .
  266. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  267. "\t\t" . '{' . PHP_EOL .
  268. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  269. "\t\t" . '}' . PHP_EOL .
  270. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  271. "\t\t" . '{' . PHP_EOL .
  272. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  273. "\t\t" . '}' . PHP_EOL .
  274. "\t\t" . 'if (isset($this->getMockController()->__construct) === true)' . PHP_EOL .
  275. "\t\t" . '{' . PHP_EOL .
  276. "\t\t\t" . '$this->getMockController()->invoke(\'__construct\', $arguments);' . PHP_EOL .
  277. "\t\t" . '}' . PHP_EOL .
  278. "\t\t" . 'else' . PHP_EOL .
  279. "\t\t" . '{' . PHP_EOL .
  280. "\t\t\t" . '$this->getMockController()->addCall(\'__construct\', $arguments);' . PHP_EOL .
  281. "\t\t\t" . 'call_user_func_array(\'parent::__construct\', $arguments);' . PHP_EOL .
  282. "\t\t" . '}' . PHP_EOL .
  283. "\t" . '}' . PHP_EOL .
  284. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  285. "\t" . '{' . PHP_EOL .
  286. "\t\t" . 'return ' . var_export(array('__construct'), true) . ';' . PHP_EOL .
  287. "\t" . '}' . PHP_EOL .
  288. '}' . PHP_EOL .
  289. '}'
  290. )
  291. ;
  292. }
  293. public function testGetMockedClassCodeForRealClassWithDeprecatedConstructor()
  294. {
  295. $this
  296. ->if($generator = new testedClass())
  297. ->and($reflectionMethodController = new mock\controller())
  298. ->and($reflectionMethodController->__construct = function() {})
  299. ->and($reflectionMethodController->getName = $realClass = uniqid())
  300. ->and($reflectionMethodController->isConstructor = true)
  301. ->and($reflectionMethodController->getParameters = array())
  302. ->and($reflectionMethodController->isPublic = true)
  303. ->and($reflectionMethodController->isProtected = false)
  304. ->and($reflectionMethodController->isPrivate = false)
  305. ->and($reflectionMethodController->isFinal = false)
  306. ->and($reflectionMethodController->isStatic = false)
  307. ->and($reflectionMethodController->isAbstract = false)
  308. ->and($reflectionMethodController->returnsReference = false)
  309. ->and($reflectionMethod = new \mock\reflectionMethod(null, null))
  310. ->and($reflectionClassController = new mock\controller())
  311. ->and($reflectionClassController->__construct = function() {})
  312. ->and($reflectionClassController->getName = $realClass)
  313. ->and($reflectionClassController->isFinal = false)
  314. ->and($reflectionClassController->isInterface = false)
  315. ->and($reflectionClassController->isAbstract = false)
  316. ->and($reflectionClassController->getMethods = array($reflectionMethod))
  317. ->and($reflectionClassController->getConstructor = $reflectionMethod)
  318. ->and($reflectionClass = new \mock\reflectionClass(null))
  319. ->and($generator->setReflectionClassFactory(function() use ($reflectionClass) { return $reflectionClass; }))
  320. ->and($adapter = new atoum\test\adapter())
  321. ->and($adapter->class_exists = function($class) use (& $realClass) { return ($class == '\\' . $realClass); })
  322. ->and($generator->setAdapter($adapter))
  323. ->then
  324. ->string($generator->getMockedClassCode($realClass))->isEqualTo(
  325. 'namespace mock {' . PHP_EOL .
  326. 'final class ' . $realClass . ' extends \\' . $realClass . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  327. '{' . PHP_EOL .
  328. $this->getMockControllerMethods() .
  329. "\t" . 'public function __construct(\mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  330. "\t" . '{' . PHP_EOL .
  331. "\t\t" . '$arguments = array_merge(array(), array_slice(func_get_args(), 0, -1));' . PHP_EOL .
  332. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  333. "\t\t" . '{' . PHP_EOL .
  334. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  335. "\t\t" . '}' . PHP_EOL .
  336. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  337. "\t\t" . '{' . PHP_EOL .
  338. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  339. "\t\t" . '}' . PHP_EOL .
  340. "\t\t" . 'if (isset($this->getMockController()->' . $realClass . ') === true)' . PHP_EOL .
  341. "\t\t" . '{' . PHP_EOL .
  342. "\t\t\t" . '$this->getMockController()->invoke(\'' . $realClass . '\', $arguments);' . PHP_EOL .
  343. "\t\t" . '}' . PHP_EOL .
  344. "\t\t" . 'else' . PHP_EOL .
  345. "\t\t" . '{' . PHP_EOL .
  346. "\t\t\t" . '$this->getMockController()->addCall(\'' . $realClass . '\', $arguments);' . PHP_EOL .
  347. "\t\t\t" . 'call_user_func_array(\'parent::' . $realClass . '\', $arguments);' . PHP_EOL .
  348. "\t\t" . '}' . PHP_EOL .
  349. "\t" . '}' . PHP_EOL .
  350. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  351. "\t" . '{' . PHP_EOL .
  352. "\t\t" . 'return ' . var_export(array($realClass), true) . ';' . PHP_EOL .
  353. "\t" . '}' . PHP_EOL .
  354. '}' . PHP_EOL .
  355. '}'
  356. )
  357. ;
  358. }
  359. public function testGetMockedClassCodeForRealClassWithCallsToParentClassShunted()
  360. {
  361. $this
  362. ->if($generator = new testedClass())
  363. ->and($reflectionMethodController = new mock\controller())
  364. ->and($reflectionMethodController->__construct = function() {})
  365. ->and($reflectionMethodController->getName = '__construct')
  366. ->and($reflectionMethodController->isConstructor = true)
  367. ->and($reflectionMethodController->getParameters = array())
  368. ->and($reflectionMethodController->isPublic = true)
  369. ->and($reflectionMethodController->isProtected = false)
  370. ->and($reflectionMethodController->isPrivate = false)
  371. ->and($reflectionMethodController->isFinal = false)
  372. ->and($reflectionMethodController->isStatic = false)
  373. ->and($reflectionMethodController->isAbstract = false)
  374. ->and($reflectionMethodController->returnsReference = false)
  375. ->and($reflectionMethod = new \mock\reflectionMethod(null, null))
  376. ->and($otherReflectionMethodController = new mock\controller())
  377. ->and($otherReflectionMethodController->__construct = function() {})
  378. ->and($otherReflectionMethodController->getName = $otherMethod = uniqid())
  379. ->and($otherReflectionMethodController->isConstructor = false)
  380. ->and($otherReflectionMethodController->getParameters = array())
  381. ->and($otherReflectionMethodController->isPublic = true)
  382. ->and($otherReflectionMethodController->isProtected = false)
  383. ->and($otherReflectionMethodController->isPrivate = false)
  384. ->and($otherReflectionMethodController->isFinal = false)
  385. ->and($otherReflectionMethodController->isStatic = false)
  386. ->and($otherReflectionMethodController->isAbstract = false)
  387. ->and($otherReflectionMethodController->returnsReference = false)
  388. ->and($otherReflectionMethod = new \mock\reflectionMethod(null, null))
  389. ->and($reflectionClassController = new mock\controller())
  390. ->and($reflectionClassController->__construct = function() {})
  391. ->and($reflectionClassController->getName = function() use (& $realClass) { return $realClass; })
  392. ->and($reflectionClassController->isFinal = false)
  393. ->and($reflectionClassController->isInterface = false)
  394. ->and($reflectionClassController->isAbstract = false)
  395. ->and($reflectionClassController->getMethods = array($reflectionMethod, $otherReflectionMethod))
  396. ->and($reflectionClassController->getConstructor = $reflectionMethod)
  397. ->and($reflectionClass = new \mock\reflectionClass(null))
  398. ->and($generator->setReflectionClassFactory(function() use ($reflectionClass) { return $reflectionClass; }))
  399. ->and($adapter = new atoum\test\adapter())
  400. ->and($adapter->class_exists = function($class) use (& $realClass) { return ($class == '\\' . $realClass); })
  401. ->and($generator->setAdapter($adapter))
  402. ->and($generator->shuntParentClassCalls())
  403. ->then
  404. ->string($generator->getMockedClassCode($realClass = uniqid()))->isEqualTo(
  405. 'namespace mock {' . PHP_EOL .
  406. 'final class ' . $realClass . ' extends \\' . $realClass . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  407. '{' . PHP_EOL .
  408. $this->getMockControllerMethods() .
  409. "\t" . 'public function __construct(\mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  410. "\t" . '{' . PHP_EOL .
  411. "\t\t" . '$arguments = array_merge(array(), array_slice(func_get_args(), 0, -1));' . PHP_EOL .
  412. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  413. "\t\t" . '{' . PHP_EOL .
  414. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  415. "\t\t" . '}' . PHP_EOL .
  416. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  417. "\t\t" . '{' . PHP_EOL .
  418. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  419. "\t\t" . '}' . PHP_EOL .
  420. "\t\t" . 'if (isset($this->getMockController()->__construct) === true)' . PHP_EOL .
  421. "\t\t" . '{' . PHP_EOL .
  422. "\t\t\t" . '$this->getMockController()->invoke(\'__construct\', $arguments);' . PHP_EOL .
  423. "\t\t" . '}' . PHP_EOL .
  424. "\t\t" . 'else' . PHP_EOL .
  425. "\t\t" . '{' . PHP_EOL .
  426. "\t\t\t" . '$this->getMockController()->addCall(\'__construct\', $arguments);' . PHP_EOL .
  427. "\t\t" . '}' . PHP_EOL .
  428. "\t" . '}' . PHP_EOL .
  429. "\t" . 'public function ' . $otherMethod . '()' . PHP_EOL .
  430. "\t" . '{' . PHP_EOL .
  431. "\t\t" . '$arguments = array_merge(array(), array_slice(func_get_args(), 0));' . PHP_EOL .
  432. "\t\t" . 'if (isset($this->getMockController()->' . $otherMethod . ') === true)' . PHP_EOL .
  433. "\t\t" . '{' . PHP_EOL .
  434. "\t\t\t" . '$return = $this->getMockController()->invoke(\'' . $otherMethod . '\', $arguments);' . PHP_EOL .
  435. "\t\t\t" . 'return $return;' . PHP_EOL .
  436. "\t\t" . '}' . PHP_EOL .
  437. "\t\t" . 'else' . PHP_EOL .
  438. "\t\t" . '{' . PHP_EOL .
  439. "\t\t\t" . '$this->getMockController()->addCall(\'' . $otherMethod . '\', $arguments);' . PHP_EOL .
  440. "\t\t" . '}' . PHP_EOL .
  441. "\t" . '}' . PHP_EOL .
  442. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  443. "\t" . '{' . PHP_EOL .
  444. "\t\t" . 'return ' . var_export(array('__construct', $otherMethod), true) . ';' . PHP_EOL .
  445. "\t" . '}' . PHP_EOL .
  446. '}' . PHP_EOL .
  447. '}'
  448. )
  449. ;
  450. }
  451. public function testGetMockedClassCodeWithOverloadMethod()
  452. {
  453. $this
  454. ->if($generator = new testedClass())
  455. ->and($reflectionMethodController = new mock\controller())
  456. ->and($reflectionMethodController->__construct = function() {})
  457. ->and($reflectionMethodController->getName = '__construct')
  458. ->and($reflectionMethodController->isConstructor = true)
  459. ->and($reflectionMethodController->getParameters = array())
  460. ->and($reflectionMethodController->isPublic = true)
  461. ->and($reflectionMethodController->isProtected = false)
  462. ->and($reflectionMethodController->isPrivate = false)
  463. ->and($reflectionMethodController->isFinal = false)
  464. ->and($reflectionMethodController->isAbstract = false)
  465. ->and($reflectionMethodController->isStatic = false)
  466. ->and($reflectionMethodController->returnsReference = false)
  467. ->and($reflectionMethod = new \mock\reflectionMethod(null, null))
  468. ->and($reflectionClassController = new mock\controller())
  469. ->and($reflectionClassController->__construct = function() {})
  470. ->and($reflectionClassController->getName = function() use (& $realClass) { return $realClass; })
  471. ->and($reflectionClassController->isFinal = false)
  472. ->and($reflectionClassController->isInterface = false)
  473. ->and($reflectionClassController->getMethods = array($reflectionMethod))
  474. ->and($reflectionClassController->getConstructor = $reflectionMethod)
  475. ->and($reflectionClassController->isAbstract = false)
  476. ->and($reflectionClass = new \mock\reflectionClass(null))
  477. ->and($generator->setReflectionClassFactory(function() use ($reflectionClass) { return $reflectionClass; }))
  478. ->and($adapter = new atoum\test\adapter())
  479. ->and($adapter->class_exists = function($class) use (& $realClass) { return ($class == '\\' . $realClass); })
  480. ->and($generator->setAdapter($adapter))
  481. ->and($overloadedMethod = new mock\php\method('__construct'))
  482. ->and($overloadedMethod->addArgument($argument = new mock\php\method\argument(uniqid())))
  483. ->and($generator->overload($overloadedMethod))
  484. ->then
  485. ->string($generator->getMockedClassCode($realClass = uniqid()))->isEqualTo(
  486. 'namespace mock {' . PHP_EOL .
  487. 'final class ' . $realClass . ' extends \\' . $realClass . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  488. '{' . PHP_EOL .
  489. $this->getMockControllerMethods() .
  490. "\t" . '' . $overloadedMethod . PHP_EOL .
  491. "\t" . '{' . PHP_EOL .
  492. "\t\t" . '$arguments = array_merge(array(' . $argument . '), array_slice(func_get_args(), 1, -1));' . PHP_EOL .
  493. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  494. "\t\t" . '{' . PHP_EOL .
  495. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  496. "\t\t" . '}' . PHP_EOL .
  497. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  498. "\t\t" . '{' . PHP_EOL .
  499. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  500. "\t\t" . '}' . PHP_EOL .
  501. "\t\t" . 'if (isset($this->getMockController()->__construct) === true)' . PHP_EOL .
  502. "\t\t" . '{' . PHP_EOL .
  503. "\t\t\t" . '$this->getMockController()->invoke(\'__construct\', $arguments);' . PHP_EOL .
  504. "\t\t" . '}' . PHP_EOL .
  505. "\t\t" . 'else' . PHP_EOL .
  506. "\t\t" . '{' . PHP_EOL .
  507. "\t\t\t" . '$this->getMockController()->addCall(\'__construct\', $arguments);' . PHP_EOL .
  508. "\t\t\t" . 'call_user_func_array(\'parent::__construct\', $arguments);' . PHP_EOL .
  509. "\t\t" . '}' . PHP_EOL .
  510. "\t" . '}' . PHP_EOL .
  511. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  512. "\t" . '{' . PHP_EOL .
  513. "\t\t" . 'return ' . var_export(array('__construct'), true) . ';' . PHP_EOL .
  514. "\t" . '}' . PHP_EOL .
  515. '}' . PHP_EOL .
  516. '}'
  517. )
  518. ;
  519. }
  520. public function testGetMockedClassCodeWithAbstractMethod()
  521. {
  522. $this
  523. ->if($generator = new testedClass())
  524. ->and($realClass = uniqid())
  525. ->and($reflectionMethodController = new mock\controller())
  526. ->and($reflectionMethodController->__construct = function() {})
  527. ->and($reflectionMethodController->getName = function() { return '__construct'; })
  528. ->and($reflectionMethodController->isConstructor = true)
  529. ->and($reflectionMethodController->getParameters = array())
  530. ->and($reflectionMethodController->isPublic = true)
  531. ->and($reflectionMethodController->isProtected = false)
  532. ->and($reflectionMethodController->isPrivate = false)
  533. ->and($reflectionMethodController->isFinal = false)
  534. ->and($reflectionMethodController->isStatic = false)
  535. ->and($reflectionMethodController->isAbstract = true)
  536. ->and($reflectionMethodController->returnsReference = false)
  537. ->and($reflectionMethod = new \mock\reflectionMethod(null, null))
  538. ->and($reflectionClassController = new mock\controller())
  539. ->and($reflectionClassController->__construct = function() {})
  540. ->and($reflectionClassController->getName = function() use ($realClass) { return $realClass; })
  541. ->and($reflectionClassController->isFinal = false)
  542. ->and($reflectionClassController->isInterface = false)
  543. ->and($reflectionClassController->getMethods = array($reflectionMethod))
  544. ->and($reflectionClassController->getConstructor = $reflectionMethod)
  545. ->and($reflectionClassController->isAbstract = false)
  546. ->and($reflectionClass = new \mock\reflectionClass(null))
  547. ->and($generator->setReflectionClassFactory(function() use ($reflectionClass) { return $reflectionClass; }))
  548. ->and($adapter = new atoum\test\adapter())
  549. ->and($adapter->class_exists = function($class) use ($realClass) { return ($class == '\\' . $realClass); })
  550. ->and($generator->setAdapter($adapter))
  551. ->then
  552. ->string($generator->getMockedClassCode($realClass))->isEqualTo(
  553. 'namespace mock {' . PHP_EOL .
  554. 'final class ' . $realClass . ' extends \\' . $realClass . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  555. '{' . PHP_EOL .
  556. $this->getMockControllerMethods() .
  557. "\t" . 'public function __construct(\mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  558. "\t" . '{' . PHP_EOL .
  559. "\t\t" . '$arguments = array_merge(array(), array_slice(func_get_args(), 0, -1));' . PHP_EOL .
  560. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  561. "\t\t" . '{' . PHP_EOL .
  562. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  563. "\t\t" . '}' . PHP_EOL .
  564. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  565. "\t\t" . '{' . PHP_EOL .
  566. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  567. "\t\t" . '}' . PHP_EOL .
  568. "\t\t" . 'if (isset($this->getMockController()->__construct) === false)' . PHP_EOL .
  569. "\t\t" . '{' . PHP_EOL .
  570. "\t\t\t" . '$this->getMockController()->__construct = function() {};' . PHP_EOL .
  571. "\t\t" . '}' . PHP_EOL .
  572. "\t\t" . '$this->getMockController()->invoke(\'__construct\', $arguments);' . PHP_EOL .
  573. "\t" . '}' . PHP_EOL .
  574. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  575. "\t" . '{' . PHP_EOL .
  576. "\t\t" . 'return ' . var_export(array('__construct'), true) . ';' . PHP_EOL .
  577. "\t" . '}' . PHP_EOL .
  578. '}' . PHP_EOL .
  579. '}'
  580. )
  581. ;
  582. }
  583. public function testGetMockedClassCodeWithShuntedMethod()
  584. {
  585. $this
  586. ->if($generator = new testedClass())
  587. ->and($realClass = uniqid())
  588. ->and($reflectionMethodController = new mock\controller())
  589. ->and($reflectionMethodController->__construct = function() {})
  590. ->and($reflectionMethodController->getName = function() { return '__construct'; })
  591. ->and($reflectionMethodController->isConstructor = true)
  592. ->and($reflectionMethodController->isAbstract = false)
  593. ->and($reflectionMethodController->getParameters = array())
  594. ->and($reflectionMethodController->isPublic = true)
  595. ->and($reflectionMethodController->isProtected = false)
  596. ->and($reflectionMethodController->isPrivate = false)
  597. ->and($reflectionMethodController->isFinal = false)
  598. ->and($reflectionMethodController->isStatic = false)
  599. ->and($reflectionMethodController->returnsReference = false)
  600. ->and($reflectionMethod = new \mock\reflectionMethod(null, null))
  601. ->and($reflectionClassController = new mock\controller())
  602. ->and($reflectionClassController->__construct = function() {})
  603. ->and($reflectionClassController->getName = function() use ($realClass) { return $realClass; })
  604. ->and($reflectionClassController->isFinal = false)
  605. ->and($reflectionClassController->isInterface = false)
  606. ->and($reflectionClassController->getMethods = array($reflectionMethod))
  607. ->and($reflectionClassController->getConstructor = $reflectionMethod)
  608. ->and($reflectionClassController->isAbstract = false)
  609. ->and($reflectionClass = new \mock\reflectionClass(null))
  610. ->and($generator->setReflectionClassFactory(function() use ($reflectionClass) { return $reflectionClass; }))
  611. ->and($adapter = new atoum\test\adapter())
  612. ->and($adapter->class_exists = function($class) use ($realClass) { return ($class == '\\' . $realClass); })
  613. ->and($generator->setAdapter($adapter))
  614. ->and($generator->shunt('__construct'))
  615. ->then
  616. ->string($generator->getMockedClassCode($realClass))->isEqualTo(
  617. 'namespace mock {' . PHP_EOL .
  618. 'final class ' . $realClass . ' extends \\' . $realClass . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  619. '{' . PHP_EOL .
  620. $this->getMockControllerMethods() .
  621. "\t" . 'public function __construct(\mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  622. "\t" . '{' . PHP_EOL .
  623. "\t\t" . '$arguments = array_merge(array(), array_slice(func_get_args(), 0, -1));' . PHP_EOL .
  624. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  625. "\t\t" . '{' . PHP_EOL .
  626. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  627. "\t\t" . '}' . PHP_EOL .
  628. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  629. "\t\t" . '{' . PHP_EOL .
  630. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  631. "\t\t" . '}' . PHP_EOL .
  632. "\t\t" . 'if (isset($this->getMockController()->__construct) === false)' . PHP_EOL .
  633. "\t\t" . '{' . PHP_EOL .
  634. "\t\t\t" . '$this->getMockController()->__construct = function() {};' . PHP_EOL .
  635. "\t\t" . '}' . PHP_EOL .
  636. "\t\t" . '$this->getMockController()->invoke(\'__construct\', $arguments);' . PHP_EOL .
  637. "\t" . '}' . PHP_EOL .
  638. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  639. "\t" . '{' . PHP_EOL .
  640. "\t\t" . 'return ' . var_export(array('__construct'), true) . ';' . PHP_EOL .
  641. "\t" . '}' . PHP_EOL .
  642. '}' . PHP_EOL .
  643. '}'
  644. )
  645. ;
  646. }
  647. public function testGetMockedClassCodeWithAllIsInterface()
  648. {
  649. $this
  650. ->if($generator = new testedClass())
  651. ->and($realClass = uniqid())
  652. ->and($reflectionMethodController = new mock\controller())
  653. ->and($reflectionMethodController->__construct = function() {})
  654. ->and($reflectionMethodController->getName = 'foo')
  655. ->and($reflectionMethodController->isConstructor = false)
  656. ->and($reflectionMethodController->getParameters = array())
  657. ->and($reflectionMethodController->isPublic = true)
  658. ->and($reflectionMethodController->isProtected = false)
  659. ->and($reflectionMethodController->isPrivate = false)
  660. ->and($reflectionMethodController->isFinal = false)
  661. ->and($reflectionMethodController->isAbstract = false)
  662. ->and($reflectionMethodController->isStatic = false)
  663. ->and($reflectionMethodController->returnsReference = false)
  664. ->and($reflectionMethod = new \mock\reflectionMethod(null, null))
  665. ->and($reflectionClassController = new mock\controller())
  666. ->and($reflectionClassController->__construct = function() {})
  667. ->and($reflectionClassController->getName = function() use ($realClass) { return $realClass; })
  668. ->and($reflectionClassController->isFinal = false)
  669. ->and($reflectionClassController->isInterface = false)
  670. ->and($reflectionClassController->getMethods = array($reflectionMethod))
  671. ->and($reflectionClassController->getConstructor = null)
  672. ->and($reflectionClassController->isAbstract = false)
  673. ->and($reflectionClass = new \mock\reflectionClass(null))
  674. ->and($generator->setReflectionClassFactory(function() use ($reflectionClass) { return $reflectionClass; }))
  675. ->and($adapter = new atoum\test\adapter())
  676. ->and($adapter->class_exists = function($class) use ($realClass) { return ($class == '\\' . $realClass); })
  677. ->and($generator->setAdapter($adapter))
  678. ->and($generator->shunt('__construct'))
  679. ->and($generator->allIsInterface())
  680. ->then
  681. ->string($generator->getMockedClassCode($realClass))->isEqualTo(
  682. 'namespace mock {' . PHP_EOL .
  683. 'final class ' . $realClass . ' extends \\' . $realClass . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  684. '{' . PHP_EOL .
  685. $this->getMockControllerMethods() .
  686. "\t" . 'public function __construct(\mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  687. "\t" . '{' . PHP_EOL .
  688. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  689. "\t\t" . '{' . PHP_EOL .
  690. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  691. "\t\t" . '}' . PHP_EOL .
  692. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  693. "\t\t" . '{' . PHP_EOL .
  694. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  695. "\t\t" . '}' . PHP_EOL .
  696. "\t\t" . 'if (isset($this->getMockController()->__construct) === true)' . PHP_EOL .
  697. "\t\t" . '{' . PHP_EOL .
  698. "\t\t\t" . '$this->getMockController()->invoke(\'__construct\', func_get_args());' . PHP_EOL .
  699. "\t\t" . '}' . PHP_EOL .
  700. "\t" . '}' . PHP_EOL .
  701. "\t" . 'public function foo()' . PHP_EOL .
  702. "\t" . '{' . PHP_EOL .
  703. "\t\t" . '$arguments = array_merge(array(), array_slice(func_get_args(), 0));' . PHP_EOL .
  704. "\t\t" . 'if (isset($this->getMockController()->foo) === false)' . PHP_EOL .
  705. "\t\t" . '{' . PHP_EOL .
  706. "\t\t\t" . '$this->getMockController()->foo = function() {};' . PHP_EOL .
  707. "\t\t" . '}' . PHP_EOL .
  708. "\t\t" . '$return = $this->getMockController()->invoke(\'foo\', $arguments);' . PHP_EOL .
  709. "\t\t" . 'return $return;' . PHP_EOL .
  710. "\t" . '}' . PHP_EOL .
  711. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  712. "\t" . '{' . PHP_EOL .
  713. "\t\t" . 'return ' . var_export(array('__construct', 'foo'), true) . ';' . PHP_EOL .
  714. "\t" . '}' . PHP_EOL .
  715. '}' . PHP_EOL .
  716. '}'
  717. )
  718. ->if($generator->testedClassIs($realClass))
  719. ->then
  720. ->string($generator->getMockedClassCode($realClass))->isEqualTo(
  721. 'namespace mock {' . PHP_EOL .
  722. 'final class ' . $realClass . ' extends \\' . $realClass . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  723. '{' . PHP_EOL .
  724. $this->getMockControllerMethods() .
  725. "\t" . 'public function __construct(\mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  726. "\t" . '{' . PHP_EOL .
  727. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  728. "\t\t" . '{' . PHP_EOL .
  729. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  730. "\t\t" . '}' . PHP_EOL .
  731. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  732. "\t\t" . '{' . PHP_EOL .
  733. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  734. "\t\t" . '}' . PHP_EOL .
  735. "\t\t" . 'if (isset($this->getMockController()->__construct) === true)' . PHP_EOL .
  736. "\t\t" . '{' . PHP_EOL .
  737. "\t\t\t" . '$this->getMockController()->invoke(\'__construct\', func_get_args());' . PHP_EOL .
  738. "\t\t" . '}' . PHP_EOL .
  739. "\t" . '}' . PHP_EOL .
  740. "\t" . 'public function foo()' . PHP_EOL .
  741. "\t" . '{' . PHP_EOL .
  742. "\t\t" . '$arguments = array_merge(array(), array_slice(func_get_args(), 0));' . PHP_EOL .
  743. "\t\t" . 'if (isset($this->getMockController()->foo) === true)' . PHP_EOL .
  744. "\t\t" . '{' . PHP_EOL .
  745. "\t\t\t" . '$return = $this->getMockController()->invoke(\'foo\', $arguments);' . PHP_EOL .
  746. "\t\t\t" . 'return $return;' . PHP_EOL .
  747. "\t\t" . '}' . PHP_EOL .
  748. "\t\t" . 'else' . PHP_EOL .
  749. "\t\t" . '{' . PHP_EOL .
  750. "\t\t\t" . '$this->getMockController()->addCall(\'foo\', $arguments);' . PHP_EOL .
  751. "\t\t\t" . '$return = call_user_func_array(\'parent::foo\', $arguments);' . PHP_EOL .
  752. "\t\t\t" . 'return $return;' . PHP_EOL .
  753. "\t\t" . '}' . PHP_EOL .
  754. "\t" . '}' . PHP_EOL .
  755. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  756. "\t" . '{' . PHP_EOL .
  757. "\t\t" . 'return ' . var_export(array('__construct', 'foo'), true) . ';' . PHP_EOL .
  758. "\t" . '}' . PHP_EOL .
  759. '}' . PHP_EOL .
  760. '}'
  761. )
  762. ;
  763. }
  764. public function testGetMockedClassCodeWithCloneMethod()
  765. {
  766. $this
  767. ->if($generator = new testedClass())
  768. ->and($realClass = uniqid())
  769. ->and($reflectionMethodController = new mock\controller())
  770. ->and($reflectionMethodController->__construct = function() {})
  771. ->and($reflectionMethodController->getName = 'clone')
  772. ->and($reflectionMethodController->isConstructor = false)
  773. ->and($reflectionMethodController->isAbstract = false)
  774. ->and($reflectionMethodController->getParameters = array())
  775. ->and($reflectionMethodController->isPublic = true)
  776. ->and($reflectionMethodController->isProtected = false)
  777. ->and($reflectionMethodController->isPrivate = false)
  778. ->and($reflectionMethodController->isFinal = false)
  779. ->and($reflectionMethodController->isStatic = false)
  780. ->and($reflectionMethodController->returnsReference = false)
  781. ->and($reflectionMethod = new \mock\reflectionMethod(null, null))
  782. ->and($reflectionClassController = new mock\controller())
  783. ->and($reflectionClassController->__construct = function() {})
  784. ->and($reflectionClassController->getName = $realClass)
  785. ->and($reflectionClassController->isFinal = false)
  786. ->and($reflectionClassController->isInterface = false)
  787. ->and($reflectionClassController->getMethods = array($reflectionMethod))
  788. ->and($reflectionClassController->getConstructor = null)
  789. ->and($reflectionClassController->isAbstract = false)
  790. ->and($reflectionClass = new \mock\reflectionClass(null))
  791. ->and($generator->setReflectionClassFactory(function() use ($reflectionClass) { return $reflectionClass; }))
  792. ->and($adapter = new atoum\test\adapter())
  793. ->and($adapter->class_exists = function($class) use ($realClass) { return ($class == '\\' . $realClass); })
  794. ->and($generator->setAdapter($adapter))
  795. ->then
  796. ->string($generator->getMockedClassCode($realClass))->isEqualTo(
  797. 'namespace mock {' . PHP_EOL .
  798. 'final class ' . $realClass . ' extends \\' . $realClass . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  799. '{' . PHP_EOL .
  800. $this->getMockControllerMethods() .
  801. "\t" . 'public function __construct(\mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  802. "\t" . '{' . PHP_EOL .
  803. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  804. "\t\t" . '{' . PHP_EOL .
  805. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  806. "\t\t" . '}' . PHP_EOL .
  807. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  808. "\t\t" . '{' . PHP_EOL .
  809. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  810. "\t\t" . '}' . PHP_EOL .
  811. "\t\t" . 'if (isset($this->getMockController()->__construct) === true)' . PHP_EOL .
  812. "\t\t" . '{' . PHP_EOL .
  813. "\t\t\t" . '$this->getMockController()->invoke(\'__construct\', func_get_args());' . PHP_EOL .
  814. "\t\t" . '}' . PHP_EOL .
  815. "\t" . '}' . PHP_EOL .
  816. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  817. "\t" . '{' . PHP_EOL .
  818. "\t\t" . 'return ' . var_export(array('__construct'), true) . ';' . PHP_EOL .
  819. "\t" . '}' . PHP_EOL .
  820. '}' . PHP_EOL .
  821. '}'
  822. )
  823. ;
  824. }
  825. public function testGetMockedClassCodeWithShuntedDeprecatedConstructor()
  826. {
  827. $this
  828. ->if($generator = new testedClass())
  829. ->and($reflectionMethodController = new mock\controller())
  830. ->and($reflectionMethodController->__construct = function() {})
  831. ->and($reflectionMethodController->getName = $realClass = uniqid())
  832. ->and($reflectionMethodController->isConstructor = true)
  833. ->and($reflectionMethodController->isAbstract = false)
  834. ->and($reflectionMethodController->getParameters = array())
  835. ->and($reflectionMethodController->isPublic = true)
  836. ->and($reflectionMethodController->isProtected = false)
  837. ->and($reflectionMethodController->isPrivate = false)
  838. ->and($reflectionMethodController->isFinal = false)
  839. ->and($reflectionMethodController->isStatic = false)
  840. ->and($reflectionMethodController->returnsReference = false)
  841. ->and($reflectionMethod = new \mock\reflectionMethod(null, null))
  842. ->and($reflectionClassController = new mock\controller())
  843. ->and($reflectionClassController->__construct = function() {})
  844. ->and($reflectionClassController->getName = $realClass)
  845. ->and($reflectionClassController->isFinal = false)
  846. ->and($reflectionClassController->isInterface = false)
  847. ->and($reflectionClassController->getMethods = array($reflectionMethod))
  848. ->and($reflectionClassController->getConstructor = $reflectionMethod)
  849. ->and($reflectionClassController->isAbstract = false)
  850. ->and($reflectionClass = new \mock\reflectionClass(null))
  851. ->and($generator->setReflectionClassFactory(function() use ($reflectionClass) { return $reflectionClass; }))
  852. ->and($adapter = new atoum\test\adapter())
  853. ->and($adapter->class_exists = function($class) use ($realClass) { return ($class == '\\' . $realClass); })
  854. ->and($generator->setAdapter($adapter))
  855. ->and($generator->shunt($realClass))
  856. ->then
  857. ->string($generator->getMockedClassCode($realClass))->isEqualTo(
  858. 'namespace mock {' . PHP_EOL .
  859. 'final class ' . $realClass . ' extends \\' . $realClass . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  860. '{' . PHP_EOL .
  861. $this->getMockControllerMethods() .
  862. "\t" . 'public function __construct(\mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  863. "\t" . '{' . PHP_EOL .
  864. "\t\t" . '$arguments = array_merge(array(), array_slice(func_get_args(), 0, -1));' . PHP_EOL .
  865. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  866. "\t\t" . '{' . PHP_EOL .
  867. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  868. "\t\t" . '}' . PHP_EOL .
  869. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  870. "\t\t" . '{' . PHP_EOL .
  871. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  872. "\t\t" . '}' . PHP_EOL .
  873. "\t\t" . 'if (isset($this->getMockController()->' . $realClass . ') === false)' . PHP_EOL .
  874. "\t\t" . '{' . PHP_EOL .
  875. "\t\t\t" . '$this->getMockController()->' . $realClass . ' = function() {};' . PHP_EOL .
  876. "\t\t" . '}' . PHP_EOL .
  877. "\t\t" . '$this->getMockController()->invoke(\'' . $realClass . '\', $arguments);' . PHP_EOL .
  878. "\t" . '}' . PHP_EOL .
  879. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  880. "\t" . '{' . PHP_EOL .
  881. "\t\t" . 'return ' . var_export(array($realClass), true) . ';' . PHP_EOL .
  882. "\t" . '}' . PHP_EOL .
  883. '}' . PHP_EOL .
  884. '}'
  885. )
  886. ;
  887. }
  888. public function testGetMockedClassCodeForInterface()
  889. {
  890. $this
  891. ->if($generator = new testedClass())
  892. ->and($reflectionMethodController = new mock\controller())
  893. ->and($reflectionMethodController->__construct = function() {})
  894. ->and($reflectionMethodController->getName = '__construct')
  895. ->and($reflectionMethodController->isConstructor = true)
  896. ->and($reflectionMethodController->getParameters = array())
  897. ->and($reflectionMethodController->isFinal = false)
  898. ->and($reflectionMethodController->isStatic = false)
  899. ->and($reflectionMethodController->returnsReference = false)
  900. ->and($reflectionMethod = new \mock\reflectionMethod(null, null))
  901. ->and($reflectionClassController = new mock\controller())
  902. ->and($reflectionClassController->__construct = function() {})
  903. ->and($reflectionClassController->getName = function() use (& $realClass) { return $realClass; })
  904. ->and($reflectionClassController->isFinal = false)
  905. ->and($reflectionClassController->isInterface = true)
  906. ->and($reflectionClassController->getMethods = array($reflectionMethod))
  907. ->and($reflectionClassController->isInstantiable = false)
  908. ->and($reflectionClassController->implementsInterface = false)
  909. ->and($reflectionClass = new \mock\reflectionClass(null))
  910. ->and($generator->setReflectionClassFactory(function() use ($reflectionClass) { return $reflectionClass; }))
  911. ->and($adapter = new atoum\test\adapter())
  912. ->and($adapter->class_exists = function($class) use (& $realClass) { return ($class == '\\' . $realClass); })
  913. ->and($generator->setAdapter($adapter))
  914. ->then
  915. ->string($generator->getMockedClassCode($realClass = uniqid()))->isEqualTo(
  916. 'namespace mock {' . PHP_EOL .
  917. 'final class ' . $realClass . ' implements \\' . $realClass . ', \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  918. '{' . PHP_EOL .
  919. $this->getMockControllerMethods() .
  920. "\t" . 'public function __construct(\mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  921. "\t" . '{' . PHP_EOL .
  922. "\t\t" . '$arguments = array_merge(array(), array_slice(func_get_args(), 0, -1));' . PHP_EOL .
  923. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  924. "\t\t" . '{' . PHP_EOL .
  925. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  926. "\t\t" . '}' . PHP_EOL .
  927. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  928. "\t\t" . '{' . PHP_EOL .
  929. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  930. "\t\t" . '}' . PHP_EOL .
  931. "\t\t" . 'if (isset($this->getMockController()->__construct) === false)' . PHP_EOL .
  932. "\t\t" . '{' . PHP_EOL .
  933. "\t\t\t" . '$this->getMockController()->__construct = function() {};' . PHP_EOL .
  934. "\t\t" . '}' . PHP_EOL .
  935. "\t\t" . '$this->getMockController()->invoke(\'__construct\', $arguments);' . PHP_EOL .
  936. "\t" . '}' . PHP_EOL .
  937. "\t" . 'public function __call($methodName, $arguments)' . PHP_EOL .
  938. "\t" . '{' . PHP_EOL .
  939. "\t\t" . 'if (isset($this->getMockController()->{$methodName}) === true)' . PHP_EOL .
  940. "\t\t" . '{' . PHP_EOL .
  941. "\t\t\t" . '$return = $this->getMockController()->invoke($methodName, $arguments);' . PHP_EOL .
  942. "\t\t\t" . 'return $return;' . PHP_EOL .
  943. "\t\t" . '}' . PHP_EOL .
  944. "\t\t" . 'else' . PHP_EOL .
  945. "\t\t" . '{' . PHP_EOL .
  946. "\t\t\t" . '$this->getMockController()->addCall($methodName, $arguments);' . PHP_EOL .
  947. "\t\t" . '}' . PHP_EOL .
  948. "\t" . '}' . PHP_EOL .
  949. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  950. "\t" . '{' . PHP_EOL .
  951. "\t\t" . 'return ' . var_export(array('__construct', '__call'), true) . ';' . PHP_EOL .
  952. "\t" . '}' . PHP_EOL .
  953. '}' . PHP_EOL .
  954. '}'
  955. )
  956. ->if($reflectionClassController->implementsInterface = function($interface) { return ($interface == 'traversable' ? true : false); })
  957. ->and($generator->setReflectionClassFactory(function($class) use ($reflectionClass) { return ($class == 'iteratorAggregate' ? new \reflectionClass('iteratorAggregate') : $reflectionClass); }))
  958. ->then
  959. ->string($generator->getMockedClassCode($realClass = uniqid()))->isEqualTo(
  960. 'namespace mock {' . PHP_EOL .
  961. 'final class ' . $realClass . ' implements \\iteratorAggregate, \\' . $realClass . ', \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  962. '{' . PHP_EOL .
  963. $this->getMockControllerMethods() .
  964. "\t" . 'public function __construct(\mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  965. "\t" . '{' . PHP_EOL .
  966. "\t\t" . '$arguments = array_merge(array(), array_slice(func_get_args(), 0, -1));' . PHP_EOL .
  967. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  968. "\t\t" . '{' . PHP_EOL .
  969. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  970. "\t\t" . '}' . PHP_EOL .
  971. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  972. "\t\t" . '{' . PHP_EOL .
  973. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  974. "\t\t" . '}' . PHP_EOL .
  975. "\t\t" . 'if (isset($this->getMockController()->__construct) === false)' . PHP_EOL .
  976. "\t\t" . '{' . PHP_EOL .
  977. "\t\t\t" . '$this->getMockController()->__construct = function() {};' . PHP_EOL .
  978. "\t\t" . '}' . PHP_EOL .
  979. "\t\t" . '$this->getMockController()->invoke(\'__construct\', $arguments);' . PHP_EOL .
  980. "\t" . '}' . PHP_EOL .
  981. "\t" . 'public function getIterator()' . PHP_EOL .
  982. "\t" . '{' . PHP_EOL .
  983. "\t\t" . '$arguments = array_merge(array(), array_slice(func_get_args(), 0));' . PHP_EOL .
  984. "\t\t" . 'if (isset($this->getMockController()->getIterator) === false)' . PHP_EOL .
  985. "\t\t" . '{' . PHP_EOL .
  986. "\t\t\t" . '$this->getMockController()->getIterator = function() {};' . PHP_EOL .
  987. "\t\t" . '}' . PHP_EOL .
  988. "\t\t" . '$return = $this->getMockController()->invoke(\'getIterator\', $arguments);' . PHP_EOL .
  989. "\t\t" . 'return $return;' . PHP_EOL .
  990. "\t" . '}' . PHP_EOL .
  991. "\t" . 'public function __call($methodName, $arguments)' . PHP_EOL .
  992. "\t" . '{' . PHP_EOL .
  993. "\t\t" . 'if (isset($this->getMockController()->{$methodName}) === true)' . PHP_EOL .
  994. "\t\t" . '{' . PHP_EOL .
  995. "\t\t\t" . '$return = $this->getMockController()->invoke($methodName, $arguments);' . PHP_EOL .
  996. "\t\t\t" . 'return $return;' . PHP_EOL .
  997. "\t\t" . '}' . PHP_EOL .
  998. "\t\t" . 'else' . PHP_EOL .
  999. "\t\t" . '{' . PHP_EOL .
  1000. "\t\t\t" . '$this->getMockController()->addCall($methodName, $arguments);' . PHP_EOL .
  1001. "\t\t" . '}' . PHP_EOL .
  1002. "\t" . '}' . PHP_EOL .
  1003. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  1004. "\t" . '{' . PHP_EOL .
  1005. "\t\t" . 'return ' . var_export(array('__construct', 'getiterator', '__call'), true) . ';' . PHP_EOL .
  1006. "\t" . '}' . PHP_EOL .
  1007. '}' . PHP_EOL .
  1008. '}'
  1009. )
  1010. ->if($generator = new testedClass())
  1011. ->and($reflectionMethodController = new mock\controller())
  1012. ->and($reflectionMethodController->__construct = function() {})
  1013. ->and($reflectionMethodController->getName = '__construct')
  1014. ->and($reflectionMethodController->isConstructor = true)
  1015. ->and($reflectionMethodController->getParameters = array())
  1016. ->and($reflectionMethodController->isFinal = false)
  1017. ->and($reflectionMethodController->isStatic = false)
  1018. ->and($reflectionMethodController->returnsReference = false)
  1019. ->and($reflectionMethod = new \mock\reflectionMethod(null, null))
  1020. ->and($reflectionClassController = new mock\controller())
  1021. ->and($reflectionClassController->__construct = function() {})
  1022. ->and($reflectionClassController->getName = function() use (& $realClass) { return $realClass; })
  1023. ->and($reflectionClassController->isFinal = false)
  1024. ->and($reflectionClassController->isInterface = true)
  1025. ->and($reflectionClassController->getMethods = array($reflectionMethod))
  1026. ->and($reflectionClassController->isInstantiable = false)
  1027. ->and($reflectionClassController->implementsInterface = false)
  1028. ->and($reflectionClass = new \mock\reflectionClass(null))
  1029. ->and($generator->setReflectionClassFactory(function() use ($reflectionClass) { return $reflectionClass; }))
  1030. ->and($adapter = new atoum\test\adapter())
  1031. ->and($adapter->class_exists = function($class) use (& $realClass) { return ($class == '\\' . $realClass); })
  1032. ->and($generator->setAdapter($adapter))
  1033. ->and($generator->disallowUndefinedMethodUsage())
  1034. ->then
  1035. ->string($generator->getMockedClassCode($realClass = uniqid()))->isEqualTo(
  1036. 'namespace mock {' . PHP_EOL .
  1037. 'final class ' . $realClass . ' implements \\' . $realClass . ', \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  1038. '{' . PHP_EOL .
  1039. $this->getMockControllerMethods() .
  1040. "\t" . 'public function __construct(\mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  1041. "\t" . '{' . PHP_EOL .
  1042. "\t\t" . '$arguments = array_merge(array(), array_slice(func_get_args(), 0, -1));' . PHP_EOL .
  1043. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  1044. "\t\t" . '{' . PHP_EOL .
  1045. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  1046. "\t\t" . '}' . PHP_EOL .
  1047. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  1048. "\t\t" . '{' . PHP_EOL .
  1049. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  1050. "\t\t" . '}' . PHP_EOL .
  1051. "\t\t" . 'if (isset($this->getMockController()->__construct) === false)' . PHP_EOL .
  1052. "\t\t" . '{' . PHP_EOL .
  1053. "\t\t\t" . '$this->getMockController()->__construct = function() {};' . PHP_EOL .
  1054. "\t\t" . '}' . PHP_EOL .
  1055. "\t\t" . '$this->getMockController()->invoke(\'__construct\', $arguments);' . PHP_EOL .
  1056. "\t" . '}' . PHP_EOL .
  1057. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  1058. "\t" . '{' . PHP_EOL .
  1059. "\t\t" . 'return ' . var_export(array('__construct'), true) . ';' . PHP_EOL .
  1060. "\t" . '}' . PHP_EOL .
  1061. '}' . PHP_EOL .
  1062. '}'
  1063. )
  1064. ;
  1065. }
  1066. /** @php < 5.6 */
  1067. public function testGetMockedClassCodeForInterfaceWithConstructorArguments()
  1068. {
  1069. $this
  1070. ->if($generator = new testedClass())
  1071. ->and($reflectionParameterController = new mock\controller())
  1072. ->and($reflectionParameterController->__construct = function() {})
  1073. ->and($reflectionParameterController->isArray = true)
  1074. ->and($reflectionParameterController->getName = 'param')
  1075. ->and($reflectionParameterController->isPassedByReference = false)
  1076. ->and($reflectionParameterController->isDefaultValueAvailable = false)
  1077. ->and($reflectionParameterController->isOptional = false)
  1078. ->and($reflectionParameter = new \mock\reflectionParameter(null, null))
  1079. ->and($reflectionMethodController = new mock\controller())
  1080. ->and($reflectionMethodController->__construct = function() {})
  1081. ->and($reflectionMethodController->getName = '__construct')
  1082. ->and($reflectionMethodController->isConstructor = true)
  1083. ->and($reflectionMethodController->getParameters = array($reflectionParameter))
  1084. ->and($reflectionMethodController->isFinal = false)
  1085. ->and($reflectionMethodController->isStatic = false)
  1086. ->and($reflectionMethodController->returnsReference = false)
  1087. ->and($reflectionMethod = new \mock\reflectionMethod(null, null))
  1088. ->and($reflectionClassController = new mock\controller())
  1089. ->and($reflectionClassController->__construct = function() {})
  1090. ->and($reflectionClassController->getName = function() use (& $realClass) { return $realClass; })
  1091. ->and($reflectionClassController->isFinal = false)
  1092. ->and($reflectionClassController->isInterface = true)
  1093. ->and($reflectionClassController->getMethods = array($reflectionMethod))
  1094. ->and($reflectionClassController->isInstantiable = false)
  1095. ->and($reflectionClassController->implementsInterface = false)
  1096. ->and($reflectionClass = new \mock\reflectionClass(null))
  1097. ->and($generator->setReflectionClassFactory(function() use ($reflectionClass) { return $reflectionClass; }))
  1098. ->and($adapter = new atoum\test\adapter())
  1099. ->and($adapter->class_exists = function($class) use (& $realClass) { return ($class == '\\' . $realClass); })
  1100. ->and($generator->setAdapter($adapter))
  1101. ->then
  1102. ->string($generator->getMockedClassCode($realClass = uniqid()))->isEqualTo(
  1103. 'namespace mock {' . PHP_EOL .
  1104. 'final class ' . $realClass . ' implements \\' . $realClass . ', \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  1105. '{' . PHP_EOL .
  1106. $this->getMockControllerMethods() .
  1107. "\t" . 'public function __construct(array $param, \mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  1108. "\t" . '{' . PHP_EOL .
  1109. "\t\t" . '$arguments = array_merge(array($param), array_slice(func_get_args(), 1, -1));' . PHP_EOL .
  1110. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  1111. "\t\t" . '{' . PHP_EOL .
  1112. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  1113. "\t\t" . '}' . PHP_EOL .
  1114. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  1115. "\t\t" . '{' . PHP_EOL .
  1116. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  1117. "\t\t" . '}' . PHP_EOL .
  1118. "\t\t" . 'if (isset($this->getMockController()->__construct) === false)' . PHP_EOL .
  1119. "\t\t" . '{' . PHP_EOL .
  1120. "\t\t\t" . '$this->getMockController()->__construct = function() {};' . PHP_EOL .
  1121. "\t\t" . '}' . PHP_EOL .
  1122. "\t\t" . '$this->getMockController()->invoke(\'__construct\', $arguments);' . PHP_EOL .
  1123. "\t" . '}' . PHP_EOL .
  1124. "\t" . 'public function __call($methodName, $arguments)' . PHP_EOL .
  1125. "\t" . '{' . PHP_EOL .
  1126. "\t\t" . 'if (isset($this->getMockController()->{$methodName}) === true)' . PHP_EOL .
  1127. "\t\t" . '{' . PHP_EOL .
  1128. "\t\t\t" . '$return = $this->getMockController()->invoke($methodName, $arguments);' . PHP_EOL .
  1129. "\t\t\t" . 'return $return;' . PHP_EOL .
  1130. "\t\t" . '}' . PHP_EOL .
  1131. "\t\t" . 'else' . PHP_EOL .
  1132. "\t\t" . '{' . PHP_EOL .
  1133. "\t\t\t" . '$this->getMockController()->addCall($methodName, $arguments);' . PHP_EOL .
  1134. "\t\t" . '}' . PHP_EOL .
  1135. "\t" . '}' . PHP_EOL .
  1136. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  1137. "\t" . '{' . PHP_EOL .
  1138. "\t\t" . 'return ' . var_export(array('__construct', '__call'), true) . ';' . PHP_EOL .
  1139. "\t" . '}' . PHP_EOL .
  1140. '}' . PHP_EOL .
  1141. '}'
  1142. )
  1143. ;
  1144. }
  1145. /** @php 5.6 */
  1146. public function testGetMockedClassCodeForInterfaceWithConstructorArgumentsPhp56()
  1147. {
  1148. $this
  1149. ->if($generator = new testedClass())
  1150. ->and($reflectionParameterController = new mock\controller())
  1151. ->and($reflectionParameterController->__construct = function() {})
  1152. ->and($reflectionParameterController->isArray = true)
  1153. ->and($reflectionParameterController->getName = 'param')
  1154. ->and($reflectionParameterController->isPassedByReference = false)
  1155. ->and($reflectionParameterController->isDefaultValueAvailable = false)
  1156. ->and($reflectionParameterController->isOptional = false)
  1157. ->and($reflectionParameterController->isVariadic = false)
  1158. ->and($reflectionParameter = new \mock\reflectionParameter(null, null))
  1159. ->and($reflectionMethodController = new mock\controller())
  1160. ->and($reflectionMethodController->__construct = function() {})
  1161. ->and($reflectionMethodController->getName = '__construct')
  1162. ->and($reflectionMethodController->isConstructor = true)
  1163. ->and($reflectionMethodController->getParameters = array($reflectionParameter))
  1164. ->and($reflectionMethodController->isFinal = false)
  1165. ->and($reflectionMethodController->isStatic = false)
  1166. ->and($reflectionMethodController->returnsReference = false)
  1167. ->and($reflectionMethod = new \mock\reflectionMethod(null, null))
  1168. ->and($reflectionClassController = new mock\controller())
  1169. ->and($reflectionClassController->__construct = function() {})
  1170. ->and($reflectionClassController->getName = function() use (& $realClass) { return $realClass; })
  1171. ->and($reflectionClassController->isFinal = false)
  1172. ->and($reflectionClassController->isInterface = true)
  1173. ->and($reflectionClassController->getMethods = array($reflectionMethod))
  1174. ->and($reflectionClassController->isInstantiable = false)
  1175. ->and($reflectionClassController->implementsInterface = false)
  1176. ->and($reflectionClass = new \mock\reflectionClass(null))
  1177. ->and($generator->setReflectionClassFactory(function() use ($reflectionClass) { return $reflectionClass; }))
  1178. ->and($adapter = new atoum\test\adapter())
  1179. ->and($adapter->class_exists = function($class) use (& $realClass) { return ($class == '\\' . $realClass); })
  1180. ->and($generator->setAdapter($adapter))
  1181. ->then
  1182. ->string($generator->getMockedClassCode($realClass = uniqid()))->isEqualTo(
  1183. 'namespace mock {' . PHP_EOL .
  1184. 'final class ' . $realClass . ' implements \\' . $realClass . ', \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  1185. '{' . PHP_EOL .
  1186. $this->getMockControllerMethods() .
  1187. "\t" . 'public function __construct(array $param, \mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  1188. "\t" . '{' . PHP_EOL .
  1189. "\t\t" . '$arguments = array_merge(array($param), array_slice(func_get_args(), 1, -1));' . PHP_EOL .
  1190. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  1191. "\t\t" . '{' . PHP_EOL .
  1192. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  1193. "\t\t" . '}' . PHP_EOL .
  1194. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  1195. "\t\t" . '{' . PHP_EOL .
  1196. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  1197. "\t\t" . '}' . PHP_EOL .
  1198. "\t\t" . 'if (isset($this->getMockController()->__construct) === false)' . PHP_EOL .
  1199. "\t\t" . '{' . PHP_EOL .
  1200. "\t\t\t" . '$this->getMockController()->__construct = function() {};' . PHP_EOL .
  1201. "\t\t" . '}' . PHP_EOL .
  1202. "\t\t" . '$this->getMockController()->invoke(\'__construct\', $arguments);' . PHP_EOL .
  1203. "\t" . '}' . PHP_EOL .
  1204. "\t" . 'public function __call($methodName, $arguments)' . PHP_EOL .
  1205. "\t" . '{' . PHP_EOL .
  1206. "\t\t" . 'if (isset($this->getMockController()->{$methodName}) === true)' . PHP_EOL .
  1207. "\t\t" . '{' . PHP_EOL .
  1208. "\t\t\t" . '$return = $this->getMockController()->invoke($methodName, $arguments);' . PHP_EOL .
  1209. "\t\t\t" . 'return $return;' . PHP_EOL .
  1210. "\t\t" . '}' . PHP_EOL .
  1211. "\t\t" . 'else' . PHP_EOL .
  1212. "\t\t" . '{' . PHP_EOL .
  1213. "\t\t\t" . '$this->getMockController()->addCall($methodName, $arguments);' . PHP_EOL .
  1214. "\t\t" . '}' . PHP_EOL .
  1215. "\t" . '}' . PHP_EOL .
  1216. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  1217. "\t" . '{' . PHP_EOL .
  1218. "\t\t" . 'return ' . var_export(array('__construct', '__call'), true) . ';' . PHP_EOL .
  1219. "\t" . '}' . PHP_EOL .
  1220. '}' . PHP_EOL .
  1221. '}'
  1222. )
  1223. ;
  1224. }
  1225. public function testGetMockedClassCodeForInterfaceWithStaticMethod()
  1226. {
  1227. $this
  1228. ->if($generator = new testedClass())
  1229. ->and($reflectionMethodController = new mock\controller())
  1230. ->and($reflectionMethodController->__construct = function() {})
  1231. ->and($reflectionMethodController->getName = $methodName = uniqid())
  1232. ->and($reflectionMethodController->isConstructor = false)
  1233. ->and($reflectionMethodController->getParameters = array())
  1234. ->and($reflectionMethodController->isFinal = false)
  1235. ->and($reflectionMethodController->isStatic = true)
  1236. ->and($reflectionMethodController->returnsReference = false)
  1237. ->and($reflectionMethod = new \mock\reflectionMethod(null, null))
  1238. ->and($reflectionClassController = new mock\controller())
  1239. ->and($reflectionClassController->__construct = function() {})
  1240. ->and($reflectionClassController->getName = function() use (& $realClass) { return $realClass; })
  1241. ->and($reflectionClassController->isFinal = false)
  1242. ->and($reflectionClassController->isInterface = true)
  1243. ->and($reflectionClassController->getMethods = array($reflectionMethod))
  1244. ->and($reflectionClassController->isInstantiable = false)
  1245. ->and($reflectionClassController->implementsInterface = false)
  1246. ->and($reflectionClass = new \mock\reflectionClass(null))
  1247. ->and($generator->setReflectionClassFactory(function() use ($reflectionClass) { return $reflectionClass; }))
  1248. ->and($adapter = new atoum\test\adapter())
  1249. ->and($adapter->class_exists = function($class) use (& $realClass) { return ($class == '\\' . $realClass); })
  1250. ->and($generator->setAdapter($adapter))
  1251. ->then
  1252. ->string($generator->getMockedClassCode($realClass = uniqid()))->isEqualTo(
  1253. 'namespace mock {' . PHP_EOL .
  1254. 'final class ' . $realClass . ' implements \\' . $realClass . ', \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  1255. '{' . PHP_EOL .
  1256. $this->getMockControllerMethods() .
  1257. "\t" . 'public static function ' . $methodName . '()' . PHP_EOL .
  1258. "\t" . '{' . PHP_EOL .
  1259. "\t\t" . '$arguments = array_merge(array(), array_slice(func_get_args(), 0, -1));' . PHP_EOL .
  1260. "\t\t" . 'return call_user_func_array(array(\'parent\', \'' . $methodName . '\'), $arguments);' . PHP_EOL .
  1261. "\t" . '}' . PHP_EOL .
  1262. "\t" . 'public function __construct(\mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  1263. "\t" . '{' . PHP_EOL .
  1264. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  1265. "\t\t" . '{' . PHP_EOL .
  1266. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  1267. "\t\t" . '}' . PHP_EOL .
  1268. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  1269. "\t\t" . '{' . PHP_EOL .
  1270. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  1271. "\t\t" . '}' . PHP_EOL .
  1272. "\t\t" . 'if (isset($this->getMockController()->__construct) === true)' . PHP_EOL .
  1273. "\t\t" . '{' . PHP_EOL .
  1274. "\t\t\t" . '$this->getMockController()->invoke(\'__construct\', func_get_args());' . PHP_EOL .
  1275. "\t\t" . '}' . PHP_EOL .
  1276. "\t" . '}' . PHP_EOL .
  1277. "\t" . 'public function __call($methodName, $arguments)' . PHP_EOL .
  1278. "\t" . '{' . PHP_EOL .
  1279. "\t\t" . 'if (isset($this->getMockController()->{$methodName}) === true)' . PHP_EOL .
  1280. "\t\t" . '{' . PHP_EOL .
  1281. "\t\t\t" . '$return = $this->getMockController()->invoke($methodName, $arguments);' . PHP_EOL .
  1282. "\t\t\t" . 'return $return;' . PHP_EOL .
  1283. "\t\t" . '}' . PHP_EOL .
  1284. "\t\t" . 'else' . PHP_EOL .
  1285. "\t\t" . '{' . PHP_EOL .
  1286. "\t\t\t" . '$this->getMockController()->addCall($methodName, $arguments);' . PHP_EOL .
  1287. "\t\t" . '}' . PHP_EOL .
  1288. "\t" . '}' . PHP_EOL .
  1289. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  1290. "\t" . '{' . PHP_EOL .
  1291. "\t\t" . 'return ' . var_export(array($methodName, '__construct', '__call'), true) . ';' . PHP_EOL .
  1292. "\t" . '}' . PHP_EOL .
  1293. '}' . PHP_EOL .
  1294. '}'
  1295. )
  1296. ->if($reflectionClassController->implementsInterface = function($interface) { return ($interface == 'traversable' ? true : false); })
  1297. ->and($generator->setReflectionClassFactory(function($class) use ($reflectionClass) { return ($class == 'iteratorAggregate' ? new \reflectionClass('iteratorAggregate') : $reflectionClass); }))
  1298. ->then
  1299. ->string($generator->getMockedClassCode($realClass = uniqid()))->isEqualTo(
  1300. 'namespace mock {' . PHP_EOL .
  1301. 'final class ' . $realClass . ' implements \\iteratorAggregate, \\' . $realClass . ', \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  1302. '{' . PHP_EOL .
  1303. $this->getMockControllerMethods() .
  1304. "\t" . 'public static function ' . $methodName . '()' . PHP_EOL .
  1305. "\t" . '{' . PHP_EOL .
  1306. "\t\t" . '$arguments = array_merge(array(), array_slice(func_get_args(), 0, -1));' . PHP_EOL .
  1307. "\t\t" . 'return call_user_func_array(array(\'parent\', \'' . $methodName . '\'), $arguments);' . PHP_EOL .
  1308. "\t" . '}' . PHP_EOL .
  1309. "\t" . 'public function getIterator()' . PHP_EOL .
  1310. "\t" . '{' . PHP_EOL .
  1311. "\t\t" . '$arguments = array_merge(array(), array_slice(func_get_args(), 0));' . PHP_EOL .
  1312. "\t\t" . 'if (isset($this->getMockController()->getIterator) === false)' . PHP_EOL .
  1313. "\t\t" . '{' . PHP_EOL .
  1314. "\t\t\t" . '$this->getMockController()->getIterator = function() {};' . PHP_EOL .
  1315. "\t\t" . '}' . PHP_EOL .
  1316. "\t\t" . '$return = $this->getMockController()->invoke(\'getIterator\', $arguments);' . PHP_EOL .
  1317. "\t\t" . 'return $return;' . PHP_EOL .
  1318. "\t" . '}' . PHP_EOL .
  1319. "\t" . 'public function __construct(\mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  1320. "\t" . '{' . PHP_EOL .
  1321. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  1322. "\t\t" . '{' . PHP_EOL .
  1323. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  1324. "\t\t" . '}' . PHP_EOL .
  1325. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  1326. "\t\t" . '{' . PHP_EOL .
  1327. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  1328. "\t\t" . '}' . PHP_EOL .
  1329. "\t\t" . 'if (isset($this->getMockController()->__construct) === true)' . PHP_EOL .
  1330. "\t\t" . '{' . PHP_EOL .
  1331. "\t\t\t" . '$this->getMockController()->invoke(\'__construct\', func_get_args());' . PHP_EOL .
  1332. "\t\t" . '}' . PHP_EOL .
  1333. "\t" . '}' . PHP_EOL .
  1334. "\t" . 'public function __call($methodName, $arguments)' . PHP_EOL .
  1335. "\t" . '{' . PHP_EOL .
  1336. "\t\t" . 'if (isset($this->getMockController()->{$methodName}) === true)' . PHP_EOL .
  1337. "\t\t" . '{' . PHP_EOL .
  1338. "\t\t\t" . '$return = $this->getMockController()->invoke($methodName, $arguments);' . PHP_EOL .
  1339. "\t\t\t" . 'return $return;' . PHP_EOL .
  1340. "\t\t" . '}' . PHP_EOL .
  1341. "\t\t" . 'else' . PHP_EOL .
  1342. "\t\t" . '{' . PHP_EOL .
  1343. "\t\t\t" . '$this->getMockController()->addCall($methodName, $arguments);' . PHP_EOL .
  1344. "\t\t" . '}' . PHP_EOL .
  1345. "\t" . '}' . PHP_EOL .
  1346. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  1347. "\t" . '{' . PHP_EOL .
  1348. "\t\t" . 'return ' . var_export(array($methodName, 'getiterator', '__construct', '__call'), true) . ';' . PHP_EOL .
  1349. "\t" . '}' . PHP_EOL .
  1350. '}' . PHP_EOL .
  1351. '}'
  1352. )
  1353. ;
  1354. }
  1355. public function testGetMockedClassCodeForRealClassWithoutConstructor()
  1356. {
  1357. $this
  1358. ->if($generator = new testedClass())
  1359. ->and($reflectionMethodController = new mock\controller())
  1360. ->and($reflectionMethodController->__construct = function() {})
  1361. ->and($reflectionMethodController->getName = $methodName = uniqid())
  1362. ->and($reflectionMethodController->isConstructor = false)
  1363. ->and($reflectionMethodController->getParameters = array())
  1364. ->and($reflectionMethodController->isPublic = true)
  1365. ->and($reflectionMethodController->isProtected = false)
  1366. ->and($reflectionMethodController->isPrivate = false)
  1367. ->and($reflectionMethodController->isFinal = false)
  1368. ->and($reflectionMethodController->isAbstract = false)
  1369. ->and($reflectionMethodController->isStatic = false)
  1370. ->and($reflectionMethodController->returnsReference = false)
  1371. ->and($reflectionMethod = new \mock\reflectionMethod(null, null))
  1372. ->and($reflectionClassController = new mock\controller())
  1373. ->and($reflectionClassController->__construct = function() {})
  1374. ->and($reflectionClassController->getName = function() use (& $realClass) { return $realClass; })
  1375. ->and($reflectionClassController->isFinal = false)
  1376. ->and($reflectionClassController->isInterface = false)
  1377. ->and($reflectionClassController->getMethods = array($reflectionMethod))
  1378. ->and($reflectionClassController->getConstructor = null)
  1379. ->and($reflectionClassController->isAbstract = false)
  1380. ->and($reflectionClass = new \mock\reflectionClass(null))
  1381. ->and($generator->setReflectionClassFactory(function() use ($reflectionClass) { return $reflectionClass; }))
  1382. ->and($adapter = new atoum\test\adapter())
  1383. ->and($adapter->class_exists = function($class) use (& $realClass) { return ($class == '\\' . $realClass); })
  1384. ->and($generator->setAdapter($adapter))
  1385. ->then
  1386. ->string($generator->getMockedClassCode($realClass = uniqid()))->isEqualTo(
  1387. 'namespace mock {' . PHP_EOL .
  1388. 'final class ' . $realClass . ' extends \\' . $realClass . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  1389. '{' . PHP_EOL .
  1390. $this->getMockControllerMethods() .
  1391. "\t" . 'public function __construct(\mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  1392. "\t" . '{' . PHP_EOL .
  1393. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  1394. "\t\t" . '{' . PHP_EOL .
  1395. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  1396. "\t\t" . '}' . PHP_EOL .
  1397. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  1398. "\t\t" . '{' . PHP_EOL .
  1399. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  1400. "\t\t" . '}' . PHP_EOL .
  1401. "\t\t" . 'if (isset($this->getMockController()->__construct) === true)' . PHP_EOL .
  1402. "\t\t" . '{' . PHP_EOL .
  1403. "\t\t\t" . '$this->getMockController()->invoke(\'__construct\', func_get_args());' . PHP_EOL .
  1404. "\t\t" . '}' . PHP_EOL .
  1405. "\t" . '}' . PHP_EOL .
  1406. "\t" . 'public function ' . $methodName . '()' . PHP_EOL .
  1407. "\t" . '{' . PHP_EOL .
  1408. "\t\t" . '$arguments = array_merge(array(), array_slice(func_get_args(), 0));' . PHP_EOL .
  1409. "\t\t" . 'if (isset($this->getMockController()->' . $methodName . ') === true)' . PHP_EOL .
  1410. "\t\t" . '{' . PHP_EOL .
  1411. "\t\t\t" . '$return = $this->getMockController()->invoke(\'' . $methodName . '\', $arguments);' . PHP_EOL .
  1412. "\t\t\t" . 'return $return;' . PHP_EOL .
  1413. "\t\t" . '}' . PHP_EOL .
  1414. "\t\t" . 'else' . PHP_EOL .
  1415. "\t\t" . '{' . PHP_EOL .
  1416. "\t\t\t" . '$this->getMockController()->addCall(\'' . $methodName . '\', $arguments);' . PHP_EOL .
  1417. "\t\t\t" . '$return = call_user_func_array(\'parent::' . $methodName . '\', $arguments);' . PHP_EOL .
  1418. "\t\t\t" . 'return $return;' . PHP_EOL .
  1419. "\t\t" . '}' . PHP_EOL .
  1420. "\t" . '}' . PHP_EOL .
  1421. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  1422. "\t" . '{' . PHP_EOL .
  1423. "\t\t" . 'return ' . var_export(array('__construct', $methodName), true) . ';' . PHP_EOL .
  1424. "\t" . '}' . PHP_EOL .
  1425. '}' . PHP_EOL .
  1426. '}'
  1427. )
  1428. ;
  1429. }
  1430. public function testGetMockedClassCodeForAbstractClassWithConstructorInInterface()
  1431. {
  1432. $this
  1433. ->if($generator = new testedClass())
  1434. ->and($publicMethodController = new mock\controller())
  1435. ->and($publicMethodController->__construct = function() {})
  1436. ->and($publicMethodController->getName = '__construct')
  1437. ->and($publicMethodController->isConstructor = true)
  1438. ->and($publicMethodController->getParameters = array())
  1439. ->and($publicMethodController->isPublic = true)
  1440. ->and($publicMethodController->isProtected = false)
  1441. ->and($publicMethodController->isPrivate = false)
  1442. ->and($publicMethodController->isFinal = false)
  1443. ->and($publicMethodController->isStatic = false)
  1444. ->and($publicMethodController->isAbstract = true)
  1445. ->and($publicMethodController->returnsReference = false)
  1446. ->and($publicMethod = new \mock\reflectionMethod(null, null))
  1447. ->and($classController = new mock\controller())
  1448. ->and($classController->__construct = function() {})
  1449. ->and($classController->getName = $className = uniqid())
  1450. ->and($classController->isFinal = false)
  1451. ->and($classController->isInterface = false)
  1452. ->and($classController->isAbstract = true)
  1453. ->and($classController->getMethods = array($publicMethod))
  1454. ->and($classController->getConstructor = $publicMethod)
  1455. ->and($class = new \mock\reflectionClass(null))
  1456. ->and($generator->setReflectionClassFactory(function() use ($class) { return $class; }))
  1457. ->and($adapter = new atoum\test\adapter())
  1458. ->and($adapter->class_exists = function($class) use ($className) { return ($class == '\\' . $className); })
  1459. ->and($generator->setAdapter($adapter))
  1460. ->then
  1461. ->string($generator->getMockedClassCode($className))->isEqualTo(
  1462. 'namespace mock {' . PHP_EOL .
  1463. 'final class ' . $className . ' extends \\' . $className . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  1464. '{' . PHP_EOL .
  1465. $this->getMockControllerMethods() .
  1466. "\t" . 'public function __construct(\mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  1467. "\t" . '{' . PHP_EOL .
  1468. "\t\t" . '$arguments = array_merge(array(), array_slice(func_get_args(), 0, -1));' . PHP_EOL .
  1469. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  1470. "\t\t" . '{' . PHP_EOL .
  1471. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  1472. "\t\t" . '}' . PHP_EOL .
  1473. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  1474. "\t\t" . '{' . PHP_EOL .
  1475. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  1476. "\t\t" . '}' . PHP_EOL .
  1477. "\t\t" . 'if (isset($this->getMockController()->__construct) === false)' . PHP_EOL .
  1478. "\t\t" . '{' . PHP_EOL .
  1479. "\t\t\t" . '$this->getMockController()->__construct = function() {};' . PHP_EOL .
  1480. "\t\t" . '}' . PHP_EOL .
  1481. "\t\t" . '$this->getMockController()->invoke(\'__construct\', $arguments);' . PHP_EOL .
  1482. "\t" . '}' . PHP_EOL .
  1483. "\t" . 'public function __call($methodName, $arguments)' . PHP_EOL .
  1484. "\t" . '{' . PHP_EOL .
  1485. "\t\t" . 'if (isset($this->getMockController()->{$methodName}) === true)' . PHP_EOL .
  1486. "\t\t" . '{' . PHP_EOL .
  1487. "\t\t\t" . '$return = $this->getMockController()->invoke($methodName, $arguments);' . PHP_EOL .
  1488. "\t\t\t" . 'return $return;' . PHP_EOL .
  1489. "\t\t" . '}' . PHP_EOL .
  1490. "\t\t" . 'else' . PHP_EOL .
  1491. "\t\t" . '{' . PHP_EOL .
  1492. "\t\t\t" . '$this->getMockController()->addCall($methodName, $arguments);' . PHP_EOL .
  1493. "\t\t" . '}' . PHP_EOL .
  1494. "\t" . '}' . PHP_EOL .
  1495. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  1496. "\t" . '{' . PHP_EOL .
  1497. "\t\t" . 'return ' . var_export(array('__construct', '__call'), true) . ';' . PHP_EOL .
  1498. "\t" . '}' . PHP_EOL .
  1499. '}' . PHP_EOL .
  1500. '}'
  1501. )
  1502. ;
  1503. }
  1504. public function testGenerate()
  1505. {
  1506. $this
  1507. ->if($generator = new testedClass())
  1508. ->then
  1509. ->exception(function() use ($generator) { $generator->generate(''); })
  1510. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  1511. ->hasMessage('Class name \'\' is invalid')
  1512. ->exception(function() use ($generator) { $generator->generate('\\'); })
  1513. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  1514. ->hasMessage('Class name \'\\\' is invalid')
  1515. ->exception(function() use ($generator, & $class) { $generator->generate($class = ('\\' . uniqid() . '\\')); })
  1516. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  1517. ->hasMessage('Class name \'' . $class . '\' is invalid')
  1518. ->if($adapter = new atoum\test\adapter())
  1519. ->and($adapter->class_exists = false)
  1520. ->and($adapter->interface_exists = false)
  1521. ->and($generator->setAdapter($adapter))
  1522. ->and($class = uniqid('unknownClass'))
  1523. ->then
  1524. ->object($generator->generate($class))->isIdenticalTo($generator)
  1525. ->class('\mock\\' . $class)
  1526. ->hasNoParent()
  1527. ->hasInterface('mageekguy\atoum\mock\aggregator')
  1528. ->if($class = '\\' . uniqid('unknownClass'))
  1529. ->then
  1530. ->object($generator->generate($class))->isIdenticalTo($generator)
  1531. ->class('\mock' . $class)
  1532. ->hasNoParent()
  1533. ->hasInterface('mageekguy\atoum\mock\aggregator')
  1534. ->if($adapter->class_exists = true)
  1535. ->and($class = uniqid())
  1536. ->then
  1537. ->exception(function () use ($generator, $class) {
  1538. $generator->generate($class);
  1539. }
  1540. )
  1541. ->isInstanceOf('mageekguy\atoum\exceptions\logic')
  1542. ->hasMessage('Class \'\mock\\' . $class . '\' already exists')
  1543. ->if($class = '\\' . uniqid())
  1544. ->then
  1545. ->exception(function () use ($generator, $class) {
  1546. $generator->generate($class);
  1547. }
  1548. )
  1549. ->isInstanceOf('mageekguy\atoum\exceptions\logic')
  1550. ->hasMessage('Class \'\mock' . $class . '\' already exists')
  1551. ->if($class = uniqid())
  1552. ->and($adapter->class_exists = function($arg) use ($class) { return $arg === '\\' . $class; })
  1553. ->and($reflectionClassController = new mock\controller())
  1554. ->and($reflectionClassController->__construct = function() {})
  1555. ->and($reflectionClassController->isFinal = true)
  1556. ->and($reflectionClassController->isInterface = false)
  1557. ->and($reflectionClass = new \mock\reflectionClass(uniqid(), $reflectionClassController))
  1558. ->and($generator->setReflectionClassFactory(function() use ($reflectionClass) { return $reflectionClass; }))
  1559. ->then
  1560. ->exception(function () use ($generator, $class) {
  1561. $generator->generate($class);
  1562. }
  1563. )
  1564. ->isInstanceOf('mageekguy\atoum\exceptions\logic')
  1565. ->hasMessage('Class \'\\' . $class . '\' is final, unable to mock it')
  1566. ->if($class = '\\' . uniqid())
  1567. ->and($adapter->class_exists = function($arg) use ($class) { return $arg === $class; })
  1568. ->then
  1569. ->exception(function () use ($generator, $class) {
  1570. $generator->generate($class);
  1571. }
  1572. )
  1573. ->isInstanceOf('mageekguy\atoum\exceptions\logic')
  1574. ->hasMessage('Class \'' . $class . '\' is final, unable to mock it')
  1575. ->if($reflectionClassController->isFinal = false)
  1576. ->and($generator = new testedClass())
  1577. ->then
  1578. ->object($generator->generate(__CLASS__))->isIdenticalTo($generator)
  1579. ->class('\mock\\' . __CLASS__)
  1580. ->hasParent(__CLASS__)
  1581. ->hasInterface('mageekguy\atoum\mock\aggregator')
  1582. ->if($generator = new testedClass())
  1583. ->and($generator->shunt('__construct'))
  1584. ->then
  1585. ->boolean($generator->isShunted('__construct'))->isTrue()
  1586. ->object($generator->generate('reflectionMethod'))->isIdenticalTo($generator)
  1587. ->boolean($generator->isShunted('__construct'))->isFalse()
  1588. ->if($generator = new testedClass())
  1589. ->and($generator->shuntParentClassCalls())
  1590. ->then
  1591. ->object($generator->generate('reflectionParameter'))->isIdenticalTo($generator)
  1592. ->boolean($generator->callsToParentClassAreShunted())->isFalse()
  1593. ;
  1594. }
  1595. public function testMethodIsMockable()
  1596. {
  1597. $this
  1598. ->if($generator = new testedClass())
  1599. ->and($this->mockGenerator->orphanize('__construct'))
  1600. ->and($method = new \mock\reflectionMethod($this, $methodName = uniqid()))
  1601. ->and($this->calling($method)->getName = $methodName)
  1602. ->and($this->calling($method)->isFinal = false)
  1603. ->and($this->calling($method)->isStatic = false)
  1604. ->and($this->calling($method)->isAbstract = false)
  1605. ->and($this->calling($method)->isPrivate = false)
  1606. ->and($this->calling($method)->isProtected = false)
  1607. ->then
  1608. ->boolean($generator->methodIsMockable($method))->isTrue()
  1609. ->if($this->calling($method)->isFinal = true)
  1610. ->then
  1611. ->boolean($generator->methodIsMockable($method))->isFalse()
  1612. ->if($this->calling($method)->isFinal = false)
  1613. ->and($this->calling($method)->isStatic = true)
  1614. ->then
  1615. ->boolean($generator->methodIsMockable($method))->isFalse()
  1616. ->if($this->calling($method)->isStatic = false)
  1617. ->and($this->calling($method)->isPrivate = true)
  1618. ->then
  1619. ->boolean($generator->methodIsMockable($method))->isFalse()
  1620. ->if($this->calling($method)->isPrivate = false)
  1621. ->and($this->calling($method)->isProtected = true)
  1622. ->then
  1623. ->boolean($generator->methodIsMockable($method))->isFalse()
  1624. ->if($generator->overload(new mock\php\method($methodName)))
  1625. ->then
  1626. ->boolean($generator->methodIsMockable($method))->isTrue()
  1627. ;
  1628. }
  1629. public function testMethodIsMockableWithReservedWord($reservedWord)
  1630. {
  1631. $this
  1632. ->if($generator = new testedClass())
  1633. ->and($this->mockGenerator->orphanize('__construct'))
  1634. ->and($method = new \mock\reflectionMethod($this, $reservedWord))
  1635. ->and($this->calling($method)->getName = $reservedWord)
  1636. ->and($this->calling($method)->isFinal = false)
  1637. ->and($this->calling($method)->isStatic = false)
  1638. ->and($this->calling($method)->isAbstract = false)
  1639. ->and($this->calling($method)->isPrivate = false)
  1640. ->and($this->calling($method)->isProtected = false)
  1641. ->then
  1642. ->boolean($generator->methodIsMockable($method))->isFalse()
  1643. ;
  1644. }
  1645. /**
  1646. * @php >= 5.4
  1647. * @php < 5.6
  1648. */
  1649. public function testGetMockedClassCodeWithOrphanizedMethod()
  1650. {
  1651. $this
  1652. ->if->mockGenerator->orphanize('__construct')
  1653. ->and($a = new \mock\reflectionParameter())
  1654. ->and($this->calling($a)->getName = 'a')
  1655. ->and($this->calling($a)->isArray = false)
  1656. ->and($this->calling($a)->isCallable = false)
  1657. ->and($this->calling($a)->getClass = null)
  1658. ->and($this->calling($a)->isPassedByReference = false)
  1659. ->and($this->calling($a)->isDefaultValueAvailable = false)
  1660. ->and($this->calling($a)->isOptional = false)
  1661. ->and($b = new \mock\reflectionParameter())
  1662. ->and($this->calling($b)->getName = 'b')
  1663. ->and($this->calling($b)->isArray = false)
  1664. ->and($this->calling($b)->isCallable = false)
  1665. ->and($this->calling($b)->getClass = null)
  1666. ->and($this->calling($b)->isPassedByReference = false)
  1667. ->and($this->calling($b)->isDefaultValueAvailable = false)
  1668. ->and($this->calling($b)->isOptional = false)
  1669. ->and($c = new \mock\reflectionParameter())
  1670. ->and($this->calling($c)->getName = 'c')
  1671. ->and($this->calling($c)->isArray = false)
  1672. ->and($this->calling($c)->isCallable = false)
  1673. ->and($this->calling($c)->getClass = null)
  1674. ->and($this->calling($c)->isPassedByReference = false)
  1675. ->and($this->calling($c)->isDefaultValueAvailable = false)
  1676. ->and($this->calling($c)->isOptional = false)
  1677. ->and->mockGenerator->orphanize('__construct')
  1678. ->and($constructor = new \mock\reflectionMethod())
  1679. ->and($this->calling($constructor)->getName = '__construct')
  1680. ->and($this->calling($constructor)->isConstructor = true)
  1681. ->and($this->calling($constructor)->getParameters = array($a, $b, $c))
  1682. ->and($this->calling($constructor)->isPublic = true)
  1683. ->and($this->calling($constructor)->isProtected = false)
  1684. ->and($this->calling($constructor)->isPrivate = false)
  1685. ->and($this->calling($constructor)->isFinal = false)
  1686. ->and($this->calling($constructor)->isStatic = false)
  1687. ->and($this->calling($constructor)->isAbstract = false)
  1688. ->and($this->calling($constructor)->returnsReference = false)
  1689. ->and->mockGenerator->orphanize('__construct')
  1690. ->and($class = new \mock\reflectionClass())
  1691. ->and($this->calling($class)->getName = $className = uniqid())
  1692. ->and($this->calling($class)->isFinal = false)
  1693. ->and($this->calling($class)->isInterface = false)
  1694. ->and($this->calling($class)->isAbstract = false)
  1695. ->and($this->calling($class)->getMethods = array($constructor))
  1696. ->and($this->calling($class)->getConstructor = $constructor)
  1697. ->and($adapter = new atoum\test\adapter())
  1698. ->and($adapter->class_exists = function($class) use ($className) { return ($class == '\\' . $className); })
  1699. ->and($generator = new testedClass())
  1700. ->and($generator->setReflectionClassFactory(function() use ($class) { return $class; }))
  1701. ->and($generator->setAdapter($adapter))
  1702. ->and($generator->orphanize('__construct'))
  1703. ->then
  1704. ->string($generator->getMockedClassCode($className))->isEqualTo(
  1705. 'namespace mock {' . PHP_EOL .
  1706. 'final class ' . $className . ' extends \\' . $className . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  1707. '{' . PHP_EOL .
  1708. $this->getMockControllerMethods() .
  1709. "\t" . 'public function __construct($a = null, $b = null, $c = null, \mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  1710. "\t" . '{' . PHP_EOL .
  1711. "\t\t" . '$arguments = array_merge(array($a, $b, $c), array_slice(func_get_args(), 3, -1));' . PHP_EOL .
  1712. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  1713. "\t\t" . '{' . PHP_EOL .
  1714. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  1715. "\t\t" . '}' . PHP_EOL .
  1716. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  1717. "\t\t" . '{' . PHP_EOL .
  1718. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  1719. "\t\t" . '}' . PHP_EOL .
  1720. "\t\t" . 'if (isset($this->getMockController()->__construct) === false)' . PHP_EOL .
  1721. "\t\t" . '{' . PHP_EOL .
  1722. "\t\t\t" . '$this->getMockController()->__construct = function() {};' . PHP_EOL .
  1723. "\t\t" . '}' . PHP_EOL .
  1724. "\t\t" . '$this->getMockController()->invoke(\'__construct\', $arguments);' . PHP_EOL .
  1725. "\t" . '}' . PHP_EOL .
  1726. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  1727. "\t" . '{' . PHP_EOL .
  1728. "\t\t" . 'return ' . var_export(array('__construct'), true) . ';' . PHP_EOL .
  1729. "\t" . '}' . PHP_EOL .
  1730. '}' . PHP_EOL .
  1731. '}'
  1732. )
  1733. ;
  1734. }
  1735. /** @php 5.6 */
  1736. public function testGetMockedClassCodeWithOrphanizedMethodPhp56()
  1737. {
  1738. $this
  1739. ->if->mockGenerator->orphanize('__construct')
  1740. ->and($a = new \mock\reflectionParameter())
  1741. ->and($this->calling($a)->getName = 'a')
  1742. ->and($this->calling($a)->isArray = false)
  1743. ->and($this->calling($a)->isCallable = false)
  1744. ->and($this->calling($a)->getClass = null)
  1745. ->and($this->calling($a)->isPassedByReference = false)
  1746. ->and($this->calling($a)->isDefaultValueAvailable = false)
  1747. ->and($this->calling($a)->isOptional = false)
  1748. ->and($this->calling($a)->isVariadic = false)
  1749. ->and($b = new \mock\reflectionParameter())
  1750. ->and($this->calling($b)->getName = 'b')
  1751. ->and($this->calling($b)->isArray = false)
  1752. ->and($this->calling($b)->isCallable = false)
  1753. ->and($this->calling($b)->getClass = null)
  1754. ->and($this->calling($b)->isPassedByReference = false)
  1755. ->and($this->calling($b)->isDefaultValueAvailable = false)
  1756. ->and($this->calling($b)->isOptional = false)
  1757. ->and($this->calling($b)->isVariadic = false)
  1758. ->and($c = new \mock\reflectionParameter())
  1759. ->and($this->calling($c)->getName = 'c')
  1760. ->and($this->calling($c)->isArray = false)
  1761. ->and($this->calling($c)->isCallable = false)
  1762. ->and($this->calling($c)->getClass = null)
  1763. ->and($this->calling($c)->isPassedByReference = false)
  1764. ->and($this->calling($c)->isDefaultValueAvailable = false)
  1765. ->and($this->calling($c)->isOptional = false)
  1766. ->and($this->calling($c)->isVariadic = false)
  1767. ->and->mockGenerator->orphanize('__construct')
  1768. ->and($constructor = new \mock\reflectionMethod())
  1769. ->and($this->calling($constructor)->getName = '__construct')
  1770. ->and($this->calling($constructor)->isConstructor = true)
  1771. ->and($this->calling($constructor)->getParameters = array($a, $b, $c))
  1772. ->and($this->calling($constructor)->isPublic = true)
  1773. ->and($this->calling($constructor)->isProtected = false)
  1774. ->and($this->calling($constructor)->isPrivate = false)
  1775. ->and($this->calling($constructor)->isFinal = false)
  1776. ->and($this->calling($constructor)->isStatic = false)
  1777. ->and($this->calling($constructor)->isAbstract = false)
  1778. ->and($this->calling($constructor)->returnsReference = false)
  1779. ->and->mockGenerator->orphanize('__construct')
  1780. ->and($class = new \mock\reflectionClass())
  1781. ->and($this->calling($class)->getName = $className = uniqid())
  1782. ->and($this->calling($class)->isFinal = false)
  1783. ->and($this->calling($class)->isInterface = false)
  1784. ->and($this->calling($class)->isAbstract = false)
  1785. ->and($this->calling($class)->getMethods = array($constructor))
  1786. ->and($this->calling($class)->getConstructor = $constructor)
  1787. ->and($adapter = new atoum\test\adapter())
  1788. ->and($adapter->class_exists = function($class) use ($className) { return ($class == '\\' . $className); })
  1789. ->and($generator = new testedClass())
  1790. ->and($generator->setReflectionClassFactory(function() use ($class) { return $class; }))
  1791. ->and($generator->setAdapter($adapter))
  1792. ->and($generator->orphanize('__construct'))
  1793. ->then
  1794. ->string($generator->getMockedClassCode($className))->isEqualTo(
  1795. 'namespace mock {' . PHP_EOL .
  1796. 'final class ' . $className . ' extends \\' . $className . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  1797. '{' . PHP_EOL .
  1798. $this->getMockControllerMethods() .
  1799. "\t" . 'public function __construct($a = null, $b = null, $c = null, \mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  1800. "\t" . '{' . PHP_EOL .
  1801. "\t\t" . '$arguments = array_merge(array($a, $b, $c), array_slice(func_get_args(), 3, -1));' . PHP_EOL .
  1802. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  1803. "\t\t" . '{' . PHP_EOL .
  1804. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  1805. "\t\t" . '}' . PHP_EOL .
  1806. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  1807. "\t\t" . '{' . PHP_EOL .
  1808. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  1809. "\t\t" . '}' . PHP_EOL .
  1810. "\t\t" . 'if (isset($this->getMockController()->__construct) === false)' . PHP_EOL .
  1811. "\t\t" . '{' . PHP_EOL .
  1812. "\t\t\t" . '$this->getMockController()->__construct = function() {};' . PHP_EOL .
  1813. "\t\t" . '}' . PHP_EOL .
  1814. "\t\t" . '$this->getMockController()->invoke(\'__construct\', $arguments);' . PHP_EOL .
  1815. "\t" . '}' . PHP_EOL .
  1816. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  1817. "\t" . '{' . PHP_EOL .
  1818. "\t\t" . 'return ' . var_export(array('__construct'), true) . ';' . PHP_EOL .
  1819. "\t" . '}' . PHP_EOL .
  1820. '}' . PHP_EOL .
  1821. '}'
  1822. )
  1823. ;
  1824. }
  1825. /**
  1826. * @php 5.4
  1827. * @php < 5.6
  1828. */
  1829. public function testGetMockedClassCodeWithProtectedAbstractMethod()
  1830. {
  1831. $this
  1832. ->if($generator = new testedClass())
  1833. ->and($parameterController1 = new mock\controller())
  1834. ->and($parameterController1->__construct = function() {})
  1835. ->and($parameterController1->isArray = false)
  1836. ->and($parameterController1->isCallable = false)
  1837. ->and($parameterController1->getClass = null)
  1838. ->and($parameterController1->getName = 'arg1')
  1839. ->and($parameterController1->isPassedByReference = false)
  1840. ->and($parameterController1->isDefaultValueAvailable = false)
  1841. ->and($parameterController1->isOptional = false)
  1842. ->and($parameter1 = new \mock\reflectionParameter(null, null))
  1843. ->and($parameterController2 = new mock\controller())
  1844. ->and($parameterController2->__construct = function() {})
  1845. ->and($parameterController2->isArray = true)
  1846. ->and($parameterController2->isCallable = false)
  1847. ->and($parameterController2->getClass = null)
  1848. ->and($parameterController2->getName = 'arg2')
  1849. ->and($parameterController2->isPassedByReference = true)
  1850. ->and($parameterController2->isDefaultValueAvailable = false)
  1851. ->and($parameterController2->isOptional = false)
  1852. ->and($parameter2 = new \mock\reflectionParameter(null, null))
  1853. ->and($publicMethodController = new mock\controller())
  1854. ->and($publicMethodController->__construct = function() {})
  1855. ->and($publicMethodController->getName = $publicMethodName = uniqid())
  1856. ->and($publicMethodController->isConstructor = false)
  1857. ->and($publicMethodController->getParameters = array($parameter1, $parameter2))
  1858. ->and($publicMethodController->isPublic = true)
  1859. ->and($publicMethodController->isProtected = false)
  1860. ->and($publicMethodController->isPrivate = false)
  1861. ->and($publicMethodController->isFinal = false)
  1862. ->and($publicMethodController->isStatic = false)
  1863. ->and($publicMethodController->isAbstract = true)
  1864. ->and($publicMethodController->returnsReference = false)
  1865. ->and($publicMethod = new \mock\reflectionMethod(null, null))
  1866. ->and($protectedMethodController = new mock\controller())
  1867. ->and($protectedMethodController->__construct = function() {})
  1868. ->and($protectedMethodController->getName = $protectedMethodName = uniqid())
  1869. ->and($protectedMethodController->isConstructor = false)
  1870. ->and($protectedMethodController->getParameters = array())
  1871. ->and($protectedMethodController->isPublic = false)
  1872. ->and($protectedMethodController->isProtected = true)
  1873. ->and($protectedMethodController->isPrivate = false)
  1874. ->and($protectedMethodController->isFinal = false)
  1875. ->and($protectedMethodController->isStatic = false)
  1876. ->and($protectedMethodController->isAbstract = true)
  1877. ->and($protectedMethodController->returnsReference = false)
  1878. ->and($protectedMethod = new \mock\reflectionMethod(null, null))
  1879. ->and($classController = new mock\controller())
  1880. ->and($classController->__construct = function() {})
  1881. ->and($classController->getName = $className = uniqid())
  1882. ->and($classController->isFinal = false)
  1883. ->and($classController->isInterface = false)
  1884. ->and($classController->getMethods = array($publicMethod, $protectedMethod))
  1885. ->and($classController->getConstructor = null)
  1886. ->and($classController->isAbstract = false)
  1887. ->and($class = new \mock\reflectionClass(null))
  1888. ->and($generator->setReflectionClassFactory(function() use ($class) { return $class; }))
  1889. ->and($adapter = new atoum\test\adapter())
  1890. ->and($adapter->class_exists = function($class) use ($className) { return ($class == '\\' . $className); })
  1891. ->and($generator->setAdapter($adapter))
  1892. ->then
  1893. ->string($generator->getMockedClassCode($className))->isEqualTo(
  1894. 'namespace mock {' . PHP_EOL .
  1895. 'final class ' . $className . ' extends \\' . $className . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  1896. '{' . PHP_EOL .
  1897. $this->getMockControllerMethods() .
  1898. "\t" . 'public function __construct(\mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  1899. "\t" . '{' . PHP_EOL .
  1900. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  1901. "\t\t" . '{' . PHP_EOL .
  1902. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  1903. "\t\t" . '}' . PHP_EOL .
  1904. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  1905. "\t\t" . '{' . PHP_EOL .
  1906. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  1907. "\t\t" . '}' . PHP_EOL .
  1908. "\t\t" . 'if (isset($this->getMockController()->__construct) === true)' . PHP_EOL .
  1909. "\t\t" . '{' . PHP_EOL .
  1910. "\t\t\t" . '$this->getMockController()->invoke(\'__construct\', func_get_args());' . PHP_EOL .
  1911. "\t\t" . '}' . PHP_EOL .
  1912. "\t" . '}' . PHP_EOL .
  1913. "\t" . 'public function ' . $publicMethodName . '($arg1, array & $arg2)' . PHP_EOL .
  1914. "\t" . '{' . PHP_EOL .
  1915. "\t\t" . '$arguments = array_merge(array($arg1, & $arg2), array_slice(func_get_args(), 2));' . PHP_EOL .
  1916. "\t\t" . 'if (isset($this->getMockController()->' . $publicMethodName . ') === false)' . PHP_EOL .
  1917. "\t\t" . '{' . PHP_EOL .
  1918. "\t\t\t" . '$this->getMockController()->' . $publicMethodName . ' = function() {};' . PHP_EOL .
  1919. "\t\t" . '}' . PHP_EOL .
  1920. "\t\t" . '$return = $this->getMockController()->invoke(\'' . $publicMethodName . '\', $arguments);' . PHP_EOL .
  1921. "\t\t" . 'return $return;' . PHP_EOL .
  1922. "\t" . '}' . PHP_EOL .
  1923. "\t" . 'protected function ' . $protectedMethodName . '()' . PHP_EOL .
  1924. "\t" . '{' . PHP_EOL .
  1925. "\t\t" . '$arguments = array_merge(array(), array_slice(func_get_args(), 0));' . PHP_EOL .
  1926. "\t\t" . 'if (isset($this->getMockController()->' . $protectedMethodName . ') === false)' . PHP_EOL .
  1927. "\t\t" . '{' . PHP_EOL .
  1928. "\t\t\t" . '$this->getMockController()->' . $protectedMethodName . ' = function() {};' . PHP_EOL .
  1929. "\t\t" . '}' . PHP_EOL .
  1930. "\t\t" . '$return = $this->getMockController()->invoke(\'' . $protectedMethodName . '\', $arguments);' . PHP_EOL .
  1931. "\t\t" . 'return $return;' . PHP_EOL .
  1932. "\t" . '}' . PHP_EOL .
  1933. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  1934. "\t" . '{' . PHP_EOL .
  1935. "\t\t" . 'return ' . var_export(array('__construct', $publicMethodName, $protectedMethodName), true) . ';' . PHP_EOL .
  1936. "\t" . '}' . PHP_EOL .
  1937. '}' . PHP_EOL .
  1938. '}'
  1939. )
  1940. ;
  1941. }
  1942. /** @php 5.6 */
  1943. public function testGetMockedClassCodeWithProtectedAbstractMethodPhp56()
  1944. {
  1945. $this
  1946. ->if($generator = new testedClass())
  1947. ->and($parameterController1 = new mock\controller())
  1948. ->and($parameterController1->__construct = function() {})
  1949. ->and($parameterController1->isArray = false)
  1950. ->and($parameterController1->isCallable = false)
  1951. ->and($parameterController1->getClass = null)
  1952. ->and($parameterController1->getName = 'arg1')
  1953. ->and($parameterController1->isPassedByReference = false)
  1954. ->and($parameterController1->isDefaultValueAvailable = false)
  1955. ->and($parameterController1->isOptional = false)
  1956. ->and($parameterController1->isVariadic = false)
  1957. ->and($parameter1 = new \mock\reflectionParameter(null, null))
  1958. ->and($parameterController2 = new mock\controller())
  1959. ->and($parameterController2->__construct = function() {})
  1960. ->and($parameterController2->isArray = true)
  1961. ->and($parameterController2->isCallable = false)
  1962. ->and($parameterController2->getClass = null)
  1963. ->and($parameterController2->getName = 'arg2')
  1964. ->and($parameterController2->isPassedByReference = true)
  1965. ->and($parameterController2->isDefaultValueAvailable = false)
  1966. ->and($parameterController2->isOptional = false)
  1967. ->and($parameterController2->isVariadic = false)
  1968. ->and($parameter2 = new \mock\reflectionParameter(null, null))
  1969. ->and($publicMethodController = new mock\controller())
  1970. ->and($publicMethodController->__construct = function() {})
  1971. ->and($publicMethodController->getName = $publicMethodName = uniqid())
  1972. ->and($publicMethodController->isConstructor = false)
  1973. ->and($publicMethodController->getParameters = array($parameter1, $parameter2))
  1974. ->and($publicMethodController->isPublic = true)
  1975. ->and($publicMethodController->isProtected = false)
  1976. ->and($publicMethodController->isPrivate = false)
  1977. ->and($publicMethodController->isFinal = false)
  1978. ->and($publicMethodController->isStatic = false)
  1979. ->and($publicMethodController->isAbstract = true)
  1980. ->and($publicMethodController->returnsReference = false)
  1981. ->and($publicMethod = new \mock\reflectionMethod(null, null))
  1982. ->and($protectedMethodController = new mock\controller())
  1983. ->and($protectedMethodController->__construct = function() {})
  1984. ->and($protectedMethodController->getName = $protectedMethodName = uniqid())
  1985. ->and($protectedMethodController->isConstructor = false)
  1986. ->and($protectedMethodController->getParameters = array())
  1987. ->and($protectedMethodController->isPublic = false)
  1988. ->and($protectedMethodController->isProtected = true)
  1989. ->and($protectedMethodController->isPrivate = false)
  1990. ->and($protectedMethodController->isFinal = false)
  1991. ->and($protectedMethodController->isStatic = false)
  1992. ->and($protectedMethodController->isAbstract = true)
  1993. ->and($protectedMethodController->returnsReference = false)
  1994. ->and($protectedMethod = new \mock\reflectionMethod(null, null))
  1995. ->and($classController = new mock\controller())
  1996. ->and($classController->__construct = function() {})
  1997. ->and($classController->getName = $className = uniqid())
  1998. ->and($classController->isFinal = false)
  1999. ->and($classController->isInterface = false)
  2000. ->and($classController->getMethods = array($publicMethod, $protectedMethod))
  2001. ->and($classController->getConstructor = null)
  2002. ->and($classController->isAbstract = false)
  2003. ->and($class = new \mock\reflectionClass(null))
  2004. ->and($generator->setReflectionClassFactory(function() use ($class) { return $class; }))
  2005. ->and($adapter = new atoum\test\adapter())
  2006. ->and($adapter->class_exists = function($class) use ($className) { return ($class == '\\' . $className); })
  2007. ->and($generator->setAdapter($adapter))
  2008. ->then
  2009. ->string($generator->getMockedClassCode($className))->isEqualTo(
  2010. 'namespace mock {' . PHP_EOL .
  2011. 'final class ' . $className . ' extends \\' . $className . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  2012. '{' . PHP_EOL .
  2013. $this->getMockControllerMethods() .
  2014. "\t" . 'public function __construct(\mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  2015. "\t" . '{' . PHP_EOL .
  2016. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  2017. "\t\t" . '{' . PHP_EOL .
  2018. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  2019. "\t\t" . '}' . PHP_EOL .
  2020. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  2021. "\t\t" . '{' . PHP_EOL .
  2022. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  2023. "\t\t" . '}' . PHP_EOL .
  2024. "\t\t" . 'if (isset($this->getMockController()->__construct) === true)' . PHP_EOL .
  2025. "\t\t" . '{' . PHP_EOL .
  2026. "\t\t\t" . '$this->getMockController()->invoke(\'__construct\', func_get_args());' . PHP_EOL .
  2027. "\t\t" . '}' . PHP_EOL .
  2028. "\t" . '}' . PHP_EOL .
  2029. "\t" . 'public function ' . $publicMethodName . '($arg1, array & $arg2)' . PHP_EOL .
  2030. "\t" . '{' . PHP_EOL .
  2031. "\t\t" . '$arguments = array_merge(array($arg1, & $arg2), array_slice(func_get_args(), 2));' . PHP_EOL .
  2032. "\t\t" . 'if (isset($this->getMockController()->' . $publicMethodName . ') === false)' . PHP_EOL .
  2033. "\t\t" . '{' . PHP_EOL .
  2034. "\t\t\t" . '$this->getMockController()->' . $publicMethodName . ' = function() {};' . PHP_EOL .
  2035. "\t\t" . '}' . PHP_EOL .
  2036. "\t\t" . '$return = $this->getMockController()->invoke(\'' . $publicMethodName . '\', $arguments);' . PHP_EOL .
  2037. "\t\t" . 'return $return;' . PHP_EOL .
  2038. "\t" . '}' . PHP_EOL .
  2039. "\t" . 'protected function ' . $protectedMethodName . '()' . PHP_EOL .
  2040. "\t" . '{' . PHP_EOL .
  2041. "\t\t" . '$arguments = array_merge(array(), array_slice(func_get_args(), 0));' . PHP_EOL .
  2042. "\t\t" . 'if (isset($this->getMockController()->' . $protectedMethodName . ') === false)' . PHP_EOL .
  2043. "\t\t" . '{' . PHP_EOL .
  2044. "\t\t\t" . '$this->getMockController()->' . $protectedMethodName . ' = function() {};' . PHP_EOL .
  2045. "\t\t" . '}' . PHP_EOL .
  2046. "\t\t" . '$return = $this->getMockController()->invoke(\'' . $protectedMethodName . '\', $arguments);' . PHP_EOL .
  2047. "\t\t" . 'return $return;' . PHP_EOL .
  2048. "\t" . '}' . PHP_EOL .
  2049. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  2050. "\t" . '{' . PHP_EOL .
  2051. "\t\t" . 'return ' . var_export(array('__construct', $publicMethodName, $protectedMethodName), true) . ';' . PHP_EOL .
  2052. "\t" . '}' . PHP_EOL .
  2053. '}' . PHP_EOL .
  2054. '}'
  2055. )
  2056. ;
  2057. }
  2058. /**
  2059. * @php 5.4
  2060. * @php < 5.6
  2061. */
  2062. public function testGetMockedClassCodeForClassWithCallableTypeHint()
  2063. {
  2064. $this
  2065. ->if($generator = new testedClass())
  2066. ->and($reflectionParameterController = new mock\controller())
  2067. ->and($reflectionParameterController->__construct = function() {})
  2068. ->and($reflectionParameterController->isArray = false)
  2069. ->and($reflectionParameterController->isCallable = true)
  2070. ->and($reflectionParameterController->getName = 'callback')
  2071. ->and($reflectionParameterController->isPassedByReference = false)
  2072. ->and($reflectionParameterController->isDefaultValueAvailable = false)
  2073. ->and($reflectionParameterController->isOptional = false)
  2074. ->and($reflectionParameter = new \mock\reflectionParameter(null, null))
  2075. ->and($reflectionMethodController = new mock\controller())
  2076. ->and($reflectionMethodController->__construct = function() {})
  2077. ->and($reflectionMethodController->getName = '__construct')
  2078. ->and($reflectionMethodController->isConstructor = true)
  2079. ->and($reflectionMethodController->getParameters = array($reflectionParameter))
  2080. ->and($reflectionMethodController->isPublic = true)
  2081. ->and($reflectionMethodController->isProtected = false)
  2082. ->and($reflectionMethodController->isPrivate = false)
  2083. ->and($reflectionMethodController->isFinal = false)
  2084. ->and($reflectionMethodController->isStatic = false)
  2085. ->and($reflectionMethodController->isAbstract = false)
  2086. ->and($reflectionMethodController->returnsReference = false)
  2087. ->and($reflectionMethod = new \mock\reflectionMethod(null, null))
  2088. ->and($reflectionClassController = new mock\controller())
  2089. ->and($reflectionClassController->__construct = function() {})
  2090. ->and($reflectionClassController->getName = function() use (& $realClass) { return $realClass; })
  2091. ->and($reflectionClassController->isFinal = false)
  2092. ->and($reflectionClassController->isInterface = false)
  2093. ->and($reflectionClassController->getMethods = array($reflectionMethod))
  2094. ->and($reflectionClassController->getConstructor = $reflectionMethod)
  2095. ->and($reflectionClassController->isAbstract = false)
  2096. ->and($reflectionClass = new \mock\reflectionClass(null))
  2097. ->and($generator->setReflectionClassFactory(function() use ($reflectionClass) { return $reflectionClass; }))
  2098. ->and($adapter = new atoum\test\adapter())
  2099. ->and($adapter->class_exists = function($class) use (& $realClass) { return ($class == '\\' . $realClass); })
  2100. ->and($generator->setAdapter($adapter))
  2101. ->then
  2102. ->string($generator->getMockedClassCode($realClass = uniqid()))->isEqualTo(
  2103. 'namespace mock {' . PHP_EOL .
  2104. 'final class ' . $realClass . ' extends \\' . $realClass . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  2105. '{' . PHP_EOL .
  2106. $this->getMockControllerMethods() .
  2107. "\t" . 'public function __construct(callable $callback, \mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  2108. "\t" . '{' . PHP_EOL .
  2109. "\t\t" . '$arguments = array_merge(array($callback), array_slice(func_get_args(), 1, -1));' . PHP_EOL .
  2110. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  2111. "\t\t" . '{' . PHP_EOL .
  2112. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  2113. "\t\t" . '}' . PHP_EOL .
  2114. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  2115. "\t\t" . '{' . PHP_EOL .
  2116. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  2117. "\t\t" . '}' . PHP_EOL .
  2118. "\t\t" . 'if (isset($this->getMockController()->__construct) === true)' . PHP_EOL .
  2119. "\t\t" . '{' . PHP_EOL .
  2120. "\t\t\t" . '$this->getMockController()->invoke(\'__construct\', $arguments);' . PHP_EOL .
  2121. "\t\t" . '}' . PHP_EOL .
  2122. "\t\t" . 'else' . PHP_EOL .
  2123. "\t\t" . '{' . PHP_EOL .
  2124. "\t\t\t" . '$this->getMockController()->addCall(\'__construct\', $arguments);' . PHP_EOL .
  2125. "\t\t\t" . 'call_user_func_array(\'parent::__construct\', $arguments);' . PHP_EOL .
  2126. "\t\t" . '}' . PHP_EOL .
  2127. "\t" . '}' . PHP_EOL .
  2128. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  2129. "\t" . '{' . PHP_EOL .
  2130. "\t\t" . 'return ' . var_export(array('__construct'), true) . ';' . PHP_EOL .
  2131. "\t" . '}' . PHP_EOL .
  2132. '}' . PHP_EOL .
  2133. '}'
  2134. )
  2135. ;
  2136. }
  2137. /** @php 5.6 */
  2138. public function testGetMockedClassCodeForClassWithCallableTypeHintPhp56()
  2139. {
  2140. $this
  2141. ->if($generator = new testedClass())
  2142. ->and($reflectionParameterController = new mock\controller())
  2143. ->and($reflectionParameterController->__construct = function() {})
  2144. ->and($reflectionParameterController->isArray = false)
  2145. ->and($reflectionParameterController->isCallable = true)
  2146. ->and($reflectionParameterController->getName = 'callback')
  2147. ->and($reflectionParameterController->isPassedByReference = false)
  2148. ->and($reflectionParameterController->isDefaultValueAvailable = false)
  2149. ->and($reflectionParameterController->isOptional = false)
  2150. ->and($reflectionParameterController->isVariadic = false)
  2151. ->and($reflectionParameter = new \mock\reflectionParameter(null, null))
  2152. ->and($reflectionMethodController = new mock\controller())
  2153. ->and($reflectionMethodController->__construct = function() {})
  2154. ->and($reflectionMethodController->getName = '__construct')
  2155. ->and($reflectionMethodController->isConstructor = true)
  2156. ->and($reflectionMethodController->getParameters = array($reflectionParameter))
  2157. ->and($reflectionMethodController->isPublic = true)
  2158. ->and($reflectionMethodController->isProtected = false)
  2159. ->and($reflectionMethodController->isPrivate = false)
  2160. ->and($reflectionMethodController->isFinal = false)
  2161. ->and($reflectionMethodController->isStatic = false)
  2162. ->and($reflectionMethodController->isAbstract = false)
  2163. ->and($reflectionMethodController->returnsReference = false)
  2164. ->and($reflectionMethod = new \mock\reflectionMethod(null, null))
  2165. ->and($reflectionClassController = new mock\controller())
  2166. ->and($reflectionClassController->__construct = function() {})
  2167. ->and($reflectionClassController->getName = function() use (& $realClass) { return $realClass; })
  2168. ->and($reflectionClassController->isFinal = false)
  2169. ->and($reflectionClassController->isInterface = false)
  2170. ->and($reflectionClassController->getMethods = array($reflectionMethod))
  2171. ->and($reflectionClassController->getConstructor = $reflectionMethod)
  2172. ->and($reflectionClassController->isAbstract = false)
  2173. ->and($reflectionClass = new \mock\reflectionClass(null))
  2174. ->and($generator->setReflectionClassFactory(function() use ($reflectionClass) { return $reflectionClass; }))
  2175. ->and($adapter = new atoum\test\adapter())
  2176. ->and($adapter->class_exists = function($class) use (& $realClass) { return ($class == '\\' . $realClass); })
  2177. ->and($generator->setAdapter($adapter))
  2178. ->then
  2179. ->string($generator->getMockedClassCode($realClass = uniqid()))->isEqualTo(
  2180. 'namespace mock {' . PHP_EOL .
  2181. 'final class ' . $realClass . ' extends \\' . $realClass . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  2182. '{' . PHP_EOL .
  2183. $this->getMockControllerMethods() .
  2184. "\t" . 'public function __construct(callable $callback, \mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  2185. "\t" . '{' . PHP_EOL .
  2186. "\t\t" . '$arguments = array_merge(array($callback), array_slice(func_get_args(), 1, -1));' . PHP_EOL .
  2187. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  2188. "\t\t" . '{' . PHP_EOL .
  2189. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  2190. "\t\t" . '}' . PHP_EOL .
  2191. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  2192. "\t\t" . '{' . PHP_EOL .
  2193. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  2194. "\t\t" . '}' . PHP_EOL .
  2195. "\t\t" . 'if (isset($this->getMockController()->__construct) === true)' . PHP_EOL .
  2196. "\t\t" . '{' . PHP_EOL .
  2197. "\t\t\t" . '$this->getMockController()->invoke(\'__construct\', $arguments);' . PHP_EOL .
  2198. "\t\t" . '}' . PHP_EOL .
  2199. "\t\t" . 'else' . PHP_EOL .
  2200. "\t\t" . '{' . PHP_EOL .
  2201. "\t\t\t" . '$this->getMockController()->addCall(\'__construct\', $arguments);' . PHP_EOL .
  2202. "\t\t\t" . 'call_user_func_array(\'parent::__construct\', $arguments);' . PHP_EOL .
  2203. "\t\t" . '}' . PHP_EOL .
  2204. "\t" . '}' . PHP_EOL .
  2205. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  2206. "\t" . '{' . PHP_EOL .
  2207. "\t\t" . 'return ' . var_export(array('__construct'), true) . ';' . PHP_EOL .
  2208. "\t" . '}' . PHP_EOL .
  2209. '}' . PHP_EOL .
  2210. '}'
  2211. )
  2212. ;
  2213. }
  2214. /** @php 5.6 */
  2215. public function testGetMockedClassCodeForClassWithVariadicArgumentsInConstruct()
  2216. {
  2217. $this
  2218. ->if($generator = new testedClass())
  2219. ->and($reflectionParameterController = new mock\controller())
  2220. ->and($reflectionParameterController->__construct = function() {})
  2221. ->and($reflectionParameterController->isArray = false)
  2222. ->and($reflectionParameterController->isCallable = false)
  2223. ->and($reflectionParameterController->getName = 'variadic')
  2224. ->and($reflectionParameterController->isPassedByReference = false)
  2225. ->and($reflectionParameterController->isDefaultValueAvailable = false)
  2226. ->and($reflectionParameterController->isOptional = false)
  2227. ->and($reflectionParameterController->isVariadic = true)
  2228. ->and($reflectionParameterController->getClass = null)
  2229. ->and($reflectionParameter = new \mock\reflectionParameter(null, null))
  2230. ->and($reflectionMethodController = new mock\controller())
  2231. ->and($reflectionMethodController->__construct = function() {})
  2232. ->and($reflectionMethodController->getName = '__construct')
  2233. ->and($reflectionMethodController->isConstructor = true)
  2234. ->and($reflectionMethodController->getParameters = array($reflectionParameter))
  2235. ->and($reflectionMethodController->isPublic = true)
  2236. ->and($reflectionMethodController->isProtected = false)
  2237. ->and($reflectionMethodController->isPrivate = false)
  2238. ->and($reflectionMethodController->isFinal = false)
  2239. ->and($reflectionMethodController->isStatic = false)
  2240. ->and($reflectionMethodController->isAbstract = false)
  2241. ->and($reflectionMethodController->returnsReference = false)
  2242. ->and($reflectionMethod = new \mock\reflectionMethod(null, null))
  2243. ->and($reflectionClassController = new mock\controller())
  2244. ->and($reflectionClassController->__construct = function() {})
  2245. ->and($reflectionClassController->getName = function() use (& $realClass) { return $realClass; })
  2246. ->and($reflectionClassController->isFinal = false)
  2247. ->and($reflectionClassController->isInterface = false)
  2248. ->and($reflectionClassController->getMethods = array($reflectionMethod))
  2249. ->and($reflectionClassController->getConstructor = $reflectionMethod)
  2250. ->and($reflectionClassController->isAbstract = false)
  2251. ->and($reflectionClass = new \mock\reflectionClass(null))
  2252. ->and($generator->setReflectionClassFactory(function() use ($reflectionClass) { return $reflectionClass; }))
  2253. ->and($adapter = new atoum\test\adapter())
  2254. ->and($adapter->class_exists = function($class) use (& $realClass) { return ($class == '\\' . $realClass); })
  2255. ->and($generator->setAdapter($adapter))
  2256. ->then
  2257. ->string($generator->getMockedClassCode($realClass = uniqid()))->isEqualTo(
  2258. 'namespace mock {' . PHP_EOL .
  2259. 'final class ' . $realClass . ' extends \\' . $realClass . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  2260. '{' . PHP_EOL .
  2261. $this->getMockControllerMethods() .
  2262. "\t" . 'public function __construct(... $variadic)' . PHP_EOL .
  2263. "\t" . '{' . PHP_EOL .
  2264. "\t\t" . '$arguments = func_get_args();' . PHP_EOL .
  2265. "\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  2266. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  2267. "\t\t" . '{' . PHP_EOL .
  2268. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  2269. "\t\t" . '}' . PHP_EOL .
  2270. "\t\t" . 'if (isset($this->getMockController()->__construct) === true)' . PHP_EOL .
  2271. "\t\t" . '{' . PHP_EOL .
  2272. "\t\t\t" . '$this->getMockController()->invoke(\'__construct\', $arguments);' . PHP_EOL .
  2273. "\t\t" . '}' . PHP_EOL .
  2274. "\t\t" . 'else' . PHP_EOL .
  2275. "\t\t" . '{' . PHP_EOL .
  2276. "\t\t\t" . '$this->getMockController()->addCall(\'__construct\', $arguments);' . PHP_EOL .
  2277. "\t\t\t" . 'call_user_func_array(\'parent::__construct\', $arguments);' . PHP_EOL .
  2278. "\t\t" . '}' . PHP_EOL .
  2279. "\t" . '}' . PHP_EOL .
  2280. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  2281. "\t" . '{' . PHP_EOL .
  2282. "\t\t" . 'return ' . var_export(array('__construct'), true) . ';' . PHP_EOL .
  2283. "\t" . '}' . PHP_EOL .
  2284. '}' . PHP_EOL .
  2285. '}'
  2286. )
  2287. ;
  2288. }
  2289. /** @php 5.6 */
  2290. public function testGetMockedClassCodeForClassWithOnlyVariadicArgumentsInMethod()
  2291. {
  2292. $this
  2293. ->if($generator = new testedClass())
  2294. ->and($reflectionParameterController = new mock\controller())
  2295. ->and($reflectionParameterController->__construct = function() {})
  2296. ->and($reflectionParameterController->isArray = false)
  2297. ->and($reflectionParameterController->isCallable = false)
  2298. ->and($reflectionParameterController->getName = 'variadic')
  2299. ->and($reflectionParameterController->isPassedByReference = false)
  2300. ->and($reflectionParameterController->isDefaultValueAvailable = false)
  2301. ->and($reflectionParameterController->isOptional = false)
  2302. ->and($reflectionParameterController->isVariadic = true)
  2303. ->and($reflectionParameterController->getClass = null)
  2304. ->and($reflectionParameter = new \mock\reflectionParameter(null, null))
  2305. ->and($reflectionMethodController = new mock\controller())
  2306. ->and($reflectionMethodController->__construct = function() {})
  2307. ->and($reflectionMethodController->getName = $methodName = uniqid())
  2308. ->and($reflectionMethodController->isConstructor = false)
  2309. ->and($reflectionMethodController->getParameters = array($reflectionParameter))
  2310. ->and($reflectionMethodController->isPublic = true)
  2311. ->and($reflectionMethodController->isProtected = false)
  2312. ->and($reflectionMethodController->isPrivate = false)
  2313. ->and($reflectionMethodController->isFinal = false)
  2314. ->and($reflectionMethodController->isStatic = false)
  2315. ->and($reflectionMethodController->isAbstract = false)
  2316. ->and($reflectionMethodController->returnsReference = false)
  2317. ->and($reflectionMethod = new \mock\reflectionMethod(null, null))
  2318. ->and($reflectionClassController = new mock\controller())
  2319. ->and($reflectionClassController->__construct = function() {})
  2320. ->and($reflectionClassController->getName = function() use (& $realClass) { return $realClass; })
  2321. ->and($reflectionClassController->isFinal = false)
  2322. ->and($reflectionClassController->isInterface = false)
  2323. ->and($reflectionClassController->getMethods = array($reflectionMethod))
  2324. ->and($reflectionClassController->getConstructor = null)
  2325. ->and($reflectionClassController->isAbstract = false)
  2326. ->and($reflectionClass = new \mock\reflectionClass(null))
  2327. ->and($generator->setReflectionClassFactory(function() use ($reflectionClass) { return $reflectionClass; }))
  2328. ->and($adapter = new atoum\test\adapter())
  2329. ->and($adapter->class_exists = function($class) use (& $realClass) { return ($class == '\\' . $realClass); })
  2330. ->and($generator->setAdapter($adapter))
  2331. ->then
  2332. ->string($generator->getMockedClassCode($realClass = uniqid()))->isEqualTo(
  2333. 'namespace mock {' . PHP_EOL .
  2334. 'final class ' . $realClass . ' extends \\' . $realClass . ' implements \mageekguy\atoum\mock\aggregator' . PHP_EOL .
  2335. '{' . PHP_EOL .
  2336. $this->getMockControllerMethods() .
  2337. "\t" . 'public function __construct(\mageekguy\atoum\mock\controller $mockController = null)' . PHP_EOL .
  2338. "\t" . '{' . PHP_EOL .
  2339. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  2340. "\t\t" . '{' . PHP_EOL .
  2341. "\t\t\t" . '$mockController = \mageekguy\atoum\mock\controller::get();' . PHP_EOL .
  2342. "\t\t" . '}' . PHP_EOL .
  2343. "\t\t" . 'if ($mockController !== null)' . PHP_EOL .
  2344. "\t\t" . '{' . PHP_EOL .
  2345. "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL .
  2346. "\t\t" . '}' . PHP_EOL .
  2347. "\t\t" . 'if (isset($this->getMockController()->__construct) === true)' . PHP_EOL .
  2348. "\t\t" . '{' . PHP_EOL .
  2349. "\t\t\t" . '$this->getMockController()->invoke(\'__construct\', func_get_args());' . PHP_EOL .
  2350. "\t\t" . '}' . PHP_EOL .
  2351. "\t" . '}' . PHP_EOL .
  2352. "\t" . 'public function ' . $methodName . '(... $variadic)' . PHP_EOL .
  2353. "\t" . '{' . PHP_EOL .
  2354. "\t\t" . '$arguments = func_get_args();' . PHP_EOL .
  2355. "\t\t" . 'if (isset($this->getMockController()->' . $methodName . ') === true)' . PHP_EOL .
  2356. "\t\t" . '{' . PHP_EOL .
  2357. "\t\t\t" . '$return = $this->getMockController()->invoke(\'' . $methodName . '\', $arguments);' . PHP_EOL .
  2358. "\t\t\t" . 'return $return;' . PHP_EOL .
  2359. "\t\t" . '}' . PHP_EOL .
  2360. "\t\t" . 'else' . PHP_EOL .
  2361. "\t\t" . '{' . PHP_EOL .
  2362. "\t\t\t" . '$this->getMockController()->addCall(\'' . $methodName . '\', $arguments);' . PHP_EOL .
  2363. "\t\t\t" . '$return = call_user_func_array(\'parent::' . $methodName . '\', $arguments);' . PHP_EOL .
  2364. "\t\t\t" . 'return $return;' . PHP_EOL .
  2365. "\t\t" . '}' . PHP_EOL .
  2366. "\t" . '}' . PHP_EOL .
  2367. "\t" . 'public static function getMockedMethods()' . PHP_EOL .
  2368. "\t" . '{' . PHP_EOL .
  2369. "\t\t" . 'return ' . var_export(array('__construct', $methodName), true) . ';' . PHP_EOL .
  2370. "\t" . '}' . PHP_EOL .
  2371. '}' . PHP_EOL .
  2372. '}'
  2373. )
  2374. ;
  2375. }
  2376. protected function getMockControllerMethods()
  2377. {
  2378. return
  2379. "\t" . 'public function getMockController()' . PHP_EOL .
  2380. "\t" . '{' . PHP_EOL .
  2381. "\t\t" . '$mockController = \mageekguy\atoum\mock\controller::getForMock($this);' . PHP_EOL .
  2382. "\t\t" . 'if ($mockController === null)' . PHP_EOL .
  2383. "\t\t" . '{' . PHP_EOL .
  2384. "\t\t\t" . '$this->setMockController($mockController = new \mageekguy\atoum\mock\controller());' . PHP_EOL .
  2385. "\t\t" . '}' . PHP_EOL .
  2386. "\t\t" . 'return $mockController;' . PHP_EOL .
  2387. "\t" . '}' . PHP_EOL .
  2388. "\t" . 'public function setMockController(\mageekguy\atoum\mock\controller $controller)' . PHP_EOL .
  2389. "\t" . '{' . PHP_EOL .
  2390. "\t\t" . 'return $controller->control($this);' . PHP_EOL .
  2391. "\t" . '}' . PHP_EOL .
  2392. "\t" . 'public function resetMockController()' . PHP_EOL .
  2393. "\t" . '{' . PHP_EOL .
  2394. "\t\t" . '\mageekguy\atoum\mock\controller::getForMock($this)->reset();' . PHP_EOL .
  2395. "\t\t" . 'return $this;' . PHP_EOL .
  2396. "\t" . '}' . PHP_EOL
  2397. ;
  2398. }
  2399. protected function testMethodIsMockableWithReservedWordDataProvider()
  2400. {
  2401. # See http://www.php.net/manual/en/reserved.keywords.php
  2402. return array(
  2403. '__halt_compiler',
  2404. 'abstract',
  2405. 'and',
  2406. 'array',
  2407. 'as',
  2408. 'break',
  2409. 'callable',
  2410. 'case',
  2411. 'catch',
  2412. 'class',
  2413. 'clone',
  2414. 'const',
  2415. 'continue',
  2416. 'declare',
  2417. 'default',
  2418. 'die',
  2419. 'do',
  2420. 'echo',
  2421. 'else',
  2422. 'elseif',
  2423. 'empty',
  2424. 'enddeclare',
  2425. 'endfor',
  2426. 'endforeach',
  2427. 'endif',
  2428. 'endswitch',
  2429. 'endwhile',
  2430. 'eval',
  2431. 'exit',
  2432. 'extends',
  2433. 'final',
  2434. 'for',
  2435. 'foreach',
  2436. 'function',
  2437. 'global',
  2438. 'goto',
  2439. 'if',
  2440. 'implements',
  2441. 'include',
  2442. 'include_once',
  2443. 'instanceof',
  2444. 'insteadof',
  2445. 'interface',
  2446. 'isset',
  2447. 'list',
  2448. 'namespace',
  2449. 'new',
  2450. 'or',
  2451. 'print',
  2452. 'private',
  2453. 'protected',
  2454. 'public',
  2455. 'require',
  2456. 'require_once',
  2457. 'return',
  2458. 'static',
  2459. 'switch',
  2460. 'throw',
  2461. 'trait',
  2462. 'try',
  2463. 'unset',
  2464. 'use',
  2465. 'var',
  2466. 'while',
  2467. 'xor'
  2468. );
  2469. }
  2470. }