RequestTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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;
  9. use lithium\core\Libraries;
  10. use lithium\console\Request;
  11. class RequestTest extends \lithium\test\Unit {
  12. public $streams;
  13. protected $_backup = array();
  14. public function setUp() {
  15. $this->streams = array(
  16. 'input' => Libraries::get(true, 'resources') . '/tmp/tests/input.txt'
  17. );
  18. $this->_backup['cwd'] = getcwd();
  19. $this->_backup['_SERVER'] = $_SERVER;
  20. $_SERVER['argv'] = array();
  21. }
  22. public function tearDown() {
  23. foreach ($this->streams as $path) {
  24. if (file_exists($path)) {
  25. unlink($path);
  26. }
  27. }
  28. $_SERVER = $this->_backup['_SERVER'];
  29. chdir($this->_backup['cwd']);
  30. }
  31. public function testConstructWithoutConfig() {
  32. $request = new Request();
  33. $expected = array();
  34. $result = $request->args;
  35. $this->assertEqual($expected, $result);
  36. $result = $request->env();
  37. $this->assertTrue(!empty($result));
  38. $expected = getcwd();
  39. $result = $result['working'];
  40. $this->assertEqual($expected, $result);
  41. }
  42. public function testEnvWorking() {
  43. $base = Libraries::get(true, 'resources') . '/tmp/tests';
  44. $this->skipIf(!is_readable($base), "Path `{$base}` is not readable.");
  45. chdir(Libraries::get(true, 'resources') . '/tmp/tests');
  46. $request = new Request();
  47. $expected = realpath(Libraries::get(true, 'resources') . '/tmp/tests');
  48. $result = $request->env('working');
  49. $this->assertEqual($expected, $result);
  50. }
  51. public function testConstructWithServer() {
  52. $_SERVER['argv'] = array('/path/to/lithium.php', 'one', 'two');
  53. $request = new Request();
  54. $expected = '/path/to/lithium.php';
  55. $result = $request->env('script');
  56. $this->assertEqual($expected, $result);
  57. $expected = array('one', 'two');
  58. $result = $request->argv;
  59. $this->assertEqual($expected, $result);
  60. }
  61. public function testConstructWithConfigArgv() {
  62. $request = new Request(array('args' => array('/path/to/lithium.php', 'wrong')));
  63. $expected = array('/path/to/lithium.php', 'wrong');
  64. $result = $request->argv;
  65. $this->assertEqual($expected, $result);
  66. $_SERVER['argv'] = array('/path/to/lithium.php');
  67. $request = new Request(array('args' => array('one', 'two')));
  68. $expected = '/path/to/lithium.php';
  69. $result = $request->env('script');
  70. $this->assertEqual($expected, $result);
  71. $expected = array('one', 'two');
  72. $result = $request->argv;
  73. $this->assertEqual($expected, $result);
  74. }
  75. public function testConstructWithConfigArgs() {
  76. $request = new Request(array(
  77. 'args' => array('ok')
  78. ));
  79. $expected = array('ok');
  80. $this->assertEqual($expected, $request->argv);
  81. $request = new Request(array(
  82. 'env' => array('script' => '/path/to/lithium.php'),
  83. 'args' => array('one', 'two', 'three', 'four')
  84. ));
  85. $expected = '/path/to/lithium.php';
  86. $result = $request->env('script');
  87. $this->assertEqual($expected, $result);
  88. $expected = array('one', 'two', 'three', 'four');
  89. $this->assertEqual($expected, $request->argv);
  90. }
  91. public function testConstructWithEnv() {
  92. $base = Libraries::get(true, 'resources') . '/tmp/tests';
  93. $this->skipIf(!is_readable($base), "Path `{$base}` is not readable.");
  94. chdir(Libraries::get(true, 'resources') . '/tmp');
  95. $request = new Request(array('env' => array('working' => '/some/other/path')));
  96. $expected = '/some/other/path';
  97. $result = $request->env('working');
  98. $this->assertEqual($expected, $result);
  99. }
  100. public function testInput() {
  101. $base = Libraries::get(true, 'resources') . '/tmp/tests';
  102. $this->skipIf(!is_writable($base), "{$base} is not writable.");
  103. $stream = fopen($this->streams['input'], 'w+');
  104. $request = new Request(array('input' => $stream));
  105. $this->assertTrue(is_resource($request->input));
  106. $this->assertEqual($stream, $request->input);
  107. $this->assertEqual(2, fwrite($request->input, 'ok'));
  108. rewind($request->input);
  109. $this->assertEqual('ok', $request->input());
  110. }
  111. public function testArgs() {
  112. $request = new Request();
  113. $request->params = array(
  114. 'command' => 'one', 'action' => 'two', 'args' => array('three', 'four', 'five')
  115. );
  116. $this->assertEqual('five', $request->args(2));
  117. }
  118. public function testShiftDefaultOne() {
  119. $request = new Request();
  120. $request->params = array(
  121. 'command' => 'one', 'action' => 'two',
  122. 'args' => array('three', 'four', 'five')
  123. );
  124. $request->shift();
  125. $expected = array('command' => 'two', 'action' => 'three', 'args' => array('four', 'five'));
  126. $this->assertEqual($expected, $request->params);
  127. }
  128. public function testShiftTwo() {
  129. $request = new Request();
  130. $request->params = array(
  131. 'command' => 'one', 'action' => 'two',
  132. 'args' => array('three', 'four', 'five')
  133. );
  134. $request->shift(2);
  135. $expected = array('command' => 'three', 'action' => 'four', 'args' => array('five'));
  136. $result = $request->params;
  137. $this->assertEqual($expected, $result);
  138. }
  139. public function testTemporaryFileStructureExists() {
  140. $resources = Libraries::get(true, 'resources');
  141. $template = $resources . '/tmp/cache/templates/';
  142. $this->assert(is_dir($template));
  143. }
  144. }
  145. ?>