GNUReadlineTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2018 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->assertSame(['foo'], $readline->listHistory());
  29. $readline->addHistory('bar');
  30. $this->assertSame(['foo', 'bar'], $readline->listHistory());
  31. $readline->addHistory('baz');
  32. $this->assertSame(['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->assertSame(['foo', 'bar'], $readline->listHistory());
  46. $readline->addHistory('baz');
  47. $this->assertSame(['bar', 'baz'], $readline->listHistory());
  48. $readline->addHistory('w00t');
  49. $this->assertSame(['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->assertSame(['bar', 'foo'], $readline->listHistory());
  64. $readline->addHistory('baz');
  65. $readline->addHistory('w00t');
  66. $readline->addHistory('baz');
  67. $this->assertSame(['bar', 'foo', 'w00t', 'baz'], $readline->listHistory());
  68. $readline->clearHistory();
  69. $this->assertEmpty($readline->listHistory());
  70. }
  71. }