PageRenderTime 74ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/unit/testsuite/Mage/Core/Model/Design/Fallback/CachingProxyTest.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 223 lines | 124 code | 27 blank | 72 comment | 0 complexity | bcfe0a367fd1eb8990236201e5c13eec 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 Magento
  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_Design_Fallback_CachingProxyTest extends PHPUnit_Framework_TestCase
  28. {
  29. /**
  30. * Temp directory for the model to store maps
  31. *
  32. * @var string
  33. */
  34. protected static $_tmpDir;
  35. /**
  36. * Base dir as passed to the model
  37. *
  38. * @var string
  39. */
  40. protected $_baseDir;
  41. /**
  42. * Mock of the model to be tested. Operates the mocked fallback object.
  43. *
  44. * @var Mage_Core_Model_Design_Fallback_CachingProxy|PHPUnit_Framework_MockObject_MockObject
  45. */
  46. protected $_model;
  47. /**
  48. * Mocked fallback object, with file resolution methods ready to be substituted.
  49. *
  50. * @var Mage_Core_Model_Design_Fallback|PHPUnit_Framework_MockObject_MockObject
  51. */
  52. protected $_fallback;
  53. /**
  54. * Mocked theme object
  55. *
  56. * @var Mage_Core_Model_Theme
  57. */
  58. protected $_theme;
  59. public static function setUpBeforeClass()
  60. {
  61. self::$_tmpDir = TESTS_TEMP_DIR . DIRECTORY_SEPARATOR . 'fallback';
  62. mkdir(self::$_tmpDir);
  63. }
  64. public function setUp()
  65. {
  66. $this->_baseDir = DIRECTORY_SEPARATOR . 'base' . DIRECTORY_SEPARATOR . 'dir';
  67. $this->_theme = $this->getMock('Mage_Core_Model_Theme', array(), array(), '', false);
  68. $params = array(
  69. 'area' => 'frontend',
  70. 'themeModel' => $this->_theme,
  71. 'locale' => 'en_US',
  72. 'appConfig' => false,
  73. 'canSaveMap' => false,
  74. 'mapDir' => self::$_tmpDir,
  75. 'baseDir' => $this->_baseDir
  76. );
  77. $this->_fallback = $this->getMock(
  78. 'Mage_Core_Model_Design_Fallback',
  79. array('getFile', 'getLocaleFile', 'getViewFile'),
  80. array($params)
  81. );
  82. $this->_model = $this->getMock(
  83. 'Mage_Core_Model_Design_Fallback_CachingProxy',
  84. array('_getFallback'),
  85. array($params)
  86. );
  87. $this->_model->expects($this->any())
  88. ->method('_getFallback')
  89. ->will($this->returnValue($this->_fallback));
  90. }
  91. /**
  92. * Calls are repeated twice to verify, that fallback is used only once, and next time a proper value is returned
  93. * via cached map.
  94. */
  95. public function testGetFile()
  96. {
  97. $module = 'Some_Module';
  98. $expected = $this->_baseDir . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'theme_file.ext';
  99. $expected = str_replace('/', DIRECTORY_SEPARATOR, $expected);
  100. $this->_fallback->expects($this->once())
  101. ->method('getFile')
  102. ->with('file.ext', $module)
  103. ->will($this->returnValue($expected));
  104. $actual = $this->_model->getFile('file.ext', $module);
  105. $this->assertEquals($expected, $actual);
  106. $actual = $this->_model->getFile('file.ext', $module);
  107. $this->assertEquals($expected, $actual);
  108. }
  109. /**
  110. * Calls are repeated twice to verify, that fallback is used only once, and next time a proper value is returned
  111. * via cached map.
  112. */
  113. public function testGetLocaleFile()
  114. {
  115. $expected = $this->_baseDir . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'locale_file.ext';
  116. $this->_fallback->expects($this->once())
  117. ->method('getLocaleFile')
  118. ->with('file.ext')
  119. ->will($this->returnValue($expected));
  120. $actual = $this->_model->getLocaleFile('file.ext');
  121. $this->assertEquals($expected, $actual);
  122. $actual = $this->_model->getLocaleFile('file.ext');
  123. $this->assertEquals($expected, $actual);
  124. }
  125. /**
  126. * Calls are repeated twice to verify, that fallback is used only once, and next time a proper value is returned
  127. * via cached map.
  128. */
  129. public function testGetViewFile()
  130. {
  131. $module = 'Some_Module';
  132. $expected = $this->_baseDir . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'view_file.ext';
  133. $this->_fallback->expects($this->once())
  134. ->method('getViewFile')
  135. ->with('file.ext', $module)
  136. ->will($this->returnValue($expected));
  137. $actual = $this->_model->getViewFile('file.ext', $module);
  138. $this->assertEquals($expected, $actual);
  139. $actual = $this->_model->getViewFile('file.ext', $module);
  140. $this->assertEquals($expected, $actual);
  141. }
  142. /**
  143. * Test that proxy caches published skin path, and further calls do not use fallback model
  144. */
  145. public function testNotifySkinFilePublished()
  146. {
  147. $module = 'Some_Module';
  148. $file = $this->_baseDir . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'file.ext';
  149. $this->_fallback->expects($this->once())
  150. ->method('getViewFile')
  151. ->with($file, $module)
  152. ->will($this->returnValue(null));
  153. // Empty at first
  154. $this->assertNull($this->_model->getViewFile($file, $module));
  155. // Store something
  156. $publicFilePath = $this->_baseDir . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'file.ext';
  157. $result = $this->_model->notifyViewFilePublished($publicFilePath, $file, $module);
  158. $this->assertSame($this->_model, $result);
  159. // Stored successfully
  160. $storedFilePath = $this->_model->getViewFile($file, $module);
  161. $this->assertEquals($publicFilePath, $storedFilePath);
  162. }
  163. /**
  164. * Tests that proxy saves data between instantiations
  165. */
  166. public function testSaving()
  167. {
  168. $module = 'Some_Module';
  169. $file = 'internal/path/to/view_file.ext';
  170. $expectedPublicFile = 'public/path/to/view_file.ext';
  171. $params = array(
  172. 'area' => 'frontend',
  173. 'themeModel' => $this->_theme,
  174. 'locale' => 'en_US',
  175. 'canSaveMap' => true,
  176. 'mapDir' => self::$_tmpDir,
  177. 'baseDir' => ''
  178. );
  179. $model = new Mage_Core_Model_Design_Fallback_CachingProxy($params);
  180. $model->notifyViewFilePublished($expectedPublicFile, $file, $module);
  181. $globPath = self::$_tmpDir . DIRECTORY_SEPARATOR . '*.*';
  182. $this->assertEmpty(glob($globPath));
  183. unset($model);
  184. $this->assertNotEmpty(glob($globPath));
  185. /** @var $model Mage_Core_Model_Design_Fallback_CachingProxy */
  186. $model = $this->getMock(
  187. 'Mage_Core_Model_Design_Fallback_CachingProxy',
  188. array('_getFallback'),
  189. array($params)
  190. );
  191. $model->expects($this->never())
  192. ->method('_getFallback');
  193. $actualPublicFile = $model->getViewFile($file, $module);
  194. $this->assertEquals($expectedPublicFile, $actualPublicFile);
  195. }
  196. }