TestTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\console\command;
  9. use lithium\console\command\Test;
  10. use lithium\console\Request;
  11. use lithium\core\Libraries;
  12. class TestTest extends \lithium\test\Unit {
  13. public $request;
  14. public $classes = array();
  15. protected $_backup = array();
  16. public function setUp() {
  17. Libraries::cache(false);
  18. $this->classes = array(
  19. 'response' => 'lithium\tests\mocks\console\MockResponse'
  20. );
  21. $this->_backup['cwd'] = getcwd();
  22. $this->_backup['_SERVER'] = $_SERVER;
  23. $_SERVER['argv'] = array();
  24. chdir(LITHIUM_LIBRARY_PATH . '/lithium');
  25. $this->request = new Request(array('input' => fopen('php://temp', 'w+')));
  26. $this->request->params = array('library' => 'build_test');
  27. }
  28. public function tearDown() {
  29. $_SERVER = $this->_backup['_SERVER'];
  30. chdir($this->_backup['cwd']);
  31. }
  32. public function skip() {
  33. $isWin = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
  34. $this->skipIf($isWin, 'The test command needs to be refactored to work on windows.');
  35. }
  36. public function testRunWithoutPath() {
  37. $command = new Test(array(
  38. 'request' => $this->request, 'classes' => $this->classes
  39. ));
  40. $result = $command->run();
  41. $this->assertFalse($result);
  42. }
  43. public function testRunWithInvalidPath() {
  44. $command = new Test(array(
  45. 'request' => $this->request, 'classes' => $this->classes
  46. ));
  47. $path = 'Foobar/lithium/tests/mocks/test/cases/MockTest.php';
  48. $command->run($path);
  49. $expected = "Path `.*` not found.\n";
  50. $result = $command->response->error;
  51. $this->assertPattern("/{$expected}/", $result);
  52. }
  53. public function testRunWithInvalidLibrary() {
  54. $command = new Test(array(
  55. 'request' => $this->request,
  56. 'classes' => $this->classes
  57. ));
  58. $command->format = 'foobar';
  59. $path = LITHIUM_LIBRARY_PATH . '/bob/tests/mocks/test/cases/MockTest.php';
  60. $command->run($path);
  61. $expected = "No library found in path `";
  62. $expected .= LITHIUM_LIBRARY_PATH . "/bob/tests/mocks/test/cases/MockTest.php`.\n";
  63. $result = $command->response->error;
  64. $this->assertEqual($expected, $result);
  65. }
  66. public function testRunWithInvalidHandler() {
  67. $command = new Test(array(
  68. 'request' => $this->request,
  69. 'classes' => $this->classes
  70. ));
  71. $command->format = 'foobar';
  72. $path = LITHIUM_LIBRARY_PATH . '/lithium/tests/mocks/test/cases/MockTest.php';
  73. $command->run($path);
  74. $expected = "No handler for format `foobar`... \n";
  75. $result = $command->response->error;
  76. $this->assertEqual($expected, $result);
  77. }
  78. public function testRunSingleTestWithAbsolutePath() {
  79. $command = new Test(array(
  80. 'request' => $this->request, 'classes' => $this->classes
  81. ));
  82. $path = LITHIUM_LIBRARY_PATH . '/lithium/tests/mocks/test/cases/MockTest.php';
  83. $command->run($path);
  84. $expected = "1 pass\n0 fails and 0 exceptions\n";
  85. $expected = preg_quote($expected);
  86. $result = $command->response->output;
  87. $this->assertPattern("/{$expected}/", $result);
  88. }
  89. public function testRunSingleTestWithRelativePath() {
  90. $command = new Test(array(
  91. 'request' => $this->request, 'classes' => $this->classes
  92. ));
  93. $path = 'tests/mocks/test/cases/MockTest.php';
  94. $command->run($path);
  95. $expected = "1 pass\n0 fails and 0 exceptions\n";
  96. $expected = preg_quote($expected);
  97. $result = $command->response->output;
  98. $this->assertPattern("/{$expected}/", $result);
  99. $command = new Test(array(
  100. 'request' => $this->request, 'classes' => $this->classes
  101. ));
  102. $current = basename(getcwd());
  103. $path = "../{$current}/tests/mocks/test/cases/MockTest.php";
  104. $command->run($path);
  105. $expected = "1 pass\n0 fails and 0 exceptions\n";
  106. $expected = preg_quote($expected);
  107. $result = $command->response->output;
  108. $this->assertPattern("/{$expected}/", $result);
  109. $current = basename(getcwd());
  110. $path = "{$current}/tests/mocks/test/cases/MockTest.php";
  111. $command->run($path);
  112. $expected = "1 pass\n0 fails and 0 exceptions\n";
  113. $expected = preg_quote($expected);
  114. $result = $command->response->output;
  115. $this->assertPattern("/{$expected}/", $result);
  116. }
  117. public function testRunMultipleTestsWithAbsolutePath() {
  118. $command = new Test(array(
  119. 'request' => $this->request, 'classes' => $this->classes
  120. ));
  121. $path = LITHIUM_LIBRARY_PATH . '/lithium/tests/mocks/test/cases';
  122. $command->run($path);
  123. $expected = "4 exceptions";
  124. $expected = preg_quote($expected, '/');
  125. $result = $command->response->output;
  126. $this->assertPattern("/{$expected}/", $result);
  127. }
  128. public function testReturnRunTestPasses() {
  129. $command = new Test(array(
  130. 'request' => $this->request, 'classes' => $this->classes
  131. ));
  132. $path = LITHIUM_LIBRARY_PATH . '/lithium/tests/mocks/test/cases/MockTest.php';
  133. $result = $command->run($path);
  134. $this->assertTrue($result);
  135. }
  136. public function testReturnRunTestFails() {
  137. $command = new Test(array(
  138. 'request' => $this->request, 'classes' => $this->classes
  139. ));
  140. $path = LITHIUM_LIBRARY_PATH . '/lithium/tests/mocks/test/cases/MockTestErrorHandling.php';
  141. $result = $command->run($path);
  142. $this->assertFalse($result);
  143. }
  144. public function testJsonFormat() {
  145. $command = new Test(array(
  146. 'request' => $this->request, 'classes' => $this->classes
  147. ));
  148. $path = LITHIUM_LIBRARY_PATH . '/lithium/tests/mocks/test/cases/MockTest.php';
  149. $command->format = 'json';
  150. $command->run($path);
  151. $result = $command->response->output;
  152. $result = json_decode($result, true);
  153. $this->assertTrue(isset($result['count']));
  154. $this->assertTrue(isset($result['stats']));
  155. }
  156. }
  157. ?>