PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/.dev/tests/Classes/Model/Product.php

https://github.com/istran/core
PHP | 299 lines | 174 code | 56 blank | 69 comment | 0 complexity | c2a30835b21fc9f7cd7c050660067ee1 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. // vim: set ts=4 sw=4 sts=4 et:
  3. /**
  4. * XLite\Model\Repo\Product class tests
  5. *
  6. * @category LiteCommerce
  7. * @package Tests
  8. * @subpackage Classes
  9. * @author Creative Development LLC <info@cdev.ru>
  10. * @copyright Copyright (c) 2010 Creative Development LLC <info@cdev.ru>. All rights reserved
  11. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  12. * @version GIT: $Id: 6e146348d3aad089510b094d831fe1e247427dd5 $
  13. * @link http://www.litecommerce.com/
  14. * @see ____file_see____
  15. * @since 3.0.0
  16. */
  17. include_once __DIR__ . '/AProduct.php';
  18. class XLite_Tests_Model_Product extends XLite_Tests_Model_AProduct
  19. {
  20. /**
  21. * getProductData
  22. *
  23. * @return void
  24. * @access protected
  25. * @see ____func_see____
  26. * @since 3.0.0
  27. */
  28. protected function getProductData()
  29. {
  30. return array(
  31. 'price' => array(4.99, null),
  32. 'sale_price' => array(3.54, null),
  33. 'sku' => array('test_sku', null),
  34. 'enabled' => array(true, null),
  35. 'weight' => array(2.88, null),
  36. 'tax_class' => array('test_class', null),
  37. 'free_shipping' => array(true, null),
  38. 'clean_url' => array('test_url', null),
  39. 'javascript' => array('test_js', null),
  40. 'name' => array('test name', null),
  41. 'description' => array('test description', null),
  42. 'brief_description' => array('test brief description', null),
  43. 'meta_tags' => array('test meta tags', null),
  44. 'meta_desc' => array('test meta description', null),
  45. 'meta_title' => array('test meta title', null),
  46. );
  47. }
  48. /**
  49. * testConstruct
  50. *
  51. * @return void
  52. * @access public
  53. * @see ____func_see____
  54. * @since 3.0.0
  55. */
  56. public function testConstruct()
  57. {
  58. $dataToSet = array();
  59. $dataToCheck = array();
  60. foreach ($this->getProductData() as $key => $data) {
  61. list($actual, $expected) = $data;
  62. $dataToSet[$key] = $actual;
  63. $dataToCheck[$key] = isset($expected) ? $expected : $actual;
  64. }
  65. $entity = new \XLite\Model\Product($dataToSet);
  66. foreach ($dataToCheck as $key => $value) {
  67. $this->assertEquals(
  68. $value,
  69. $entity->{'get' . \XLite\Core\Converter::convertToCamelCase($key)}(),
  70. 'Field "' . $key . '" does not match'
  71. );
  72. }
  73. // Order item
  74. $i = new \XLite\Model\OrderItem;
  75. $entity->addOrderItems($i);
  76. $this->assertEquals($i, $entity->getOrderItems()->get(0), 'check order item');
  77. // Image
  78. $i = new \XLite\Model\Image\Product\Image;
  79. $entity->addImages($i);
  80. $this->assertEquals($i, $entity->getImages()->get(0), 'check image');
  81. }
  82. /**
  83. * testAddCategoryProducts
  84. *
  85. * @return void
  86. * @access public
  87. * @see ____func_see____
  88. * @since 3.0.0
  89. */
  90. public function testAddCategoryProducts()
  91. {
  92. $c = \XLite\Core\Database::getRepo('XLite\Model\Category')->find(14015);
  93. $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(15090);
  94. $cp = new \XLite\Model\CategoryProducts();
  95. $cp->setCategory($c);
  96. $cp->setProduct($p);
  97. $result = $cp;
  98. // Check keys
  99. $this->assertNotNull($result->getCategory(), 'Invalid category');
  100. $this->assertNotNull($result->getProduct(), 'Invalid product');
  101. $this->assertEquals(14015, $result->getCategory()->getCategoryId(), 'Invalid category ID');
  102. $this->assertEquals(15090, $result->getProduct()->getProductId(), 'Invalid product ID');
  103. }
  104. /**
  105. * testAddOptionGroups
  106. *
  107. * @return void
  108. * @access public
  109. * @see ____func_see____
  110. * @since 3.0.0
  111. */
  112. public function testAddOptionGroups()
  113. {
  114. $entity = new \XLite\Model\Product();
  115. $entity->addOptionGroups(
  116. new XLite\Module\CDev\ProductOptions\Model\OptionGroup(
  117. array(
  118. 'type' => XLite\Module\CDev\ProductOptions\Model\OptionGroup::GROUP_TYPE,
  119. 'view_type' => XLite\Module\CDev\ProductOptions\Model\OptionGroup::SELECT_VISIBLE,
  120. )
  121. )
  122. );
  123. $result = array_pop($entity->getOptionGroups()->toArray());
  124. // Check keys
  125. $this->assertEquals(XLite\Module\CDev\ProductOptions\Model\OptionGroup::GROUP_TYPE, $result->getType(), 'Invalid group type');
  126. $this->assertEquals(XLite\Module\CDev\ProductOptions\Model\OptionGroup::SELECT_VISIBLE, $result->getViewType(), 'Invalid view type');
  127. }
  128. /**
  129. * testAddTranslations
  130. *
  131. * @return void
  132. * @access public
  133. * @see ____func_see____
  134. * @since 3.0.0
  135. */
  136. public function testAddTranslations()
  137. {
  138. $entity = new \XLite\Model\Product();
  139. $entity->addTranslations(new \XLite\Model\ProductTranslation(array('name' => 'test')));
  140. $result = array_pop($entity->getTranslations()->toArray());
  141. // Check keys
  142. $this->assertEquals('test', $result->getName(), 'Invalid product name');
  143. }
  144. /**
  145. * testGetProductId
  146. *
  147. * @return void
  148. * @access public
  149. * @see ____func_see____
  150. * @since 3.0.0
  151. */
  152. public function testGetProductId()
  153. {
  154. $result = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(15090);
  155. // Check entity
  156. $this->assertNotNull($result, 'Product not found');
  157. $this->assertEquals(15090, $result->getProductId(), 'Product ID does not match');
  158. }
  159. public function testisAvailable()
  160. {
  161. $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(16281);
  162. $p->setEnabled(true);
  163. $this->assertTrue($p->isAvailable(), 'check enabled');
  164. $p->setEnabled(false);
  165. $this->assertTrue($p->isAvailable(), 'check disabled (is admin zone)');
  166. }
  167. public function testgetTaxedPrice()
  168. {
  169. $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(16281);
  170. $this->assertEquals($p->getPrice(), $p->getTaxedPrice(), 'check taxed price (equals price)');
  171. }
  172. public function testgetListPrice()
  173. {
  174. $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(16281);
  175. $this->assertEquals($p->getTaxedPrice(), $p->getListPrice(), 'check taxed price (equals taxed price)');
  176. }
  177. public function testhasImage()
  178. {
  179. $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(15090);
  180. $this->assertTrue($p->hasImage(), 'check image');
  181. $p->getImages()->clear();
  182. $this->assertFalse($p->hasImage(), 'check empty image');
  183. }
  184. public function testgetImageURL()
  185. {
  186. $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(15090);
  187. $this->assertRegExp('/images.product.demo_p15090\.jpeg$/Ss', $p->getImageURL(), 'check image URL');
  188. $p->getImages()->clear();
  189. $this->assertNull($p->getImageURL(), 'check empty image URL');
  190. }
  191. public function testgetCategory()
  192. {
  193. $c = \XLite\Core\Database::getRepo('XLite\Model\Category')->find(14015);
  194. $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(16281);
  195. $cp = new \XLite\Model\CategoryProducts();
  196. $cp->setCategory($c);
  197. $cp->setProduct($p);
  198. \XLite\Core\Database::getEM()->persist($cp);
  199. \XLite\Core\Database::getEM()->flush();
  200. \XLite\Core\Database::getEM()->clear();
  201. $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(16281);
  202. $cp = $p->getCategory(14015);
  203. $this->assertTrue($cp instanceof \XLite\Model\Category, 'check class');
  204. $this->assertEquals(14015, $cp->getCategoryId(), 'check category id');
  205. $cp = $p->getCategory(999999999);
  206. $this->assertTrue($cp instanceof \XLite\Model\Category, 'check class #2');
  207. $this->assertTrue(is_null($cp->getCategoryId()), 'check category is null');
  208. $cp = $p->getCategory();
  209. $this->assertTrue($cp instanceof \XLite\Model\Category, 'check class #3');
  210. $this->assertEquals(14015, $cp->getCategoryId(), 'check category id #3');
  211. }
  212. public function testgetOrderBy()
  213. {
  214. $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(16281);
  215. $this->assertEquals(10, $p->getOrderBy(14009), 'check order by of exist link');
  216. $this->assertEquals(0, $p->getOrderBy(999999999), 'check order by of NON exist link');
  217. }
  218. public function testcountImages()
  219. {
  220. $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(15090);
  221. $this->assertEquals(1, $p->countImages(), 'check 1 images');
  222. $p->getImages()->clear();
  223. $this->assertEquals(0, $p->countImages(), 'check zero images');
  224. }
  225. public function testgetCommonDescription()
  226. {
  227. $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(16282);
  228. $this->assertEquals(
  229. '<h5>Cucumber</h5>
  230. <p>The cucumber (Cucumis sativus) is a widely cultivated plant in the gourd family Cucurbitaceae, which includes squash, and in the same genus as the muskmelon.</p>
  231. <p>&nbsp;</p>
  232. <div style="padding: 24px 24px 24px 21px; display: block; background-color: #ececec;">From <a style="color: #1e7ec8; text-decoration: underline;" title="Wikipedia" href="http://en.wikipedia.org">Wikipedia</a>, the free encyclopedia</div>',
  233. $p->getCommonDescription(),
  234. 'check as description'
  235. );
  236. $p->setBriefDescription('test brief description');
  237. $this->assertEquals(
  238. 'test brief description',
  239. $p->getCommonDescription(),
  240. 'check as description'
  241. );
  242. }
  243. }