DbTableTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Session\Test\Unit\SaveHandler;
  7. class DbTableTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * Session table name
  11. */
  12. const SESSION_TABLE = 'session_table_name';
  13. /**#@+
  14. * Table column names
  15. */
  16. const COLUMN_SESSION_ID = 'session_id';
  17. const COLUMN_SESSION_DATA = 'session_data';
  18. const COLUMN_SESSION_EXPIRES = 'session_expires';
  19. /**#@-*/
  20. /**
  21. * Test select object
  22. */
  23. const SELECT_OBJECT = 'select_object';
  24. /**#@+
  25. * Test session data
  26. */
  27. const SESSION_ID = 'custom_session_id';
  28. const SESSION_DATA = 'custom_session_data';
  29. /**#@-*/
  30. /**
  31. * Model under test
  32. *
  33. * @var \Magento\Framework\Session\SaveHandler\DbTable
  34. */
  35. protected $_model;
  36. protected function tearDown()
  37. {
  38. unset($this->_model);
  39. }
  40. /**
  41. * Data provider for testRead
  42. *
  43. * @return array
  44. */
  45. public function readDataProvider()
  46. {
  47. return [
  48. 'session_encoded' => ['$dataEncoded' => true],
  49. 'session_not_encoded' => ['$dataEncoded' => false]
  50. ];
  51. }
  52. public function testCheckConnection()
  53. {
  54. $connection = $this->createPartialMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class, ['isTableExists']);
  55. $connection->expects(
  56. $this->atLeastOnce()
  57. )->method(
  58. 'isTableExists'
  59. )->with(
  60. $this->equalTo(self::SESSION_TABLE)
  61. )->will(
  62. $this->returnValue(true)
  63. );
  64. $resource = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  65. $resource->expects($this->once())->method('getTableName')->will($this->returnValue(self::SESSION_TABLE));
  66. $resource->expects($this->once())->method('getConnection')->will($this->returnValue($connection));
  67. $this->_model = new \Magento\Framework\Session\SaveHandler\DbTable($resource);
  68. $method = new \ReflectionMethod(\Magento\Framework\Session\SaveHandler\DbTable::class, 'checkConnection');
  69. $method->setAccessible(true);
  70. $this->assertNull($method->invoke($this->_model));
  71. }
  72. /**
  73. * @expectedException \Magento\Framework\Exception\SessionException
  74. * @expectedExceptionMessage The write connection to the database isn't available. Please try again later.
  75. */
  76. public function testCheckConnectionNoConnection()
  77. {
  78. $resource = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  79. $resource->expects($this->once())->method('getTableName')->will($this->returnValue(self::SESSION_TABLE));
  80. $resource->expects($this->once())->method('getConnection')->will($this->returnValue(null));
  81. $this->_model = new \Magento\Framework\Session\SaveHandler\DbTable($resource);
  82. $method = new \ReflectionMethod(\Magento\Framework\Session\SaveHandler\DbTable::class, 'checkConnection');
  83. $method->setAccessible(true);
  84. $this->assertNull($method->invoke($this->_model));
  85. }
  86. /**
  87. * @expectedException \Magento\Framework\Exception\SessionException
  88. * @expectedExceptionMessage The database storage table doesn't exist. Verify the table and try again.
  89. */
  90. public function testCheckConnectionNoTable()
  91. {
  92. $connection = $this->createPartialMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class, ['isTableExists']);
  93. $connection->expects(
  94. $this->once()
  95. )->method(
  96. 'isTableExists'
  97. )->with(
  98. $this->equalTo(self::SESSION_TABLE)
  99. )->will(
  100. $this->returnValue(false)
  101. );
  102. $resource = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  103. $resource->expects($this->once())->method('getTableName')->will($this->returnValue(self::SESSION_TABLE));
  104. $resource->expects($this->once())->method('getConnection')->will($this->returnValue($connection));
  105. $this->_model = new \Magento\Framework\Session\SaveHandler\DbTable($resource);
  106. $method = new \ReflectionMethod(\Magento\Framework\Session\SaveHandler\DbTable::class, 'checkConnection');
  107. $method->setAccessible(true);
  108. $this->assertNull($method->invoke($this->_model));
  109. }
  110. /**
  111. * @param bool $isDataEncoded
  112. *
  113. * @dataProvider readDataProvider
  114. */
  115. public function testRead($isDataEncoded)
  116. {
  117. $this->_prepareMockForRead($isDataEncoded);
  118. $result = $this->_model->read(self::SESSION_ID);
  119. $this->assertEquals(self::SESSION_DATA, $result);
  120. }
  121. /**
  122. * Prepares mock for test model with specified connections
  123. *
  124. * @param \PHPUnit_Framework_MockObject_MockObject $connection
  125. */
  126. protected function _prepareResourceMock($connection)
  127. {
  128. $resource = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  129. $resource->expects($this->once())->method('getTableName')->will($this->returnValue(self::SESSION_TABLE));
  130. $resource->expects($this->once())->method('getConnection')->will($this->returnValue($connection));
  131. $this->_model = new \Magento\Framework\Session\SaveHandler\DbTable($resource);
  132. }
  133. /**
  134. * Prepare mocks for testRead
  135. *
  136. * @param bool $isDataEncoded
  137. */
  138. protected function _prepareMockForRead($isDataEncoded)
  139. {
  140. $connection = $this->createPartialMock(
  141. \Magento\Framework\DB\Adapter\Pdo\Mysql::class,
  142. ['select', 'from', 'where', 'fetchOne', 'isTableExists']
  143. );
  144. $connection->expects($this->once())->method('isTableExists')->will($this->returnValue(true));
  145. $connection->expects($this->once())->method('select')->will($this->returnSelf());
  146. $connection->expects(
  147. $this->once()
  148. )->method(
  149. 'from'
  150. )->with(
  151. self::SESSION_TABLE,
  152. [self::COLUMN_SESSION_DATA]
  153. )->will(
  154. $this->returnSelf()
  155. );
  156. $connection->expects(
  157. $this->once()
  158. )->method(
  159. 'where'
  160. )->with(
  161. self::COLUMN_SESSION_ID . ' = :' . self::COLUMN_SESSION_ID
  162. )->will(
  163. $this->returnValue(self::SELECT_OBJECT)
  164. );
  165. $sessionData = self::SESSION_DATA;
  166. if ($isDataEncoded) {
  167. $sessionData = base64_encode($sessionData);
  168. }
  169. $connection->expects(
  170. $this->once()
  171. )->method(
  172. 'fetchOne'
  173. )->with(
  174. self::SELECT_OBJECT,
  175. [self::COLUMN_SESSION_ID => self::SESSION_ID]
  176. )->will(
  177. $this->returnValue($sessionData)
  178. );
  179. $this->_prepareResourceMock($connection);
  180. }
  181. /**
  182. * Data provider for testWrite
  183. *
  184. * @return array
  185. */
  186. public function writeDataProvider()
  187. {
  188. return [
  189. 'session_exists' => ['$sessionExists' => true],
  190. 'session_not_exists' => ['$sessionExists' => false]
  191. ];
  192. }
  193. /**
  194. * @param bool $sessionExists
  195. *
  196. * @dataProvider writeDataProvider
  197. */
  198. public function testWrite($sessionExists)
  199. {
  200. $this->_prepareMockForWrite($sessionExists);
  201. $this->assertTrue($this->_model->write(self::SESSION_ID, self::SESSION_DATA));
  202. }
  203. /**
  204. * Prepare mocks for testWrite
  205. *
  206. * @param bool $sessionExists
  207. */
  208. protected function _prepareMockForWrite($sessionExists)
  209. {
  210. $connection = $this->createPartialMock(
  211. \Magento\Framework\DB\Adapter\Pdo\Mysql::class,
  212. ['select', 'from', 'where', 'fetchOne', 'update', 'insert', 'isTableExists']
  213. );
  214. $connection->expects($this->once())->method('isTableExists')->will($this->returnValue(true));
  215. $connection->expects($this->once())->method('select')->will($this->returnSelf());
  216. $connection->expects($this->once())->method('from')->with(self::SESSION_TABLE)->will($this->returnSelf());
  217. $connection->expects(
  218. $this->once()
  219. )->method(
  220. 'where'
  221. )->with(
  222. self::COLUMN_SESSION_ID . ' = :' . self::COLUMN_SESSION_ID
  223. )->will(
  224. $this->returnValue(self::SELECT_OBJECT)
  225. );
  226. $connection->expects(
  227. $this->once()
  228. )->method(
  229. 'fetchOne'
  230. )->with(
  231. self::SELECT_OBJECT,
  232. [self::COLUMN_SESSION_ID => self::SESSION_ID]
  233. )->will(
  234. $this->returnValue($sessionExists)
  235. );
  236. if ($sessionExists) {
  237. $connection->expects($this->never())->method('insert');
  238. $connection->expects(
  239. $this->once()
  240. )->method(
  241. 'update'
  242. )->will(
  243. $this->returnCallback([$this, 'verifyUpdate'])
  244. );
  245. } else {
  246. $connection->expects(
  247. $this->once()
  248. )->method(
  249. 'insert'
  250. )->will(
  251. $this->returnCallback([$this, 'verifyInsert'])
  252. );
  253. $connection->expects($this->never())->method('update');
  254. }
  255. $this->_prepareResourceMock($connection);
  256. }
  257. /**
  258. * Verify arguments of insert method
  259. *
  260. * @param string $table
  261. * @param array $bind
  262. */
  263. public function verifyInsert($table, array $bind)
  264. {
  265. $this->assertEquals(self::SESSION_TABLE, $table);
  266. $this->assertInternalType('int', $bind[self::COLUMN_SESSION_EXPIRES]);
  267. $this->assertEquals(base64_encode(self::SESSION_DATA), $bind[self::COLUMN_SESSION_DATA]);
  268. $this->assertEquals(self::SESSION_ID, $bind[self::COLUMN_SESSION_ID]);
  269. }
  270. /**
  271. * Verify arguments of update method
  272. *
  273. * @param string $table
  274. * @param array $bind
  275. * @param array $where
  276. */
  277. public function verifyUpdate($table, array $bind, array $where)
  278. {
  279. $this->assertEquals(self::SESSION_TABLE, $table);
  280. $this->assertInternalType('int', $bind[self::COLUMN_SESSION_EXPIRES]);
  281. $this->assertEquals(base64_encode(self::SESSION_DATA), $bind[self::COLUMN_SESSION_DATA]);
  282. $this->assertEquals([self::COLUMN_SESSION_ID . '=?' => self::SESSION_ID], $where);
  283. }
  284. }