StdinTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace League\CLImate\Tests;
  3. use League\CLImate\Util\Reader\Stdin;
  4. require_once 'StdinGlobalMock.php';
  5. class StdinTest extends TestBase
  6. {
  7. /** @test */
  8. public function it_can_read_a_line_from_stdin()
  9. {
  10. $stdin = new Stdin;
  11. self::$functions->shouldReceive('fopen')
  12. ->once()
  13. ->with('php://stdin', 'r')
  14. ->andReturn('resource');
  15. self::$functions->shouldReceive('fgets')
  16. ->once()
  17. ->with('resource', 1024)
  18. ->andReturn('I hear you loud and clear.');
  19. $response = $stdin->line();
  20. $this->assertSame('I hear you loud and clear.', $response);
  21. }
  22. /** @test */
  23. public function it_can_read_multiple_lines_from_stdin()
  24. {
  25. $stdin = new Stdin;
  26. self::$functions->shouldReceive('fopen')
  27. ->once()
  28. ->with('php://stdin', 'r')
  29. ->andReturn('resource');
  30. self::$functions->shouldReceive('stream_get_contents')
  31. ->once()
  32. ->with('resource')
  33. ->andReturn('I hear you loud and clear.');
  34. $response = $stdin->multiLine();
  35. $this->assertSame('I hear you loud and clear.', $response);
  36. }
  37. /** @test */
  38. public function it_can_several_characters_from_stdin()
  39. {
  40. $stdin = new Stdin;
  41. self::$functions->shouldReceive('fopen')
  42. ->once()
  43. ->with('php://stdin', 'r')
  44. ->andReturn('resource');
  45. self::$functions->shouldReceive('fread')
  46. ->once()
  47. ->with('resource', 6)
  48. ->andReturn('I hear');
  49. $response = $stdin->char(6);
  50. $this->assertSame('I hear', $response);
  51. }
  52. }