PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/units/classes/asserters/phpClass.php

http://github.com/mageekguy/atoum
PHP | 547 lines | 465 code | 82 blank | 0 comment | 0 complexity | 3c9f272614b7b1ff8e2d4547baa9a7d6 MD5 | raw file
  1. <?php
  2. namespace mageekguy\atoum\tests\units\asserters;
  3. use
  4. mageekguy\atoum,
  5. mageekguy\atoum\asserter,
  6. mageekguy\atoum\tools\variable
  7. ;
  8. require_once __DIR__ . '/../../runner.php';
  9. class phpClass extends atoum\test
  10. {
  11. public function beforeTestMethod($testMethod)
  12. {
  13. $this->mockGenerator
  14. ->shunt('__construct')
  15. ;
  16. }
  17. public function testClass()
  18. {
  19. $this->testedClass->extends('mageekguy\atoum\asserter');
  20. }
  21. public function test__construct()
  22. {
  23. $this
  24. ->given($this->newTestedInstance)
  25. ->then
  26. ->object($this->testedInstance->getGenerator())->isEqualTo(new asserter\generator())
  27. ->object($this->testedInstance->getAnalyzer())->isEqualTo(new variable\analyzer())
  28. ->object($this->testedInstance->getLocale())->isEqualTo(new atoum\locale())
  29. ->if($this->newTestedInstance($generator = new asserter\generator(), $analyzer = new variable\analyzer(), $locale = new atoum\locale()))
  30. ->then
  31. ->object($this->testedInstance->getGenerator())->isIdenticalTo($generator)
  32. ->object($this->testedInstance->getAnalyzer())->isIdenticalTo($analyzer)
  33. ->object($this->testedInstance->getLocale())->isIdenticalTo($locale)
  34. ;
  35. }
  36. public function test__toString()
  37. {
  38. $this
  39. ->given($this->newTestedInstance)
  40. ->then
  41. ->castToString($this->testedInstance)->isEmpty
  42. ->if(
  43. $this->testedInstance
  44. ->setReflectionClassInjector(function($class) {
  45. $mockController = new atoum\mock\controller();
  46. $mockController->__construct->doesNothing();
  47. $mockController->getName = $class;
  48. return new \mock\reflectionClass($class, $mockController);
  49. }
  50. ),
  51. $this->testedInstance->setWith($class = uniqid())
  52. )
  53. ->then
  54. ->castToString($this->testedInstance)->isEqualTo($class)
  55. ;
  56. }
  57. public function testGetClass()
  58. {
  59. $this
  60. ->given($this->newTestedInstance)
  61. ->then
  62. ->variable($this->testedInstance->getClass())->isNull()
  63. ->if($this->testedInstance->setWith(__CLASS__))
  64. ->then
  65. ->string($this->testedInstance->getClass())->isEqualTo(__CLASS__)
  66. ;
  67. }
  68. public function testSetReflectionClassInjector()
  69. {
  70. $this
  71. ->given($asserter = $this->newTestedInstance)
  72. ->then
  73. ->object($this->testedInstance->setReflectionClassInjector(function($class) use (& $reflectionClass) { return ($reflectionClass = new \mock\reflectionClass($class)); }))->isTestedInstance
  74. ->object($this->testedInstance->getReflectionClass($class = uniqid()))->isIdenticalTo($reflectionClass)
  75. ->exception(function() use ($asserter) { $asserter->setReflectionClassInjector(function() {}); })
  76. ->isInstanceOf('mageekguy\atoum\exceptions\logic\invalidArgument')
  77. ->hasMessage('Reflection class injector must take one argument')
  78. ;
  79. }
  80. public function testGetReflectionClass()
  81. {
  82. $this
  83. ->given($asserter = $this->newTestedInstance)
  84. ->then
  85. ->object($this->testedInstance->getReflectionClass(__CLASS__))->isInstanceOf('reflectionClass')
  86. ->string($this->testedInstance->getReflectionClass(__CLASS__)->getName())->isEqualTo(__CLASS__)
  87. ->if($this->testedInstance->setReflectionClassInjector(function($class) use (& $reflectionClass) { return ($reflectionClass = new \mock\reflectionClass($class)); }))
  88. ->then
  89. ->object($this->testedInstance->getReflectionClass($class = uniqid()))->isIdenticalTo($reflectionClass)
  90. ->mock($reflectionClass)->call('__construct')->withArguments($class)->once
  91. ->if($asserter->setReflectionClassInjector(function($class) use (& $reflectionClass) { return uniqid(); }))
  92. ->then
  93. ->exception(function() use ($asserter) { $asserter->getReflectionClass(uniqid()); })
  94. ->isInstanceOf('mageekguy\atoum\exceptions\runtime\unexpectedValue')
  95. ->hasMessage('Reflection class injector must return a \reflectionClass instance')
  96. ;
  97. }
  98. public function testSetWith()
  99. {
  100. $this
  101. ->given($asserter = $this->newTestedInstance)
  102. ->if(
  103. $this->testedInstance
  104. ->setReflectionClassInjector(function($class) {
  105. $mockController = new atoum\mock\controller();
  106. $mockController->__construct = function() { throw new \reflectionException();};
  107. return new \mock\reflectionClass($class, $mockController);
  108. }
  109. )
  110. ->setLocale($locale = new \mock\atoum\locale()),
  111. $this->calling($locale)->_ = $notExists = uniqid()
  112. )
  113. ->then
  114. ->exception(function() use ($asserter, & $class) { $asserter->setWith($class = uniqid()); })
  115. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  116. ->hasMessage($notExists)
  117. ->mock($locale)->call('_')->withArguments('Class \'%s\' does not exist', $class)->once
  118. ->if(
  119. $this->testedInstance
  120. ->setReflectionClassInjector(function($class) {
  121. $mockController = new atoum\mock\controller();
  122. $mockController->__construct->doesNothing();
  123. $mockController->getName = $class;
  124. return new \mock\reflectionClass($class, $mockController);
  125. }
  126. )
  127. )
  128. ->then
  129. ->object($this->testedInstance->setWith($class = uniqid()))->isTestedInstance
  130. ->string($this->testedInstance->getClass())->isEqualTo($class)
  131. ;
  132. }
  133. public function testHasParent()
  134. {
  135. $this
  136. ->given($asserter = $this->newTestedInstance)
  137. ->then
  138. ->exception(function() use ($asserter) { $asserter->hasParent(uniqid()); })
  139. ->isInstanceOf('logicException')
  140. ->hasMessage('Class is undefined')
  141. ->if(
  142. $this->testedInstance
  143. ->setReflectionClassInjector(function($class) use (& $parent) {
  144. $parentMockController = new atoum\mock\controller();
  145. $parentMockController->getName = $parent = uniqid();
  146. $mockController = new atoum\mock\controller();
  147. $mockController->getName = $class;
  148. $mockController->getParentClass = new \mock\reflectionClass(uniqid(), $parentMockController);
  149. return new \mock\reflectionClass($class, $mockController);
  150. }
  151. )
  152. ->setWith(uniqid())
  153. ->setLocale($locale = new \mock\atoum\locale()),
  154. $this->calling($locale)->_ = $isNotChild = uniqid()
  155. )
  156. ->then
  157. ->exception(function() use ($asserter, & $notParent) { $asserter->hasParent($notParent = uniqid()); })
  158. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  159. ->hasMessage($isNotChild)
  160. ->mock($locale)->call('_')->withArguments('%s is not the parent of class %s', $notParent, $asserter)->once
  161. ->exception(function() use ($asserter, & $failMessage) { $asserter->hasParent(uniqid(), $failMessage = uniqid()); })
  162. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  163. ->hasMessage($failMessage)
  164. ->object($this->testedInstance->hasParent($parent))->isTestedInstance
  165. ->object($this->testedInstance->hasParent(strtoupper($parent)))->isTestedInstance
  166. ;
  167. }
  168. public function testHasNoParent()
  169. {
  170. $this
  171. ->given($asserter = $this->newTestedInstance)
  172. ->then
  173. ->exception(function() use ($asserter) { $asserter->hasNoParent(); })
  174. ->isInstanceOf('logicException')
  175. ->hasMessage('Class is undefined')
  176. ->exception(function() use ($asserter) { $asserter->hasNoParent; })
  177. ->isInstanceOf('logicException')
  178. ->hasMessage('Class is undefined')
  179. ->if(
  180. $reflectionClass = new \mock\reflectionClass($className = uniqid()),
  181. $this->calling($reflectionClass)->getName = $className,
  182. $this->calling($reflectionClass)->getParentClass = false,
  183. $asserter
  184. ->setReflectionClassInjector(function($class) use ($reflectionClass) { return $reflectionClass; })
  185. ->setWith($class = uniqid())
  186. )
  187. ->then
  188. ->object($asserter->hasNoParent())->isIdenticalTo($asserter)
  189. ->object($asserter->hasNoParent)->isIdenticalTo($asserter)
  190. ->if(
  191. $this->calling($reflectionClass)->getParentClass = $parentClass = new \mock\reflectionClass(uniqid()),
  192. $this->calling($parentClass)->__toString = $parentClassName = uniqid(),
  193. $this->testedInstance->setLocale($locale = new \mock\atoum\locale()),
  194. $this->calling($locale)->_ = $hasParent = uniqid()
  195. )
  196. ->then
  197. ->exception(function() use ($asserter) { $asserter->hasNoParent(); })
  198. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  199. ->hasMessage($hasParent)
  200. ->mock($locale)->call('_')->withArguments('%s has parent %s', $asserter, $parentClass)->once
  201. ->exception(function() use ($asserter, & $failMessage) { $asserter->hasNoParent($failMessage = uniqid()); })
  202. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  203. ->hasMessage($failMessage)
  204. ->exception(function() use ($asserter) { $asserter->hasNoParent; })
  205. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  206. ->hasMessage($hasParent)
  207. ->mock($locale)->call('_')->withArguments('%s has parent %s', $asserter, $parentClass)->twice
  208. ;
  209. }
  210. public function testIsSubclassOf()
  211. {
  212. $this
  213. ->given($asserter = $this->newTestedInstance)
  214. ->then
  215. ->exception(function() use ($asserter) { $asserter->isSubclassOf(uniqid()); })
  216. ->isInstanceOf('logicException')
  217. ->hasMessage('Class is undefined')
  218. ->exception(function() use ($asserter) { $asserter->extends(uniqid()); })
  219. ->isInstanceOf('logicException')
  220. ->hasMessage('Class is undefined')
  221. ->exception(function() use ($asserter) { $asserter->exTENDs(uniqid()); })
  222. ->isInstanceOf('logicException')
  223. ->hasMessage('Class is undefined')
  224. ->given(
  225. $this->testedInstance
  226. ->setReflectionClassInjector(function($class) use (& $reflectionClass) {
  227. $mockController = new atoum\mock\controller();
  228. $mockController->__construct = function() {};
  229. $mockController->getName = $class;
  230. return $reflectionClass = new \mock\reflectionClass($class, $mockController);
  231. }
  232. )
  233. ->setWith(uniqid())
  234. ->setLocale($locale = new \mock\atoum\locale()),
  235. $this->calling($locale)->_ = $isNotSubclass = uniqid()
  236. )
  237. ->if($this->calling($reflectionClass)->isSubclassOf = false)
  238. ->then
  239. ->exception(function() use ($asserter, & $parentClass) { $asserter->isSubclassOf($parentClass = uniqid()); })
  240. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  241. ->hasMessage($isNotSubclass)
  242. ->mock($locale)->call('_')->withArguments('%s does not extend %s', $asserter, $parentClass)->once
  243. ->exception(function() use ($asserter, & $parentClass) { $asserter->extends($parentClass = uniqid()); })
  244. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  245. ->hasMessage($isNotSubclass)
  246. ->mock($locale)->call('_')->withArguments('%s does not extend %s', $asserter, $parentClass)->once
  247. ->exception(function() use ($asserter, & $failMessage) { $asserter->isSubclassOf(uniqid(), $failMessage = uniqid()); })
  248. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  249. ->hasMessage($failMessage)
  250. ->exception(function() use ($asserter, & $failMessage) { $asserter->extends(uniqid(), $failMessage = uniqid()); })
  251. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  252. ->hasMessage($failMessage)
  253. ->if($this->calling($reflectionClass)->isSubclassOf = true)
  254. ->then
  255. ->object($this->testedInstance->isSubclassOf(uniqid()))->isTestedInstance
  256. ->object($this->testedInstance->extends(uniqid()))->isTestedInstance
  257. ;
  258. }
  259. public function testHasInterface()
  260. {
  261. $this
  262. ->given($asserter = $this->newTestedInstance)
  263. ->then
  264. ->exception(function() use ($asserter) { $asserter->hasInterface(uniqid()); })
  265. ->isInstanceOf('logicException')
  266. ->hasMessage('Class is undefined')
  267. ->exception(function() use ($asserter) { $asserter->implements(uniqid()); })
  268. ->isInstanceOf('logicException')
  269. ->hasMessage('Class is undefined')
  270. ->exception(function() use ($asserter) { $asserter->imPLEMENts(uniqid()); })
  271. ->isInstanceOf('logicException')
  272. ->hasMessage('Class is undefined')
  273. ->given(
  274. $this->testedInstance
  275. ->setReflectionClassInjector(function($class) use (& $reflectionClass) {
  276. $mockController = new atoum\mock\controller();
  277. $mockController->__construct = function() {};
  278. $mockController->getName = $class;
  279. return $reflectionClass = new \mock\reflectionClass($class, $mockController);
  280. }
  281. )
  282. ->setWith(uniqid())
  283. ->setLocale($locale = new \mock\atoum\locale()),
  284. $this->calling($locale)->_ = $notImplements = uniqid()
  285. )
  286. ->if($this->calling($reflectionClass)->implementsInterface = false)
  287. ->then
  288. ->exception(function() use ($asserter, & $interface) { $asserter->hasInterface($interface = uniqid()); })
  289. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  290. ->hasMessage($notImplements)
  291. ->mock($locale)->call('_')->withArguments('%s does not implement %s', $asserter, $interface)->once
  292. ->exception(function() use ($asserter, & $failMessage) { $asserter->hasInterface(uniqid(), $failMessage = uniqid()); })
  293. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  294. ->hasMessage($failMessage)
  295. ->exception(function() use ($asserter, & $interface) { $asserter->implements($interface = uniqid()); })
  296. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  297. ->hasMessage($notImplements)
  298. ->mock($locale)->call('_')->withArguments('%s does not implement %s', $asserter, $interface)->once
  299. ->exception(function() use ($asserter, & $failMessage) { $asserter->implements(uniqid(), $failMessage = uniqid()); })
  300. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  301. ->hasMessage($failMessage)
  302. ->if($this->calling($reflectionClass)->implementsInterface = true)
  303. ->then
  304. ->object($this->testedInstance->hasInterface(uniqid()))->isTestedInstance
  305. ->object($this->testedInstance->implements(uniqid()))->isTestedInstance
  306. ;
  307. }
  308. public function testIsAbstract()
  309. {
  310. $this
  311. ->given($asserter = $this->newTestedInstance)
  312. ->then
  313. ->exception(function() use ($asserter) { $asserter->isAbstract(); })
  314. ->isInstanceOf('logicException')
  315. ->hasMessage('Class is undefined')
  316. ->exception(function() use ($asserter) { $asserter->isAbstract; })
  317. ->isInstanceOf('logicException')
  318. ->hasMessage('Class is undefined')
  319. ->given(
  320. $this->testedInstance
  321. ->setReflectionClassInjector(function($class) use (& $reflectionClass) {
  322. $mockController = new atoum\mock\controller();
  323. $mockController->__construct = function() {};
  324. $mockController->getName = $class;
  325. return $reflectionClass = new \mock\reflectionClass($class, $mockController);
  326. }
  327. )
  328. ->setWith(uniqid())
  329. ->setLocale($locale = new \mock\atoum\locale()),
  330. $this->calling($locale)->_ = $notAbstract = uniqid()
  331. )
  332. ->if($this->calling($reflectionClass)->isAbstract = false)
  333. ->then
  334. ->exception(function() use ($asserter) { $asserter->isAbstract(); })
  335. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  336. ->hasMessage($notAbstract)
  337. ->mock($locale)->call('_')->withArguments('%s is not abstract', $asserter)->once
  338. ->exception(function() use ($asserter, & $failMessage) { $asserter->isAbstract($failMessage = uniqid()); })
  339. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  340. ->hasMessage($failMessage)
  341. ->exception(function() use ($asserter) { $asserter->isAbstract; })
  342. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  343. ->hasMessage($notAbstract)
  344. ->mock($locale)->call('_')->withArguments('%s is not abstract', $asserter)->twice
  345. ->if($this->calling($reflectionClass)->isAbstract = true)
  346. ->object($this->testedInstance->isAbstract())->isTestedInstance
  347. ->object($this->testedInstance->isAbstract)->isTestedInstance
  348. ;
  349. }
  350. public function testIsFinal()
  351. {
  352. $this
  353. ->given($asserter = $this->newTestedInstance)
  354. ->then
  355. ->exception(function() use ($asserter) { $asserter->isFinal(); })
  356. ->isInstanceOf('logicException')
  357. ->hasMessage('Class is undefined')
  358. ->given(
  359. $this->testedInstance
  360. ->setReflectionClassInjector(function($class) use (& $reflectionClass) {
  361. $mockController = new atoum\mock\controller();
  362. $mockController->__construct = function() {};
  363. $mockController->getName = $class;
  364. return $reflectionClass = new \mock\reflectionClass($class, $mockController);
  365. }
  366. )
  367. ->setWith(uniqid())
  368. ->setLocale($locale = new \mock\atoum\locale()),
  369. $this->calling($locale)->_ = $notFinal = uniqid()
  370. )
  371. ->if($this->calling($reflectionClass)->isFinal = false)
  372. ->then
  373. ->exception(function() use ($asserter) { $asserter->isFinal(); })
  374. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  375. ->hasMessage($notFinal)
  376. ->mock($locale)->call('_')->withArguments('%s is not final', $asserter)->once
  377. ->exception(function() use ($asserter, & $failMessage) { $asserter->isFinal($failMessage = uniqid()); })
  378. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  379. ->hasMessage($failMessage)
  380. ->exception(function() use ($asserter) { $asserter->isFinal; })
  381. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  382. ->hasMessage($notFinal)
  383. ->mock($locale)->call('_')->withArguments('%s is not final', $asserter)->twice
  384. ->if($this->calling($reflectionClass)->isFinal = true)
  385. ->object($this->testedInstance->isFinal())->isTestedInstance
  386. ->object($this->testedInstance->isFinal)->isTestedInstance
  387. ;
  388. }
  389. public function testHasMethod()
  390. {
  391. $this
  392. ->if($asserter = $this->newTestedInstance)
  393. ->then
  394. ->exception(function() use ($asserter) { $asserter->hasMethod(uniqid()); })
  395. ->isInstanceOf('logicException')
  396. ->hasMessage('Class is undefined')
  397. ->given(
  398. $this->testedInstance
  399. ->setReflectionClassInjector(function($class) use (& $reflectionClass) {
  400. $mockController = new atoum\mock\controller();
  401. $mockController->__construct = function() {};
  402. $mockController->getName = $class;
  403. return $reflectionClass = new \mock\reflectionClass($class, $mockController);
  404. }
  405. )
  406. ->setWith(uniqid())
  407. ->setLocale($locale = new \mock\atoum\locale()),
  408. $this->calling($locale)->_ = $methodUnknown = uniqid()
  409. )
  410. ->if($this->calling($reflectionClass)->hasMethod = false)
  411. ->then
  412. ->exception(function() use ($asserter, & $method) { $asserter->hasMethod($method = uniqid()); })
  413. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  414. ->hasMessage($methodUnknown)
  415. ->mock($locale)->call('_')->withArguments('%s::%s() does not exist', $asserter, $method)->once
  416. ->exception(function() use ($asserter, & $failMessage) { $asserter->hasMethod(uniqid(), $failMessage = uniqid()); })
  417. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  418. ->hasMessage($failMessage)
  419. ->if($this->calling($reflectionClass)->hasMethod = true)
  420. ->then
  421. ->object($this->testedInstance->hasMethod(uniqid()))->isTestedInstance
  422. ;
  423. }
  424. public function testHasConstant()
  425. {
  426. $this
  427. ->if($asserter = $this->newTestedInstance)
  428. ->then
  429. ->exception(function() use ($asserter) { $asserter->hasConstant(uniqid()); })
  430. ->isInstanceOf('logicException')
  431. ->hasMessage('Class is undefined')
  432. ->given(
  433. $this->testedInstance
  434. ->setReflectionClassInjector(function($class) use (& $reflectionClass) {
  435. $mockController = new atoum\mock\controller();
  436. $mockController->__construct = function() {};
  437. $mockController->getName = $class;
  438. return $reflectionClass = new \mock\reflectionClass($class, $mockController);
  439. }
  440. )
  441. ->setWith(uniqid())
  442. ->setLocale($locale = new \mock\atoum\locale()),
  443. $this->calling($locale)->_ = $constantUnknown = uniqid()
  444. )
  445. ->if($this->calling($reflectionClass)->hasConstant = false)
  446. ->then
  447. ->exception(function() use ($asserter, & $constant) { $asserter->hasConstant($constant = uniqid()); })
  448. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  449. ->hasMessage($constantUnknown)
  450. ->mock($locale)->call('_')->withArguments('%s::%s does not exist', $asserter, $constant)->once
  451. ->exception(function() use ($asserter, & $failMessage) { $asserter->hasConstant(uniqid(), $failMessage = uniqid()); })
  452. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  453. ->hasMessage($failMessage)
  454. ->if(
  455. $this->calling($reflectionClass)->hasConstant = true,
  456. $this->calling($reflectionClass)->getConstant = $constantObject = uniqid(),
  457. $this->testedInstance->setGenerator($generator = new \mock\atoum\asserter\generator()),
  458. $this->calling($generator)->getAsserterInstance = $asserter = uniqid()
  459. )
  460. ->then
  461. ->string($this->testedInstance->hasConstant($constant = uniqid()))->isEqualTo($asserter)
  462. ->mock($generator)->call('getAsserterInstance')->withArguments('constant', array($constantObject))->once
  463. ;
  464. }
  465. }