ControllerTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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\Controller;
  10. use lithium\console\Request;
  11. use lithium\core\Libraries;
  12. class ControllerTest 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. $this->classes = array('response' => 'lithium\tests\mocks\console\MockResponse');
  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');
  28. }
  29. public function tearDown() {
  30. $_SERVER = $this->_backup['_SERVER'];
  31. chdir($this->_backup['cwd']);
  32. $this->_cleanUp();
  33. }
  34. public function testClass() {
  35. $this->request->params += array(
  36. 'command' => 'controller', 'action' => 'Posts'
  37. );
  38. $model = new Controller(array(
  39. 'request' => $this->request, 'classes' => $this->classes
  40. ));
  41. $expected = 'PostsController';
  42. $result = $model->invokeMethod('_class', array($this->request));
  43. $this->assertEqual($expected, $result);
  44. }
  45. public function testUse() {
  46. $this->request->params += array(
  47. 'command' => 'controller', 'action' => 'Posts'
  48. );
  49. $model = new Controller(array(
  50. 'request' => $this->request, 'classes' => $this->classes
  51. ));
  52. $expected = 'create_test\\models\\Posts';
  53. $result = $model->invokeMethod('_use', array($this->request));
  54. $this->assertEqual($expected, $result);
  55. }
  56. public function testRun() {
  57. $this->request->params += array(
  58. 'command' => 'create', 'action' => 'controller',
  59. 'args' => array('Posts')
  60. );
  61. $controller = new Controller(array(
  62. 'request' => $this->request, 'classes' => $this->classes
  63. ));
  64. $controller->path = $this->_testPath;
  65. $controller->run('controller');
  66. $expected = "PostsController created in controllers/PostsController.php.\n";
  67. $result = $controller->response->output;
  68. $this->assertEqual($expected, $result);
  69. $expected = <<<'test'
  70. namespace create_test\controllers;
  71. use create_test\models\Posts;
  72. use lithium\action\DispatchException;
  73. class PostsController extends \lithium\action\Controller {
  74. public function index() {
  75. $posts = Posts::all();
  76. return compact('posts');
  77. }
  78. public function view() {
  79. $post = Posts::first($this->request->id);
  80. return compact('post');
  81. }
  82. public function add() {
  83. $post = Posts::create();
  84. if (($this->request->data) && $post->save($this->request->data)) {
  85. return $this->redirect(array('Posts::view', 'args' => array($post->id)));
  86. }
  87. return compact('post');
  88. }
  89. public function edit() {
  90. $post = Posts::find($this->request->id);
  91. if (!$post) {
  92. return $this->redirect('Posts::index');
  93. }
  94. if (($this->request->data) && $post->save($this->request->data)) {
  95. return $this->redirect(array('Posts::view', 'args' => array($post->id)));
  96. }
  97. return compact('post');
  98. }
  99. public function delete() {
  100. if (!$this->request->is('post') && !$this->request->is('delete')) {
  101. $msg = "Posts::delete can only be called with http:post or http:delete.";
  102. throw new DispatchException($msg);
  103. }
  104. Posts::find($this->request->id)->delete();
  105. return $this->redirect('Posts::index');
  106. }
  107. }
  108. test;
  109. $replace = array("<?php", "?>");
  110. $result = str_replace($replace, '',
  111. file_get_contents($this->_testPath . '/create_test/controllers/PostsController.php')
  112. );
  113. $this->assertEqual($expected, $result);
  114. }
  115. }
  116. ?>