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

/vendor/zendframework/zftool/tests/ZFToolTest/Diagnostics/DiagnosticsControllerTest.php

https://bitbucket.org/Evgenii/zf2t
PHP | 716 lines | 502 code | 112 blank | 102 comment | 0 complexity | 3d986d64c34f4baa4f85bd136d24f624 MD5 | raw file
  1. <?php
  2. namespace ZFToolTest\Diagnostics\Test;
  3. use ZFTool\Controller\DiagnosticsController;
  4. use ZFTool\Diagnostics\Exception\RuntimeException;
  5. use ZFTool\Diagnostics\Result\Failure;
  6. use ZFTool\Diagnostics\Result\Success;
  7. use ZFTool\Diagnostics\Result\Warning;
  8. use ZFTool\Diagnostics\Test\Callback;
  9. use ZFToolTest\Diagnostics\TestAsset\AlwaysSuccessTest;
  10. use ZFToolTest\Diagnostics\TestAsset\ReturnThisTest;
  11. use ZFToolTest\Diagnostics\TestAssets\ConsoleAdapter;
  12. use ZFToolTest\DummyModule;
  13. use ZFToolTest\TestAsset\InjectableModuleManager;
  14. use Zend\Console\Request as ConsoleRequest;
  15. use Zend\Mvc\MvcEvent;
  16. use Zend\Mvc\Router\RouteMatch;
  17. use Zend\ServiceManager\ServiceManager;
  18. use Zend\Stdlib\ArrayObject;
  19. use Zend\Stdlib\ArrayUtils;
  20. require_once __DIR__.'/TestAsset/ConsoleAdapter.php';
  21. require_once __DIR__.'/TestAsset/InjectableModuleManager.php';
  22. require_once __DIR__.'/TestAsset/ReturnThisTest.php';
  23. require_once __DIR__.'/TestAsset/AlwaysSuccessTest.php';
  24. require_once __DIR__.'/TestAsset/DummyModule.php';
  25. class DiagnosticsControllerTest extends \PHPUnit_Framework_TestCase
  26. {
  27. /**
  28. * @var ServiceManager
  29. */
  30. protected $sm;
  31. /**
  32. * @var InjectableModuleManager
  33. */
  34. protected $mm;
  35. /**
  36. * @var DiagnosticsController
  37. */
  38. protected $controller;
  39. /**
  40. * @var RouteMatch
  41. */
  42. protected $routeMatch;
  43. /**
  44. * @var ArrayObject
  45. */
  46. protected $config;
  47. protected static $staticTestMethodCalled = false;
  48. public function setup()
  49. {
  50. $this->config = new ArrayObject(array(
  51. 'diagnostics' => array()
  52. ));
  53. $this->sm = new ServiceManager();
  54. $this->sm->setService('console', new ConsoleAdapter());
  55. $this->sm->setService('config', $this->config);
  56. $this->sm->setAlias('configuration','config');
  57. $this->mm = new InjectableModuleManager();
  58. $this->sm->setService('modulemanager', $this->mm);
  59. $event = new MvcEvent();
  60. $this->routeMatch = new RouteMatch(array(
  61. 'controller' => 'ZFTools\Controller\Diagnostics',
  62. 'action' => 'run'
  63. ));
  64. $event->setRouteMatch($this->routeMatch);
  65. $this->controller = new DiagnosticsController();
  66. $this->controller->setServiceLocator($this->sm);
  67. $this->controller->setEvent($event);
  68. }
  69. public function invalidDefinitionsProvider()
  70. {
  71. $res = fopen('php://memory', 'r');
  72. fclose($res);
  73. return array(
  74. 'an empty array' => array(
  75. array(),
  76. 'Cannot use an empty array%a'
  77. ),
  78. 'an invalid test instance' => array(
  79. new \stdClass(),
  80. 'Cannot use object of class "stdClass"%a'
  81. ),
  82. 'an unknown definition type' => array(
  83. $res,
  84. 'Cannot understand diagnostic test definition %a'
  85. ),
  86. 'an invalid class name' => array(
  87. 'stdClass',
  88. 'The test object of class stdClass does not implement ZFTool\Diagnostics\Test\TestInterface'
  89. ),
  90. 'an unknown test identifier' => array(
  91. 'some\unknown\class\or\service\identifier',
  92. 'Cannot find test class or service with the name of "some\unknown\class\or\service\identifier"%a'
  93. )
  94. );
  95. }
  96. public function testEmptyResult()
  97. {
  98. $result = $this->controller->dispatch(new ConsoleRequest());
  99. $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
  100. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  101. $this->assertEquals(0, $result->getVariable('results')->count());
  102. }
  103. /**
  104. * 'diagnostics' => array(
  105. * 'group' => array(
  106. * 'test label' => new Test()
  107. * )
  108. * )
  109. */
  110. public function testConfigBasedTestInstance()
  111. {
  112. $expectedResult = new Success('bar');
  113. $test = new ReturnThisTest($expectedResult);
  114. $this->config['diagnostics']['group']['foo'] = $test;
  115. $result = $this->controller->dispatch(new ConsoleRequest());
  116. $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
  117. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  118. $results = $result->getVariable('results');
  119. $this->assertEquals(1, $results->count());
  120. $this->assertTrue($results->offsetExists($test));
  121. $this->assertSame($expectedResult, $results[$test]);
  122. $this->assertSame('group: foo', $test->getLabel());
  123. }
  124. /**
  125. * 'diagnostics' => array(
  126. * 'group' => array(
  127. * 'test label' => 'My\Namespace\ClassName'
  128. * )
  129. * )
  130. */
  131. public function testConfigBasedTestClassName()
  132. {
  133. $this->config['diagnostics']['group']['foo'] = 'ZFToolTest\Diagnostics\TestAsset\AlwaysSuccessTest';
  134. $result = $this->controller->dispatch(new ConsoleRequest());
  135. $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
  136. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  137. $results = $result->getVariable('results');
  138. $this->assertEquals(1, $results->count());
  139. $tests = ArrayUtils::iteratorToArray(($results));
  140. $test = array_pop($tests);
  141. $this->assertInstanceOf('ZFToolTest\Diagnostics\TestAsset\AlwaysSuccessTest', $test);
  142. $this->assertSame('group: foo', $test->getLabel());
  143. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Success', $results[$test]);
  144. }
  145. /**
  146. * 'diagnostics' => array(
  147. * 'group' => array(
  148. * 'test label' => array('My\Namespace\ClassName', 'methodName')
  149. * )
  150. * )
  151. */
  152. public function testConfigBasedStaticMethod()
  153. {
  154. static::$staticTestMethodCalled = false;
  155. $this->config['diagnostics']['group']['foo'] = array(__CLASS__, 'staticTestMethod');
  156. $result = $this->controller->dispatch(new ConsoleRequest());
  157. $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
  158. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  159. $results = $result->getVariable('results');
  160. $this->assertEquals(1, $results->count());
  161. $tests = ArrayUtils::iteratorToArray(($results));
  162. $test = array_pop($tests);
  163. $this->assertInstanceOf('ZFTool\Diagnostics\Test\Callback', $test);
  164. $this->assertTrue(static::$staticTestMethodCalled);
  165. $this->assertSame('group: foo', $test->getLabel());
  166. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Success', $results[$test]);
  167. $this->assertEquals('bar', $results[$test]->getMessage());
  168. }
  169. /**
  170. * 'diagnostics' => array(
  171. * 'group' => array(
  172. * 'test label' => array(
  173. * array('My\Namespace\ClassName', 'methodName'),
  174. * 'param1',
  175. * 'param2',
  176. * )
  177. * )
  178. * )
  179. */
  180. public function testConfigBasedStaticMethodWithParams()
  181. {
  182. static::$staticTestMethodCalled = false;
  183. $expectedData = mt_rand(1,PHP_INT_MAX);
  184. $expectedMessage = mt_rand(1,PHP_INT_MAX);
  185. $this->config['diagnostics']['group']['foo'] = array(
  186. array(__CLASS__, 'staticTestMethod'),
  187. $expectedMessage,
  188. $expectedData
  189. );
  190. $result = $this->controller->dispatch(new ConsoleRequest());
  191. $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
  192. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  193. $results = $result->getVariable('results');
  194. $this->assertEquals(1, $results->count());
  195. $tests = ArrayUtils::iteratorToArray(($results));
  196. $test = array_pop($tests);
  197. $this->assertInstanceOf('ZFTool\Diagnostics\Test\Callback', $test);
  198. $this->assertTrue(static::$staticTestMethodCalled);
  199. $this->assertSame('group: foo', $test->getLabel());
  200. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Success', $results[$test]);
  201. $this->assertEquals($expectedMessage, $results[$test]->getMessage());
  202. $this->assertEquals($expectedData, $results[$test]->getData());
  203. }
  204. /**
  205. * 'diagnostics' => array(
  206. * 'group' => array(
  207. * 'test label' => 'someFunctionName'
  208. * )
  209. * )
  210. */
  211. public function testConfigBasedFunction()
  212. {
  213. $this->config['diagnostics']['group']['foo'] = __NAMESPACE__ . '\testOutlineFunction';
  214. $result = $this->controller->dispatch(new ConsoleRequest());
  215. $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
  216. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  217. $results = $result->getVariable('results');
  218. $this->assertEquals(1, $results->count());
  219. $tests = ArrayUtils::iteratorToArray(($results));
  220. $test = array_pop($tests);
  221. $this->assertInstanceOf('ZFTool\Diagnostics\Test\Callback', $test);
  222. $this->assertSame('group: foo', $test->getLabel());
  223. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Success', $results[$test]);
  224. $this->assertEquals('bar', $results[$test]->getMessage());
  225. }
  226. /**
  227. * 'diagnostics' => array(
  228. * 'group' => array(
  229. * 'test label' => array('someFunctionName', 'param1', 'param2')
  230. * )
  231. * )
  232. */
  233. public function testConfigBasedFunctionWithParams()
  234. {
  235. $expectedData = mt_rand(1,PHP_INT_MAX);
  236. $expectedMessage = mt_rand(1,PHP_INT_MAX);
  237. $this->config['diagnostics']['group']['foo'] = array(
  238. __NAMESPACE__ . '\testOutlineFunction',
  239. $expectedMessage,
  240. $expectedData
  241. );
  242. $result = $this->controller->dispatch(new ConsoleRequest());
  243. $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
  244. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  245. $results = $result->getVariable('results');
  246. $this->assertEquals(1, $results->count());
  247. $tests = ArrayUtils::iteratorToArray(($results));
  248. $test = array_pop($tests);
  249. $this->assertInstanceOf('ZFTool\Diagnostics\Test\Callback', $test);
  250. $this->assertSame('group: foo', $test->getLabel());
  251. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Success', $results[$test]);
  252. $this->assertEquals($expectedMessage, $results[$test]->getMessage());
  253. $this->assertEquals($expectedData, $results[$test]->getData());
  254. }
  255. /**
  256. * 'diagnostics' => array(
  257. * 'group' => array(
  258. * 'test label' => array('ClassExists', 'params')
  259. * )
  260. * )
  261. */
  262. public function testConfigBasedBuiltinTest()
  263. {
  264. $this->config['diagnostics']['group']['foo'] = array('ClassExists', __CLASS__);
  265. $result = $this->controller->dispatch(new ConsoleRequest());
  266. $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
  267. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  268. $results = $result->getVariable('results');
  269. $this->assertEquals(1, $results->count());
  270. $tests = ArrayUtils::iteratorToArray(($results));
  271. $test = array_pop($tests);
  272. $this->assertInstanceOf('ZFTool\Diagnostics\Test\ClassExists', $test);
  273. $this->assertSame('group: foo', $test->getLabel());
  274. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Success', $results[$test]);
  275. }
  276. /**
  277. * 'diagnostics' => array(
  278. * 'group' => array(
  279. * 'test label' => 'Some\ServiceManager\Identifier'
  280. * )
  281. * ),
  282. * 'service_manager' => array(
  283. * 'invokables' => array(
  284. * 'Some\ServiceManager\Identifier' => 'Some\Test\Class'
  285. * )
  286. * )
  287. */
  288. public function testConfigBasedServiceName()
  289. {
  290. $expectedData = mt_rand(1,PHP_INT_MAX);
  291. $expectedMessage = mt_rand(1,PHP_INT_MAX);
  292. $test = new Callback(function () use ($expectedMessage, $expectedData) {
  293. return new Success($expectedMessage, $expectedData);
  294. });
  295. $this->sm->setService('ZFToolTest\TestService', $test);
  296. $this->config['diagnostics']['group']['foo'] = 'ZFToolTest\TestService';
  297. $result = $this->controller->dispatch(new ConsoleRequest());
  298. $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
  299. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  300. $results = $result->getVariable('results');
  301. $this->assertEquals(1, $results->count());
  302. $tests = ArrayUtils::iteratorToArray(($results));
  303. $this->assertSame($test, array_pop($tests));
  304. $this->assertSame('group: foo', $test->getLabel());
  305. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Success', $results[$test]);
  306. $this->assertEquals($expectedMessage, $results[$test]->getMessage());
  307. $this->assertEquals($expectedData, $results[$test]->getData());
  308. }
  309. /**
  310. * 'diagnostics' => array(
  311. * 'group' => array(
  312. * 'test label' => 'PhpVersion'
  313. * )
  314. * )
  315. */
  316. public function testBuiltInBeforeCallable()
  317. {
  318. $this->config['diagnostics']['group']['foo'] = array('PhpVersion', '1.0.0');
  319. $result = $this->controller->dispatch(new ConsoleRequest());
  320. $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
  321. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  322. $results = $result->getVariable('results');
  323. $this->assertEquals(1, $results->count());
  324. $tests = ArrayUtils::iteratorToArray(($results));
  325. $test = array_pop($tests);
  326. $this->assertInstanceOf('ZFTool\Diagnostics\Test\PhpVersion', $test);
  327. }
  328. public function testModuleProvidedDefinitions()
  329. {
  330. $module = new DummyModule($this->sm);
  331. $this->mm->injectModule('dummymodule',$module);
  332. $result = $this->controller->dispatch(new ConsoleRequest());
  333. $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
  334. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  335. $results = $result->getVariable('results');
  336. $this->assertEquals(5, $results->count());
  337. $expected = array(
  338. array('dummymodule: test1', 'ZFTool\Diagnostics\Result\Success', 'test1 success'),
  339. array('dummymodule: test2', 'ZFTool\Diagnostics\Result\Success', ''),
  340. array('dummymodule: test3', 'ZFTool\Diagnostics\Result\Failure', ''),
  341. array('dummymodule: test4', 'ZFTool\Diagnostics\Result\Failure', 'static test message'),
  342. array('dummymodule: test5', 'ZFTool\Diagnostics\Result\Failure', 'someOtherMessage'),
  343. );
  344. $x = 0;
  345. foreach($results as $test){
  346. $result = $results[$test];
  347. list($label, $class, $message) = $expected[$x++];
  348. error_reporting(E_ERROR);
  349. $this->assertInstanceOf('ZFTool\Diagnostics\Test\TestInterface', $test);
  350. $this->assertEquals($label, $test->getLabel());
  351. $this->assertEquals($message, $result->getMessage());
  352. $this->assertInstanceOf($class, $result);
  353. }
  354. }
  355. public function testTriggerAWarning()
  356. {
  357. $test = new Callback(function () {
  358. 1/0; // < throw a warning
  359. });
  360. $this->config['diagnostics']['group']['foo'] = $test;
  361. $result = $this->controller->dispatch(new ConsoleRequest());
  362. $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
  363. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  364. $results = $result->getVariable('results');
  365. $this->assertEquals(1, $results->count());
  366. $tests = ArrayUtils::iteratorToArray(($results));
  367. $this->assertSame($test, array_pop($tests));
  368. $this->assertSame('group: foo', $test->getLabel());
  369. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Failure', $results[$test]);
  370. }
  371. public function testThrowingAnException()
  372. {
  373. $e = new \Exception();
  374. $test = new Callback(function () use (&$e) {
  375. throw $e;
  376. });
  377. $this->config['diagnostics']['group']['foo'] = $test;
  378. $result = $this->controller->dispatch(new ConsoleRequest());
  379. $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
  380. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  381. $results = $result->getVariable('results');
  382. $this->assertEquals(1, $results->count());
  383. $tests = ArrayUtils::iteratorToArray(($results));
  384. $this->assertSame($test, array_pop($tests));
  385. $this->assertSame('group: foo', $test->getLabel());
  386. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Failure', $results[$test]);
  387. $this->assertSame($e, $results[$test]->getData());
  388. }
  389. public function testInvalidResult()
  390. {
  391. $someObj = new \stdClass;
  392. $test = new ReturnThisTest($someObj);
  393. $this->config['diagnostics']['group']['foo'] = $test;
  394. $dispatchResult = $this->controller->dispatch(new ConsoleRequest());
  395. $this->assertInstanceOf('Zend\View\Model\ViewModel', $dispatchResult);
  396. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $dispatchResult->getVariable('results'));
  397. $results = $dispatchResult->getVariable('results');
  398. $this->assertEquals(1, $results->count());
  399. $test = array_pop(ArrayUtils::iteratorToArray(($results)));
  400. $this->assertSame('group: foo', $test->getLabel());
  401. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Failure', $results[$test]);
  402. $this->assertSame($someObj, $results[$test]->getData());
  403. $someResource = fopen('php://memory','r');
  404. fclose($someResource);
  405. $test = new ReturnThisTest($someResource);
  406. $this->config['diagnostics']['group']['foo'] = $test;
  407. $dispatchResult = $this->controller->dispatch(new ConsoleRequest());
  408. $this->assertInstanceOf('Zend\View\Model\ViewModel', $dispatchResult);
  409. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $dispatchResult->getVariable('results'));
  410. $results = $dispatchResult->getVariable('results');
  411. $test = array_pop(ArrayUtils::iteratorToArray(($results)));
  412. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Failure', $results[$test]);
  413. $this->assertSame($someResource, $results[$test]->getData());
  414. $test = new ReturnThisTest(123);
  415. $this->config['diagnostics']['group']['foo'] = $test;
  416. $dispatchResult = $this->controller->dispatch(new ConsoleRequest());
  417. $this->assertInstanceOf('Zend\View\Model\ViewModel', $dispatchResult);
  418. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $dispatchResult->getVariable('results'));
  419. $results = $dispatchResult->getVariable('results');
  420. $test = array_pop(ArrayUtils::iteratorToArray(($results)));
  421. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Warning', $results[$test]);
  422. $this->assertEquals(123, $results[$test]->getData());
  423. }
  424. /**
  425. * 'diagnostics' => array(
  426. * 'group' => array(
  427. * 'Some\Test',
  428. * 'Some\Other\Test',
  429. * 'test3' => 'Another\One'
  430. * )
  431. * ),
  432. */
  433. public function testIgnoreNumericLabel()
  434. {
  435. $this->config['diagnostics']['group'][] = array('ClassExists',__CLASS__);
  436. $this->config['diagnostics']['group'][] = array('ClassExists',__CLASS__);
  437. $this->config['diagnostics']['group']['test3'] = array('ClassExists',__CLASS__);
  438. $result = $this->controller->dispatch(new ConsoleRequest());
  439. $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
  440. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  441. $results = $result->getVariable('results');
  442. $this->assertEquals(3, $results->count());
  443. $tests = ArrayUtils::iteratorToArray(($results));
  444. $test = array_shift($tests);
  445. $this->assertInstanceOf('ZFTool\Diagnostics\Test\ClassExists', $test);
  446. $this->assertNull($test->getLabel());
  447. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Success', $results[$test]);
  448. $test = array_shift($tests);
  449. $this->assertInstanceOf('ZFTool\Diagnostics\Test\ClassExists', $test);
  450. $this->assertNull($test->getLabel());
  451. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Success', $results[$test]);
  452. $test = array_shift($tests);
  453. $this->assertInstanceOf('ZFTool\Diagnostics\Test\ClassExists', $test);
  454. $this->assertSame('group: test3', $test->getLabel());
  455. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Success', $results[$test]);
  456. }
  457. /**
  458. * @dataProvider invalidDefinitionsProvider
  459. */
  460. public function testInvalidDefinitions($definition, $exceptionMessage)
  461. {
  462. $this->config['diagnostics']['group']['foo'] = $definition;
  463. try{
  464. $res = $this->controller->dispatch(new ConsoleRequest());
  465. }catch(RuntimeException $e){
  466. $this->assertStringMatchesFormat($exceptionMessage, $e->getMessage());
  467. return;
  468. }
  469. $this->fail('Definition is invalid!');
  470. }
  471. public function testFiltering()
  472. {
  473. $this->config['diagnostics']['group1']['test11'] = $test11 = new AlwaysSuccessTest();
  474. $this->config['diagnostics']['group2']['test21'] = $test21 = new AlwaysSuccessTest();
  475. $this->config['diagnostics']['group2']['test22'] = $test22 = new AlwaysSuccessTest();
  476. $this->routeMatch->setParam('testGroupName', 'group2');
  477. $result = $this->controller->dispatch(new ConsoleRequest());
  478. $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
  479. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  480. $results = $result->getVariable('results');
  481. $this->assertEquals(2, $results->count());
  482. $tests = ArrayUtils::iteratorToArray(($results));
  483. $this->assertSame($test21, $test = array_shift($tests));
  484. $this->assertEquals('group2: test21', $test->getLabel());
  485. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Success', $results[$test]);
  486. $this->assertSame($test22, $test = array_shift($tests));
  487. $this->assertEquals('group2: test22', $test->getLabel());
  488. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Success', $results[$test]);
  489. }
  490. /**
  491. * @depends testModuleProvidedDefinitions
  492. */
  493. public function testFilteringByModuleName()
  494. {
  495. $this->mm->injectModule('foomodule1', new DummyModule($this->sm));
  496. $this->mm->injectModule('foomodule2', new DummyModule($this->sm));
  497. $this->mm->injectModule('foomodule3', new DummyModule($this->sm));
  498. $this->routeMatch->setParam('testGroupName', 'foomodule2');
  499. $result = $this->controller->dispatch(new ConsoleRequest());
  500. $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
  501. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  502. $results = $result->getVariable('results');
  503. $this->assertEquals(5, $results->count());
  504. $tests = ArrayUtils::iteratorToArray(($results));
  505. $this->assertInstanceOf('ZFTool\Diagnostics\Test\TestInterface', $test = array_shift($tests));
  506. $this->assertEquals('foomodule2: test1', $test->getLabel());
  507. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Success', $results[$test]);
  508. }
  509. public function testBreakOnFailure()
  510. {
  511. $this->config['diagnostics']['group']['test1'] = $test1 = new AlwaysSuccessTest();
  512. $this->config['diagnostics']['group']['test2'] = $test2 = new ReturnThisTest(new Failure());
  513. $this->config['diagnostics']['group']['test3'] = $test3 = new AlwaysSuccessTest();
  514. $this->routeMatch->setParam('break', true);
  515. $result = $this->controller->dispatch(new ConsoleRequest());
  516. $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
  517. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  518. $results = $result->getVariable('results');
  519. $this->assertEquals(2, $results->count());
  520. $tests = ArrayUtils::iteratorToArray(($results));
  521. $this->assertSame($test1, $test = array_shift($tests));
  522. $this->assertEquals('group: test1', $test->getLabel());
  523. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Success', $results[$test]);
  524. $this->assertSame($test2, $test = array_shift($tests));
  525. $this->assertEquals('group: test2', $test->getLabel());
  526. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Failure', $results[$test]);
  527. $this->assertNull(array_shift($tests));
  528. }
  529. public function testBasicOutput()
  530. {
  531. $this->config['diagnostics']['group']['test1'] = $test1 = new AlwaysSuccessTest();
  532. ob_start();
  533. $result = $this->controller->dispatch(new ConsoleRequest());
  534. $this->assertStringMatchesFormat('Starting%a.%aOK%a', ob_get_clean());
  535. $this->assertInstanceOf('Zend\View\Model\ConsoleModel', $result);
  536. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  537. }
  538. public function testVerboseOutput()
  539. {
  540. $this->config['diagnostics']['group']['test1'] = $test1 = new AlwaysSuccessTest();
  541. $this->routeMatch->setParam('verbose', true);
  542. ob_start();
  543. $result = $this->controller->dispatch(new ConsoleRequest());
  544. $this->assertStringMatchesFormat('Starting%aOK%agroup: test1%aOK (1 diagnostic test%a', ob_get_clean());
  545. $this->assertInstanceOf('Zend\View\Model\ConsoleModel', $result);
  546. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  547. }
  548. public function testDebugOutput()
  549. {
  550. $this->config['diagnostics']['group']['test1'] = $test1 = new ReturnThisTest(
  551. new Success('foo', 'bar')
  552. );
  553. $this->routeMatch->setParam('debug', true);
  554. ob_start();
  555. $result = $this->controller->dispatch(new ConsoleRequest());
  556. $this->assertStringMatchesFormat('Starting%aOK%agroup: test1%afoo%abar%aOK (1 diagnostic test%a', ob_get_clean());
  557. $this->assertInstanceOf('Zend\View\Model\ConsoleModel', $result);
  558. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  559. }
  560. public function testQuietMode()
  561. {
  562. $this->config['diagnostics']['group']['test1'] = $test1 = new AlwaysSuccessTest();
  563. $this->routeMatch->setParam('quiet', true);
  564. ob_start();
  565. $result = $this->controller->dispatch(new ConsoleRequest());
  566. $this->assertEquals('', ob_get_clean());
  567. $this->assertInstanceOf('Zend\View\Model\ConsoleModel', $result);
  568. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  569. }
  570. public function testHttpMode()
  571. {
  572. $this->config['diagnostics']['group']['test1'] = $test1 = new AlwaysSuccessTest();
  573. ob_start();
  574. $result = $this->controller->dispatch(new \Zend\Http\Request());
  575. $this->assertEquals('', ob_get_clean());
  576. $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
  577. $this->assertInstanceOf('ZFTool\Diagnostics\Result\Collection', $result->getVariable('results'));
  578. }
  579. public function testErrorCodes()
  580. {
  581. $this->routeMatch->setParam('quiet', true);
  582. $this->config['diagnostics']['group']['test1'] = $test1 = new AlwaysSuccessTest();
  583. $result = $this->controller->dispatch(new ConsoleRequest());
  584. $this->assertInstanceOf('Zend\View\Model\ConsoleModel', $result);
  585. $this->assertEquals(0, $result->getErrorLevel());
  586. $this->config['diagnostics']['group']['test1'] = $test1 = new ReturnThisTest(new Failure());
  587. $result = $this->controller->dispatch(new ConsoleRequest());
  588. $this->assertInstanceOf('Zend\View\Model\ConsoleModel', $result);
  589. $this->assertEquals(1, $result->getErrorLevel());
  590. $this->config['diagnostics']['group']['test1'] = $test1 = new ReturnThisTest(new Warning());
  591. $result = $this->controller->dispatch(new ConsoleRequest());
  592. $this->assertInstanceOf('Zend\View\Model\ConsoleModel', $result);
  593. $this->assertEquals(0, $result->getErrorLevel());
  594. }
  595. public static function staticTestMethod($message = 'bar', $data = null)
  596. {
  597. static::$staticTestMethodCalled = true;
  598. return new Success($message, $data);
  599. }
  600. }
  601. function testOutlineFunction($message = 'bar', $data = null)
  602. {
  603. return new Success($message, $data);
  604. }