WritersTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. use \Bramus\Ansi\Writers\StreamWriter;
  3. use \Bramus\Ansi\Writers\BufferWriter;
  4. use \Bramus\Ansi\Writers\ProxyWriter;
  5. class WritersTest extends PHPUnit_Framework_TestCase
  6. {
  7. public function testStreamWriter()
  8. {
  9. // Start object buffering to catch any output
  10. ob_start();
  11. // Create a StreamWriter
  12. // @note: Using php://output instead of php://stdout — https://bugs.php.net/bug.php?id=49688
  13. $w = new StreamWriter('php://output');
  14. // Write something to the writer
  15. $w->write('test');
  16. // The written data should be echo'd (StreamWriter)
  17. $this->assertEquals('test', ob_get_contents());
  18. // Cleanup
  19. ob_end_clean();
  20. }
  21. public function testBufferWriter()
  22. {
  23. // Create a BufferWriter
  24. $w = new BufferWriter();
  25. // Write something to the Proxy
  26. $w->write('test');
  27. // Flush its contents
  28. $res = $w->flush();
  29. // The written data should be returned
  30. $this->assertEquals('test', $res);
  31. }
  32. public function testProxyWriter()
  33. {
  34. // Start object buffering to catch any output
  35. ob_start();
  36. // Create a ProxyWriter which proxies for a StreamWriter
  37. $w = new ProxyWriter(new StreamWriter('php://output'));
  38. // Write something to the Proxy
  39. $w->write('test');
  40. // Flush its contents
  41. $res = $w->flush();
  42. // The written data should be echo'd (StreamWriter)
  43. $this->assertEquals('test', ob_get_contents());
  44. // The written data should be returned too (ProxyWriter)
  45. $this->assertEquals('test', $res);
  46. // Cleanup
  47. ob_end_clean();
  48. }
  49. }