HistoryTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Component\BrowserKit\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\BrowserKit\History;
  13. use Symfony\Component\BrowserKit\Request;
  14. class HistoryTest extends TestCase
  15. {
  16. public function testAdd()
  17. {
  18. $history = new History();
  19. $history->add(new Request('http://www.example1.com/', 'get'));
  20. $this->assertSame('http://www.example1.com/', $history->current()->getUri(), '->add() adds a request to the history');
  21. $history->add(new Request('http://www.example2.com/', 'get'));
  22. $this->assertSame('http://www.example2.com/', $history->current()->getUri(), '->add() adds a request to the history');
  23. $history->add(new Request('http://www.example3.com/', 'get'));
  24. $history->back();
  25. $history->add(new Request('http://www.example4.com/', 'get'));
  26. $this->assertSame('http://www.example4.com/', $history->current()->getUri(), '->add() adds a request to the history');
  27. $history->back();
  28. $this->assertSame('http://www.example2.com/', $history->current()->getUri(), '->add() adds a request to the history');
  29. }
  30. public function testClearIsEmpty()
  31. {
  32. $history = new History();
  33. $history->add(new Request('http://www.example.com/', 'get'));
  34. $this->assertFalse($history->isEmpty(), '->isEmpty() returns false if the history is not empty');
  35. $history->clear();
  36. $this->assertTrue($history->isEmpty(), '->isEmpty() true if the history is empty');
  37. }
  38. public function testCurrent()
  39. {
  40. $history = new History();
  41. try {
  42. $history->current();
  43. $this->fail('->current() throws a \LogicException if the history is empty');
  44. } catch (\Exception $e) {
  45. $this->assertInstanceOf('LogicException', $e, '->current() throws a \LogicException if the history is empty');
  46. }
  47. $history->add(new Request('http://www.example.com/', 'get'));
  48. $this->assertSame('http://www.example.com/', $history->current()->getUri(), '->current() returns the current request in the history');
  49. }
  50. public function testBack()
  51. {
  52. $history = new History();
  53. $history->add(new Request('http://www.example.com/', 'get'));
  54. try {
  55. $history->back();
  56. $this->fail('->back() throws a \LogicException if the history is already on the first page');
  57. } catch (\Exception $e) {
  58. $this->assertInstanceOf('LogicException', $e, '->current() throws a \LogicException if the history is already on the first page');
  59. }
  60. $history->add(new Request('http://www.example1.com/', 'get'));
  61. $history->back();
  62. $this->assertSame('http://www.example.com/', $history->current()->getUri(), '->back() returns the previous request in the history');
  63. }
  64. public function testForward()
  65. {
  66. $history = new History();
  67. $history->add(new Request('http://www.example.com/', 'get'));
  68. $history->add(new Request('http://www.example1.com/', 'get'));
  69. try {
  70. $history->forward();
  71. $this->fail('->forward() throws a \LogicException if the history is already on the last page');
  72. } catch (\Exception $e) {
  73. $this->assertInstanceOf('LogicException', $e, '->forward() throws a \LogicException if the history is already on the last page');
  74. }
  75. $history->back();
  76. $history->forward();
  77. $this->assertSame('http://www.example1.com/', $history->current()->getUri(), '->forward() returns the next request in the history');
  78. }
  79. }