GNUReadlineTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2017 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Psy\Test\Readline;
  11. use Psy\Readline\GNUReadline;
  12. class GNUReadlineTest extends \PHPUnit_Framework_TestCase
  13. {
  14. private $historyFile;
  15. public function setUp()
  16. {
  17. if (!GNUReadline::isSupported()) {
  18. $this->markTestSkipped('GNUReadline not enabled');
  19. }
  20. $this->historyFile = tempnam(sys_get_temp_dir(), 'psysh_test_history');
  21. file_put_contents($this->historyFile, "_HiStOrY_V2_\n");
  22. }
  23. public function testHistory()
  24. {
  25. $readline = new GNUReadline($this->historyFile);
  26. $this->assertEmpty($readline->listHistory());
  27. $readline->addHistory('foo');
  28. $this->assertEquals(array('foo'), $readline->listHistory());
  29. $readline->addHistory('bar');
  30. $this->assertEquals(array('foo', 'bar'), $readline->listHistory());
  31. $readline->addHistory('baz');
  32. $this->assertEquals(array('foo', 'bar', 'baz'), $readline->listHistory());
  33. $readline->clearHistory();
  34. $this->assertEmpty($readline->listHistory());
  35. }
  36. /**
  37. * @depends testHistory
  38. */
  39. public function testHistorySize()
  40. {
  41. $readline = new GNUReadline($this->historyFile, 2);
  42. $this->assertEmpty($readline->listHistory());
  43. $readline->addHistory('foo');
  44. $readline->addHistory('bar');
  45. $this->assertEquals(array('foo', 'bar'), $readline->listHistory());
  46. $readline->addHistory('baz');
  47. $this->assertEquals(array('bar', 'baz'), $readline->listHistory());
  48. $readline->addHistory('w00t');
  49. $this->assertEquals(array('baz', 'w00t'), $readline->listHistory());
  50. $readline->clearHistory();
  51. $this->assertEmpty($readline->listHistory());
  52. }
  53. /**
  54. * @depends testHistory
  55. */
  56. public function testHistoryEraseDups()
  57. {
  58. $readline = new GNUReadline($this->historyFile, 0, true);
  59. $this->assertEmpty($readline->listHistory());
  60. $readline->addHistory('foo');
  61. $readline->addHistory('bar');
  62. $readline->addHistory('foo');
  63. $this->assertEquals(array('bar', 'foo'), $readline->listHistory());
  64. $readline->addHistory('baz');
  65. $readline->addHistory('w00t');
  66. $readline->addHistory('baz');
  67. $this->assertEquals(array('bar', 'foo', 'w00t', 'baz'), $readline->listHistory());
  68. $readline->clearHistory();
  69. $this->assertEmpty($readline->listHistory());
  70. }
  71. }