CreateTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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\tests\mocks\console\command\MockCreate;
  10. use lithium\console\Request;
  11. use lithium\core\Libraries;
  12. use lithium\data\Connections;
  13. class CreateTest extends \lithium\test\Unit {
  14. public $request;
  15. protected $_backup = array();
  16. protected $_testPath = null;
  17. public function skip() {
  18. $this->_testPath = Libraries::get(true, 'resources') . '/tmp/tests';
  19. $this->skipIf(!is_writable($this->_testPath), "Path `{$this->_testPath}` is not writable.");
  20. }
  21. public function setUp() {
  22. $this->_backup['cwd'] = getcwd();
  23. $this->_backup['_SERVER'] = $_SERVER;
  24. $_SERVER['argv'] = array();
  25. Libraries::add('create_test', array('path' => $this->_testPath . '/create_test'));
  26. $this->request = new Request(array('input' => fopen('php://temp', 'w+')));
  27. $this->request->params = array('library' => 'create_test', 'action' => null);
  28. Connections::add('default', array(
  29. 'type' => null,
  30. 'adapter' => 'lithium\tests\mocks\data\model\MockDatabase'
  31. ));
  32. }
  33. public function tearDown() {
  34. $_SERVER = $this->_backup['_SERVER'];
  35. chdir($this->_backup['cwd']);
  36. $this->_cleanUp();
  37. }
  38. public function testConstruct() {
  39. $create = new MockCreate(array('request' => $this->request));
  40. $expected = 'create_test';
  41. $result = $create->library;
  42. $this->assertEqual($expected, $result);
  43. }
  44. public function testNonExistentCommand() {
  45. $this->request->params['args'] = array('does_not_exist', 'anywhere');
  46. $create = new MockCreate(array('request' => $this->request));
  47. $result = $create->run('does_not_exist');
  48. $this->assertFalse($result);
  49. $expected = "does_not_exist could not be created.\n";
  50. $result = $create->response->error;
  51. $this->assertEqual($expected, $result);
  52. }
  53. public function testNamespace() {
  54. $create = new MockCreate(array('request' => $this->request));
  55. $this->request->params['command'] = 'one';
  56. $expected = 'create_test\\two';
  57. $result = $create->invokeMethod('_namespace', array(
  58. $this->request, array(
  59. 'spaces' => array('one' => 'two')
  60. )
  61. ));
  62. $this->assertEqual($expected, $result);
  63. }
  64. public function testSave() {
  65. chdir($this->_testPath);
  66. $this->request->params = array('library' => 'create_test', 'template' => 'test');
  67. $create = new MockCreate(array('request' => $this->request));
  68. $result = $create->save(array(
  69. 'namespace' => 'create_test\tests\cases\models',
  70. 'use' => 'create_test\models\Posts',
  71. 'class' => 'PostTest',
  72. 'methods' => "\tpublic function testCreate() {\n\n\t}\n"
  73. ));
  74. $this->assertTrue($result);
  75. $result = $this->_testPath . '/create_test/tests/cases/models/PostTest.php';
  76. $this->assertTrue(file_exists($result));
  77. $this->_cleanUp();
  78. }
  79. public function testRunWithoutCommand() {
  80. $create = new MockCreate(array('request' => $this->request));
  81. $result = $create->run();
  82. $this->assertFalse($result);
  83. $result = $create->response->output;
  84. $this->assertFalse($result);
  85. }
  86. public function testRunNotSaved() {
  87. $this->request->params = array(
  88. 'library' => 'not_here', 'command' => 'create', 'action' => 'model',
  89. 'args' => array('model', 'Posts')
  90. );
  91. $create = new MockCreate(array('request' => $this->request));
  92. $result = $create->run('model');
  93. $this->assertFalse($result);
  94. $expected = "model could not be created.\n";
  95. $result = $create->response->error;
  96. $this->assertEqual($expected, $result);
  97. }
  98. public function testRunWithModelCommand() {
  99. $this->request->params = array(
  100. 'library' => 'create_test', 'command' => 'create', 'action' => 'model',
  101. 'args' => array('Posts')
  102. );
  103. $create = new MockCreate(array('request' => $this->request));
  104. $create->run('model');
  105. $expected = 'model';
  106. $result = $create->request->command;
  107. $this->assertEqual($expected, $result);
  108. $result = $this->_testPath . '/create_test/models/Posts.php';
  109. $this->assertTrue(file_exists($result));
  110. }
  111. public function testRunWithTestModelCommand() {
  112. $this->request->params = array(
  113. 'library' => 'create_test', 'command' => 'create', 'action' => 'test',
  114. 'args' => array('model', 'Posts')
  115. );
  116. $create = new MockCreate(array('request' => $this->request));
  117. $create->run('test');
  118. $expected = 'model';
  119. $result = $create->request->command;
  120. $this->assertEqual($expected, $result);
  121. $result = $this->_testPath . '/create_test/tests/cases/models/PostsTest.php';
  122. $this->assertTrue(file_exists($result));
  123. }
  124. public function testRunWithTestControllerCommand() {
  125. $this->request->params = array(
  126. 'library' => 'create_test', 'command' => 'create', 'action' => 'test',
  127. 'args' => array('controller', 'Posts')
  128. );
  129. $create = new MockCreate(array('request' => $this->request));
  130. $create->run('test');
  131. $expected = 'controller';
  132. $result = $create->request->command;
  133. $this->assertEqual($expected, $result);
  134. $result = $this->_testPath . '/create_test/tests/cases/controllers/PostsControllerTest.php';
  135. $this->assertTrue(file_exists($result));
  136. }
  137. public function testRunWithTestOtherCommand() {
  138. $this->request->params = array(
  139. 'library' => 'create_test', 'command' => 'create', 'action' => 'test',
  140. 'args' => array('something', 'Posts')
  141. );
  142. $create = new MockCreate(array('request' => $this->request));
  143. $create->run('test');
  144. $expected = 'something';
  145. $result = $create->request->command;
  146. $this->assertEqual($expected, $result);
  147. $result = $this->_testPath . '/create_test/tests/cases/something/PostsTest.php';
  148. $this->assertTrue(file_exists($result));
  149. }
  150. public function testRunAll() {
  151. $this->request->params = array(
  152. 'library' => 'create_test', 'command' => 'create', 'action' => 'Posts',
  153. 'args' => array()
  154. );
  155. $create = new MockCreate(array('request' => $this->request));
  156. $create->run('Posts');
  157. $result = $this->_testPath . '/create_test/models/Posts.php';
  158. $this->assertTrue(file_exists($result));
  159. $result = $this->_testPath . '/create_test/controllers/PostsController.php';
  160. $this->assertTrue(file_exists($result));
  161. $result = $this->_testPath . '/create_test/tests/cases/models/PostsTest.php';
  162. $this->assertTrue(file_exists($result));
  163. $result = $this->_testPath . '/create_test/tests/cases/controllers/PostsControllerTest.php';
  164. $this->assertTrue(file_exists($result));
  165. }
  166. }
  167. ?>