PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/dev/tests/unit/testsuite/Mage/Core/Model/Resource/SessionTest.php

https://bitbucket.org/jokusafet/magento2
PHP | 242 lines | 127 code | 23 blank | 92 comment | 3 complexity | bb272d9cdc92a16db2a5e4b6ee3ad10c MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @subpackage unit_tests
  24. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  25. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  26. */
  27. class Mage_Core_Model_Resource_SessionTest extends PHPUnit_Framework_TestCase
  28. {
  29. /**
  30. * Session table name
  31. */
  32. const SESSION_TABLE = 'session_table_name';
  33. /**#@+
  34. * Table column names
  35. */
  36. const COLUMN_SESSION_ID = 'session_id';
  37. const COLUMN_SESSION_DATA = 'session_data';
  38. const COLUMN_SESSION_EXPIRES = 'session_expires';
  39. /**#@-*/
  40. /**
  41. * Test select object
  42. */
  43. const SELECT_OBJECT = 'select_object';
  44. /**#@+
  45. * Test session data
  46. */
  47. const SESSION_ID = 'custom_session_id';
  48. const SESSION_DATA = 'custom_session_data';
  49. /**#@-*/
  50. /**
  51. * Model under test
  52. *
  53. * @var Mage_Core_Model_Resource_Session
  54. */
  55. protected $_model;
  56. protected function tearDown()
  57. {
  58. unset($this->_model);
  59. }
  60. /**
  61. * Data provider for testRead
  62. *
  63. * @return array
  64. */
  65. public function readDataProvider()
  66. {
  67. return array(
  68. 'session_encoded' => array('$dataEncoded' => true),
  69. 'session_not_encoded' => array('$dataEncoded' => false),
  70. );
  71. }
  72. /**
  73. * @param bool $isDataEncoded
  74. *
  75. * @dataProvider readDataProvider
  76. */
  77. public function testRead($isDataEncoded)
  78. {
  79. $this->_prepareMockForRead($isDataEncoded);
  80. $result = $this->_model->read(self::SESSION_ID);
  81. $this->assertEquals(self::SESSION_DATA, $result);
  82. }
  83. /**
  84. * Prepares mock for test model with specified connections
  85. *
  86. * @param PHPUnit_Framework_MockObject_MockObject $connection
  87. */
  88. protected function _prepareResourceMock($connection)
  89. {
  90. $resource = $this->getMock('Mage_Core_Model_Resource', array('getTableName', 'getConnection'));
  91. $resource->expects($this->once())
  92. ->method('getTableName')
  93. ->will($this->returnValue(self::SESSION_TABLE));
  94. $resource->expects($this->once())
  95. ->method('getConnection')
  96. ->will($this->returnValue($connection));
  97. $this->_model = new Mage_Core_Model_Resource_Session($resource);
  98. }
  99. /**
  100. * Prepare mocks for testRead
  101. *
  102. * @param bool $isDataEncoded
  103. */
  104. protected function _prepareMockForRead($isDataEncoded)
  105. {
  106. $connection = $this->getMock('Varien_Db_Adapter_Pdo_Mysql',
  107. array('select', 'from', 'where', 'fetchOne'), array(), '', false
  108. );
  109. $connection->expects($this->once())
  110. ->method('select')
  111. ->will($this->returnSelf());
  112. $connection->expects($this->once())
  113. ->method('from')
  114. ->with(self::SESSION_TABLE, array(self::COLUMN_SESSION_DATA))
  115. ->will($this->returnSelf());
  116. $connection->expects($this->once())
  117. ->method('where')
  118. ->with(self::COLUMN_SESSION_ID . ' = :' . self::COLUMN_SESSION_ID)
  119. ->will($this->returnValue(self::SELECT_OBJECT));
  120. $sessionData = self::SESSION_DATA;
  121. if ($isDataEncoded) {
  122. $sessionData = base64_encode($sessionData);
  123. }
  124. $connection->expects($this->once())
  125. ->method('fetchOne')
  126. ->with(self::SELECT_OBJECT, array(self::COLUMN_SESSION_ID => self::SESSION_ID))
  127. ->will($this->returnValue($sessionData));
  128. $this->_prepareResourceMock($connection);
  129. }
  130. /**
  131. * Data provider for testWrite
  132. *
  133. * @return array
  134. */
  135. public function writeDataProvider()
  136. {
  137. return array(
  138. 'session_exists' => array('$sessionExists' => true),
  139. 'session_not_exists' => array('$sessionExists' => false),
  140. );
  141. }
  142. /**
  143. * @param bool $sessionExists
  144. *
  145. * @dataProvider writeDataProvider
  146. */
  147. public function testWrite($sessionExists)
  148. {
  149. $this->_prepareMockForWrite($sessionExists);
  150. $this->assertTrue($this->_model->write(self::SESSION_ID, self::SESSION_DATA));
  151. }
  152. /**
  153. * Prepare mocks for testWrite
  154. *
  155. * @param bool $sessionExists
  156. */
  157. protected function _prepareMockForWrite($sessionExists)
  158. {
  159. $connection = $this->getMock('Varien_Db_Adapter_Pdo_Mysql',
  160. array('select', 'from', 'where', 'fetchOne', 'update', 'insert'), array(), '', false
  161. );
  162. $connection->expects($this->once())
  163. ->method('select')
  164. ->will($this->returnSelf());
  165. $connection->expects($this->once())
  166. ->method('from')
  167. ->with(self::SESSION_TABLE)
  168. ->will($this->returnSelf());
  169. $connection->expects($this->once())
  170. ->method('where')
  171. ->with(self::COLUMN_SESSION_ID . ' = :' . self::COLUMN_SESSION_ID)
  172. ->will($this->returnValue(self::SELECT_OBJECT));
  173. $connection->expects($this->once())
  174. ->method('fetchOne')
  175. ->with(self::SELECT_OBJECT, array(self::COLUMN_SESSION_ID => self::SESSION_ID))
  176. ->will($this->returnValue($sessionExists));
  177. if ($sessionExists) {
  178. $connection->expects($this->never())
  179. ->method('insert');
  180. $connection->expects($this->once())
  181. ->method('update')
  182. ->will($this->returnCallback(array($this, 'verifyUpdate')));
  183. } else {
  184. $connection->expects($this->once())
  185. ->method('insert')
  186. ->will($this->returnCallback(array($this, 'verifyInsert')));
  187. $connection->expects($this->never())
  188. ->method('update');
  189. }
  190. $this->_prepareResourceMock($connection);
  191. }
  192. /**
  193. * Verify arguments of insert method
  194. *
  195. * @param string $table
  196. * @param array $bind
  197. */
  198. public function verifyInsert($table, array $bind)
  199. {
  200. $this->assertEquals(self::SESSION_TABLE, $table);
  201. $this->assertInternalType('int', $bind[self::COLUMN_SESSION_EXPIRES]);
  202. $this->assertEquals(base64_encode(self::SESSION_DATA), $bind[self::COLUMN_SESSION_DATA]);
  203. $this->assertEquals(self::SESSION_ID, $bind[self::COLUMN_SESSION_ID]);
  204. }
  205. /**
  206. * Verify arguments of update method
  207. *
  208. * @param string $table
  209. * @param array $bind
  210. * @param array $where
  211. */
  212. public function verifyUpdate($table, array $bind, array $where)
  213. {
  214. $this->assertEquals(self::SESSION_TABLE, $table);
  215. $this->assertInternalType('int', $bind[self::COLUMN_SESSION_EXPIRES]);
  216. $this->assertEquals(base64_encode(self::SESSION_DATA), $bind[self::COLUMN_SESSION_DATA]);
  217. $this->assertEquals(array(self::COLUMN_SESSION_ID . '=?' => self::SESSION_ID), $where);
  218. }
  219. }