LibeditTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\Libedit;
  12. class LibeditTest extends \PHPUnit_Framework_TestCase
  13. {
  14. private $historyFile;
  15. public function setUp()
  16. {
  17. if (!Libedit::isSupported()) {
  18. $this->markTestSkipped('Libedit not enabled');
  19. }
  20. $this->historyFile = tempnam(sys_get_temp_dir(), 'psysh_test_history');
  21. if (false === file_put_contents($this->historyFile, "_HiStOrY_V2_\n")) {
  22. $this->fail('Unable to write history file: ' . $this->historyFile);
  23. }
  24. // Calling readline_read_history before readline_clear_history
  25. // avoids segfault with PHP 5.5.7 & libedit v3.1
  26. readline_read_history($this->historyFile);
  27. readline_clear_history();
  28. }
  29. public function tearDown()
  30. {
  31. if (is_file($this->historyFile)) {
  32. unlink($this->historyFile);
  33. }
  34. }
  35. public function testHistory()
  36. {
  37. $readline = new Libedit($this->historyFile);
  38. $this->assertEmpty($readline->listHistory());
  39. $readline->addHistory('foo');
  40. $this->assertEquals(array('foo'), $readline->listHistory());
  41. $readline->addHistory('bar');
  42. $this->assertEquals(array('foo', 'bar'), $readline->listHistory());
  43. $readline->addHistory('baz');
  44. $this->assertEquals(array('foo', 'bar', 'baz'), $readline->listHistory());
  45. $readline->clearHistory();
  46. $this->assertEmpty($readline->listHistory());
  47. }
  48. /**
  49. * @depends testHistory
  50. */
  51. public function testHistorySize()
  52. {
  53. $readline = new Libedit($this->historyFile, 2);
  54. $this->assertEmpty($readline->listHistory());
  55. $readline->addHistory('foo');
  56. $readline->addHistory('bar');
  57. $this->assertEquals(array('foo', 'bar'), $readline->listHistory());
  58. $readline->addHistory('baz');
  59. $this->assertEquals(array('bar', 'baz'), $readline->listHistory());
  60. $readline->addHistory('w00t');
  61. $this->assertEquals(array('baz', 'w00t'), $readline->listHistory());
  62. $readline->clearHistory();
  63. $this->assertEmpty($readline->listHistory());
  64. }
  65. /**
  66. * @depends testHistory
  67. */
  68. public function testHistoryEraseDups()
  69. {
  70. $readline = new Libedit($this->historyFile, 0, true);
  71. $this->assertEmpty($readline->listHistory());
  72. $readline->addHistory('foo');
  73. $readline->addHistory('bar');
  74. $readline->addHistory('foo');
  75. $this->assertEquals(array('bar', 'foo'), $readline->listHistory());
  76. $readline->addHistory('baz');
  77. $readline->addHistory('w00t');
  78. $readline->addHistory('baz');
  79. $this->assertEquals(array('bar', 'foo', 'w00t', 'baz'), $readline->listHistory());
  80. $readline->clearHistory();
  81. $this->assertEmpty($readline->listHistory());
  82. }
  83. public function testListHistory()
  84. {
  85. $readline = new Libedit($this->historyFile);
  86. file_put_contents(
  87. $this->historyFile,
  88. "This is an entry\n\0This is a comment\nThis is an entry\0With a comment\n",
  89. FILE_APPEND
  90. );
  91. $this->assertEquals(array(
  92. 'This is an entry',
  93. 'This is an entry',
  94. ), $readline->listHistory());
  95. $readline->clearHistory();
  96. }
  97. /**
  98. * Libedit being a BSD library,
  99. * it doesn't support non-unix line separators.
  100. */
  101. public function testLinebreaksSupport()
  102. {
  103. $readline = new Libedit($this->historyFile);
  104. file_put_contents(
  105. $this->historyFile,
  106. "foo\rbar\nbaz\r\nw00t",
  107. FILE_APPEND
  108. );
  109. $this->assertEquals(array(
  110. "foo\rbar",
  111. "baz\r",
  112. 'w00t',
  113. ), $readline->listHistory());
  114. $readline->clearHistory();
  115. }
  116. }