TestTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\create;
  9. use lithium\console\command\create\Test;
  10. use lithium\console\Request;
  11. use lithium\core\Libraries;
  12. class TestTest extends \lithium\test\Unit {
  13. public $request;
  14. protected $_backup = array();
  15. protected $_testPath = null;
  16. public function skip() {
  17. $this->_testPath = Libraries::get(true, 'resources') . '/tmp/tests';
  18. $this->skipIf(!is_writable($this->_testPath), "Path `{$this->_testPath}` is not writable.");
  19. }
  20. public function setUp() {
  21. Libraries::cache(false);
  22. $this->classes = array('response' => 'lithium\tests\mocks\console\MockResponse');
  23. $this->_backup['cwd'] = getcwd();
  24. $this->_backup['_SERVER'] = $_SERVER;
  25. $_SERVER['argv'] = array();
  26. Libraries::add('create_test', array('path' => $this->_testPath . '/create_test'));
  27. $this->request = new Request(array('input' => fopen('php://temp', 'w+')));
  28. $this->request->params = array('library' => 'create_test');
  29. }
  30. public function tearDown() {
  31. $_SERVER = $this->_backup['_SERVER'];
  32. chdir($this->_backup['cwd']);
  33. $this->_cleanUp();
  34. }
  35. public function testTestModel() {
  36. $this->request->params += array(
  37. 'command' => 'create', 'action' => 'test',
  38. 'args' => array('model', 'Posts')
  39. );
  40. $test = new Test(array(
  41. 'request' => $this->request, 'classes' => $this->classes
  42. ));
  43. $test->path = $this->_testPath;
  44. $test->run('test');
  45. $expected = "PostsTest created in tests/cases/models/PostsTest.php.\n";
  46. $result = $test->response->output;
  47. $this->assertEqual($expected, $result);
  48. $expected = <<<'test'
  49. namespace create_test\tests\cases\models;
  50. use create_test\models\Posts;
  51. class PostsTest extends \lithium\test\Unit {
  52. public function setUp() {}
  53. public function tearDown() {}
  54. }
  55. test;
  56. $replace = array("<?php", "?>");
  57. $result = str_replace($replace, '',
  58. file_get_contents($this->_testPath . '/create_test/tests/cases/models/PostsTest.php')
  59. );
  60. $this->assertEqual($expected, $result);
  61. }
  62. public function testTestModelWithMethods() {
  63. $this->_cleanUp();
  64. mkdir($this->_testPath . '/create_test/models/', 0755, true);
  65. $id = rand();
  66. $path = "create_test/models/Post{$id}s.php";
  67. file_put_contents("{$this->_testPath}/{$path}",
  68. "<?php
  69. namespace create_test\models;
  70. class Post{$id}s {
  71. public function someMethod() {}
  72. }"
  73. );
  74. $this->request->params += array('command' => 'create', 'action' => 'test', 'args' => array(
  75. 'model', "Post{$id}s"
  76. ));
  77. $test = new Test(array('request' => $this->request, 'classes' => $this->classes));
  78. $test->path = $this->_testPath;
  79. $test->run('test');
  80. $expected = "Post{$id}sTest created in tests/cases/models/Post{$id}sTest.php.\n";
  81. $result = $test->response->output;
  82. $this->assertEqual($expected, $result);
  83. $expected = <<<test
  84. namespace create_test\\tests\\cases\\models;
  85. use create_test\\models\\Post{$id}s;
  86. class Post{$id}sTest extends \\lithium\\test\\Unit {
  87. public function setUp() {}
  88. public function tearDown() {}
  89. public function testSomeMethod() {}
  90. }
  91. test;
  92. $replace = array("<?php", "?>");
  93. $path = "create_test/tests/cases/models/Post{$id}sTest.php";
  94. $result = str_replace($replace, '', file_get_contents("{$this->_testPath}/{$path}"));
  95. $this->assertEqual($expected, $result);
  96. }
  97. }
  98. ?>