SessionHandlerProxyTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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\HttpFoundation\Tests\Session\Storage\Proxy;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
  13. /**
  14. * Tests for SessionHandlerProxy class.
  15. *
  16. * @author Drak <drak@zikula.org>
  17. *
  18. * @runTestsInSeparateProcesses
  19. * @preserveGlobalState disabled
  20. */
  21. class SessionHandlerProxyTest extends TestCase
  22. {
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_Matcher
  25. */
  26. private $mock;
  27. /**
  28. * @var SessionHandlerProxy
  29. */
  30. private $proxy;
  31. protected function setUp()
  32. {
  33. $this->mock = $this->getMockBuilder('SessionHandlerInterface')->getMock();
  34. $this->proxy = new SessionHandlerProxy($this->mock);
  35. }
  36. protected function tearDown()
  37. {
  38. $this->mock = null;
  39. $this->proxy = null;
  40. }
  41. public function testOpenTrue()
  42. {
  43. $this->mock->expects($this->once())
  44. ->method('open')
  45. ->will($this->returnValue(true));
  46. $this->assertFalse($this->proxy->isActive());
  47. $this->proxy->open('name', 'id');
  48. $this->assertFalse($this->proxy->isActive());
  49. }
  50. public function testOpenFalse()
  51. {
  52. $this->mock->expects($this->once())
  53. ->method('open')
  54. ->will($this->returnValue(false));
  55. $this->assertFalse($this->proxy->isActive());
  56. $this->proxy->open('name', 'id');
  57. $this->assertFalse($this->proxy->isActive());
  58. }
  59. public function testClose()
  60. {
  61. $this->mock->expects($this->once())
  62. ->method('close')
  63. ->will($this->returnValue(true));
  64. $this->assertFalse($this->proxy->isActive());
  65. $this->proxy->close();
  66. $this->assertFalse($this->proxy->isActive());
  67. }
  68. public function testCloseFalse()
  69. {
  70. $this->mock->expects($this->once())
  71. ->method('close')
  72. ->will($this->returnValue(false));
  73. $this->assertFalse($this->proxy->isActive());
  74. $this->proxy->close();
  75. $this->assertFalse($this->proxy->isActive());
  76. }
  77. public function testRead()
  78. {
  79. $this->mock->expects($this->once())
  80. ->method('read');
  81. $this->proxy->read('id');
  82. }
  83. public function testWrite()
  84. {
  85. $this->mock->expects($this->once())
  86. ->method('write');
  87. $this->proxy->write('id', 'data');
  88. }
  89. public function testDestroy()
  90. {
  91. $this->mock->expects($this->once())
  92. ->method('destroy');
  93. $this->proxy->destroy('id');
  94. }
  95. public function testGc()
  96. {
  97. $this->mock->expects($this->once())
  98. ->method('gc');
  99. $this->proxy->gc(86400);
  100. }
  101. /**
  102. * @requires PHPUnit 5.1
  103. */
  104. public function testValidateId()
  105. {
  106. $mock = $this->getMockBuilder(['SessionHandlerInterface', 'SessionUpdateTimestampHandlerInterface'])->getMock();
  107. $mock->expects($this->once())
  108. ->method('validateId');
  109. $proxy = new SessionHandlerProxy($mock);
  110. $proxy->validateId('id');
  111. $this->assertTrue($this->proxy->validateId('id'));
  112. }
  113. /**
  114. * @requires PHPUnit 5.1
  115. */
  116. public function testUpdateTimestamp()
  117. {
  118. $mock = $this->getMockBuilder(['SessionHandlerInterface', 'SessionUpdateTimestampHandlerInterface'])->getMock();
  119. $mock->expects($this->once())
  120. ->method('updateTimestamp');
  121. $proxy = new SessionHandlerProxy($mock);
  122. $proxy->updateTimestamp('id', 'data');
  123. $this->mock->expects($this->once())
  124. ->method('write');
  125. $this->proxy->updateTimestamp('id', 'data');
  126. }
  127. }