PageRenderTime 94ms CodeModel.GetById 23ms RepoModel.GetById 5ms 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

Large files files are truncated, but you can click here to view the full 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 fu…

Large files files are truncated, but you can click here to view the full file