HistoryTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Tests\Components\BrowserKit;
  11. use Symfony\Components\BrowserKit\History;
  12. use Symfony\Components\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. {
  42. $history->current();
  43. $this->fail('->current() throws a \LogicException if the history is empty');
  44. }
  45. catch (\Exception $e)
  46. {
  47. $this->assertInstanceof('LogicException', $e, '->current() throws a \LogicException if the history is empty');
  48. }
  49. $history->add(new Request('http://www.example.com/', 'get'));
  50. $this->assertSame('http://www.example.com/', $history->current()->getUri(), '->current() returns the current request in the history');
  51. }
  52. public function testBack()
  53. {
  54. $history = new History();
  55. $history->add(new Request('http://www.example.com/', 'get'));
  56. try
  57. {
  58. $history->back();
  59. $this->fail('->back() throws a \LogicException if the history is already on the first page');
  60. }
  61. catch (\Exception $e)
  62. {
  63. $this->assertInstanceof('LogicException', $e, '->current() throws a \LogicException if the history is already on the first page');
  64. }
  65. $history->add(new Request('http://www.example1.com/', 'get'));
  66. $history->back();
  67. $this->assertSame('http://www.example.com/', $history->current()->getUri(), '->back() returns the previous request in the history');
  68. }
  69. public function testForward()
  70. {
  71. $history = new History();
  72. $history->add(new Request('http://www.example.com/', 'get'));
  73. $history->add(new Request('http://www.example1.com/', 'get'));
  74. try
  75. {
  76. $history->forward();
  77. $this->fail('->forward() throws a \LogicException if the history is already on the last page');
  78. }
  79. catch (\Exception $e)
  80. {
  81. $this->assertInstanceof('LogicException', $e, '->forward() throws a \LogicException if the history is already on the last page');
  82. }
  83. $history->back();
  84. $history->forward();
  85. $this->assertSame('http://www.example1.com/', $history->current()->getUri(), '->forward() returns the next request in the history');
  86. }
  87. }