HistoryTest.php 3.7 KB

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