ResponseTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\Response;
  11. class ResponseTest extends \lithium\test\Unit {
  12. public $streams;
  13. public function setUp() {
  14. $this->streams = array(
  15. 'output' => Libraries::get(true, 'resources') . '/tmp/tests/output.txt',
  16. 'error' => Libraries::get(true, 'resources') . '/tmp/tests/error.txt'
  17. );
  18. }
  19. public function tearDown() {
  20. foreach ($this->streams as $path) {
  21. if (file_exists($path)) {
  22. unlink($path);
  23. }
  24. }
  25. }
  26. public function testConstructWithoutConfig() {
  27. $response = new Response();
  28. $this->assertTrue(is_resource($response->output));
  29. $this->assertTrue(is_resource($response->error));
  30. }
  31. public function testConstructWithConfigOutput() {
  32. $base = Libraries::get(true, 'resources') . '/tmp/tests';
  33. $this->skipIf(!is_writable($base), "Path `{$base}` is not writable.");
  34. $stream = fopen($this->streams['output'], 'w');
  35. $response = new Response(array(
  36. 'output' => $stream
  37. ));
  38. $this->assertTrue(is_resource($response->output));
  39. $this->assertEqual($stream, $response->output);
  40. }
  41. public function testConstructWithConfigError() {
  42. $base = Libraries::get(true, 'resources') . '/tmp/tests';
  43. $this->skipIf(!is_writable($base), "Path `{$base}` is not writable.");
  44. $stream = fopen($this->streams['error'], 'w');
  45. $response = new Response(array('error' => $stream));
  46. $this->assertTrue(is_resource($response->error));
  47. $this->assertEqual($stream, $response->error);
  48. }
  49. public function testOutput() {
  50. $base = Libraries::get(true, 'resources') . '/tmp/tests';
  51. $this->skipIf(!is_writable($base), "Path `{$base}` is not writable.");
  52. $response = new Response(array('output' => fopen($this->streams['output'], 'w+')));
  53. $this->assertTrue(is_resource($response->output));
  54. $this->assertEqual(2, $response->output('ok'));
  55. $this->assertEqual('ok', file_get_contents($this->streams['output']));
  56. }
  57. public function testError() {
  58. $base = Libraries::get(true, 'resources') . '/tmp/tests';
  59. $this->skipIf(!is_writable($base), "Path `{$base}` is not writable.");
  60. $response = new Response(array('error' => fopen($this->streams['error'], 'w+')));
  61. $this->assertTrue(is_resource($response->error));
  62. $this->assertEqual(2, $response->error('ok'));
  63. $this->assertEqual('ok', file_get_contents($this->streams['error']));
  64. }
  65. }
  66. ?>