SessionTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\Bundle\FrameworkBundle\Tests\Functional;
  11. class SessionTest extends WebTestCase
  12. {
  13. /**
  14. * Tests session attributes persist.
  15. *
  16. * @dataProvider getConfigs
  17. */
  18. public function testWelcome($config, $insulate)
  19. {
  20. $client = $this->createClient(array('test_case' => 'Session', 'root_config' => $config));
  21. if ($insulate) {
  22. $client->insulate();
  23. }
  24. // no session
  25. $crawler = $client->request('GET', '/session');
  26. $this->assertContains('You are new here and gave no name.', $crawler->text());
  27. // remember name
  28. $crawler = $client->request('GET', '/session/drak');
  29. $this->assertContains('Hello drak, nice to meet you.', $crawler->text());
  30. // prove remembered name
  31. $crawler = $client->request('GET', '/session');
  32. $this->assertContains('Welcome back drak, nice to meet you.', $crawler->text());
  33. // clear session
  34. $crawler = $client->request('GET', '/session_logout');
  35. $this->assertContains('Session cleared.', $crawler->text());
  36. // prove cleared session
  37. $crawler = $client->request('GET', '/session');
  38. $this->assertContains('You are new here and gave no name.', $crawler->text());
  39. }
  40. /**
  41. * Tests flash messages work in practice.
  42. *
  43. * @dataProvider getConfigs
  44. */
  45. public function testFlash($config, $insulate)
  46. {
  47. $client = $this->createClient(array('test_case' => 'Session', 'root_config' => $config));
  48. if ($insulate) {
  49. $client->insulate();
  50. }
  51. // set flash
  52. $crawler = $client->request('GET', '/session_setflash/Hello%20world.');
  53. // check flash displays on redirect
  54. $this->assertContains('Hello world.', $client->followRedirect()->text());
  55. // check flash is gone
  56. $crawler = $client->request('GET', '/session_showflash');
  57. $this->assertContains('No flash was set.', $crawler->text());
  58. }
  59. /**
  60. * See if two separate insulated clients can run without
  61. * polluting eachother's session data.
  62. *
  63. * @dataProvider getConfigs
  64. */
  65. public function testTwoClients($config, $insulate)
  66. {
  67. // start first client
  68. $client1 = $this->createClient(array('test_case' => 'Session', 'root_config' => $config));
  69. if ($insulate) {
  70. $client1->insulate();
  71. }
  72. // start second client
  73. $client2 = $this->createClient(array('test_case' => 'Session', 'root_config' => $config));
  74. if ($insulate) {
  75. $client2->insulate();
  76. }
  77. // new session, so no name set.
  78. $crawler1 = $client1->request('GET', '/session');
  79. $this->assertContains('You are new here and gave no name.', $crawler1->text());
  80. // set name of client1
  81. $crawler1 = $client1->request('GET', '/session/client1');
  82. $this->assertContains('Hello client1, nice to meet you.', $crawler1->text());
  83. // no session for client2
  84. $crawler2 = $client2->request('GET', '/session');
  85. $this->assertContains('You are new here and gave no name.', $crawler2->text());
  86. // remember name client2
  87. $crawler2 = $client2->request('GET', '/session/client2');
  88. $this->assertContains('Hello client2, nice to meet you.', $crawler2->text());
  89. // prove remembered name of client1
  90. $crawler1 = $client1->request('GET', '/session');
  91. $this->assertContains('Welcome back client1, nice to meet you.', $crawler1->text());
  92. // prove remembered name of client2
  93. $crawler2 = $client2->request('GET', '/session');
  94. $this->assertContains('Welcome back client2, nice to meet you.', $crawler2->text());
  95. // clear client1
  96. $crawler1 = $client1->request('GET', '/session_logout');
  97. $this->assertContains('Session cleared.', $crawler1->text());
  98. // prove client1 data is cleared
  99. $crawler1 = $client1->request('GET', '/session');
  100. $this->assertContains('You are new here and gave no name.', $crawler1->text());
  101. // prove remembered name of client2 remains untouched.
  102. $crawler2 = $client2->request('GET', '/session');
  103. $this->assertContains('Welcome back client2, nice to meet you.', $crawler2->text());
  104. }
  105. public function getConfigs()
  106. {
  107. return array(
  108. // configfile, insulate
  109. array('config.yml', true),
  110. array('config.yml', false),
  111. );
  112. }
  113. }