PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/.dev/tests/Classes/Module/CDev/Bestsellers/Model/Repo/Product.php

https://github.com/istran/core
PHP | 228 lines | 86 code | 42 blank | 100 comment | 2 complexity | 60b9ca208cfe5f8b267b29bec1c47848 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. * LiteCommerce
  5. *
  6. * NOTICE OF LICENSE
  7. *
  8. * This source file is subject to the Open Software License (OSL 3.0)
  9. * that is bundled with this package in the file LICENSE.txt.
  10. * It is also available through the world-wide-web at this URL:
  11. * http://opensource.org/licenses/osl-3.0.php
  12. * If you did not receive a copy of the license and are unable to
  13. * obtain it through the world-wide-web, please send an email
  14. * to licensing@litecommerce.com so we can send you a copy immediately.
  15. *
  16. * @category LiteCommerce
  17. * @package Tests
  18. * @subpackage Classes
  19. * @author Creative Development LLC <info@cdev.ru>
  20. * @copyright Copyright (c) 2010 Creative Development LLC <info@cdev.ru>. All rights reserved
  21. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @version GIT: $Id: 007df39432351ca5b6b52cb01a68b1754633b606 $
  23. * @link http://www.litecommerce.com/
  24. * @see ____file_see____
  25. * @since 3.0.0
  26. */
  27. class XLite_Tests_Module_CDev_Bestsellers_Model_Repo_Product extends XLite_Tests_Model_OrderAbstract
  28. {
  29. /**
  30. * Product id constants
  31. */
  32. const PR1 = '00002';
  33. const PR2 = '00041';
  34. const PR3 = '00043';
  35. const PR4 = '00047';
  36. /**
  37. * Category id for the PR1 product
  38. */
  39. const CATEGORY = 'apparel';
  40. /**
  41. * First test sequence of the bestsellers
  42. *
  43. * @var array
  44. * @access protected
  45. * @see ____var_see____
  46. * @since 1.0.0
  47. */
  48. protected $test1 = array(
  49. 0 => self::PR1,
  50. 1 => self::PR2,
  51. 2 => self::PR4,
  52. );
  53. /**
  54. * Second test sequence of the bestsellers
  55. *
  56. * @var array
  57. * @access protected
  58. * @see ____var_see____
  59. * @since 1.0.0
  60. */
  61. protected $test2 = array(
  62. 0 => self::PR1,
  63. 1 => self::PR3,
  64. 2 => self::PR2,
  65. );
  66. /**
  67. * Test of bestseller for the root category
  68. *
  69. * @return void
  70. * @access public
  71. * @see ____func_see____
  72. * @since 1.0.0
  73. */
  74. public function testFindBestsellersRoot()
  75. {
  76. /**
  77. * First order goes with processed status
  78. */
  79. $order = $this->getLocalTestOrder(
  80. \XLite\Model\Order::STATUS_PROCESSED,
  81. array(
  82. self::PR1 => 500,
  83. self::PR2 => 40,
  84. self::PR4 => 30,
  85. )
  86. );
  87. $best = $this->findBestsellers(3);
  88. $this->assertEquals(count($this->test1), count($best), 'Wrong number of bestsellers was returned. (1)');
  89. /**
  90. * First sequence
  91. */
  92. foreach ($this->test1 as $index => $id) {
  93. $this->assertTrue(isset($best[$index]), 'Not set #' . $index . ' product in bestsellers (1)');
  94. $this->assertEquals($best[$index]->getSku(), $id, 'Wrong #' . $index . ' product in bestsellers (1)');
  95. }
  96. /**
  97. * Second order goes with completed status
  98. */
  99. $order = $this->getLocalTestOrder(
  100. \XLite\Model\Order::STATUS_COMPLETED,
  101. array(
  102. self::PR3 => 45,
  103. )
  104. );
  105. $best = $this->findBestsellers(3);
  106. $this->assertEquals(count($this->test2), count($best), 'Wrong number of bestsellers was returned. (2)');
  107. /**
  108. * Second sequence
  109. */
  110. foreach ($this->test2 as $index => $id) {
  111. $this->assertTrue(isset($best[$index]), 'Not set #' . $index . ' product in bestsellers (2)');
  112. $this->assertEquals($best[$index]->getSku(), $id, 'Wrong #' . $index . ' product in bestsellers (2)');
  113. }
  114. }
  115. /**
  116. * Test of bestseller in some non-root category
  117. *
  118. * @return void
  119. * @access public
  120. * @see ____func_see____
  121. * @since 1.0.0
  122. */
  123. public function testFindBestsellersCategory()
  124. {
  125. $order = $this->getLocalTestOrder(
  126. \XLite\Model\Order::STATUS_COMPLETED,
  127. array(
  128. self::PR1 => 500,
  129. self::PR2 => 400,
  130. self::PR4 => 300,
  131. )
  132. );
  133. $c = \XLite\Core\Database::getRepo('XLite\Model\Category')->findOneBy(array('cleanUrl' => self::CATEGORY));
  134. $best = $this->findBestsellers(1, $c->getCategoryId());
  135. $this->assertEquals(1, count($best), 'Wrong number of bestsellers was returned (1)');
  136. $one = $best[0];
  137. $this->assertEquals(self::PR1, $one->getSku(), 'Wrong root category bestsellers list');
  138. }
  139. /**
  140. * FOR INNER USE ONLY
  141. */
  142. /**
  143. * Prepare order
  144. *
  145. * @param mixed $status ____param_comment____
  146. * @param array $items ____param_comment____
  147. *
  148. * @return void
  149. * @access protected
  150. * @see ____func_see____
  151. * @since 1.0.0
  152. */
  153. protected function getLocalTestOrder($status, array $items)
  154. {
  155. $this->orderProducts = array_keys($items);
  156. $order = $this->getTestOrder();
  157. if (!is_null($status)) {
  158. $order->setStatus($status);
  159. }
  160. $order->setPaymentMethod(\XLite\Core\Database::getRepo('XLite\Model\Payment\Method')->find(3));
  161. foreach ($order->getItems() as $index => $item) {
  162. if (isset($items[$item->getSku()])) {
  163. $item->setAmount($items[$item->getSku()]);
  164. }
  165. }
  166. $order->calculate();
  167. \XLite\Core\Database::getEM()->flush();
  168. \XLite\Core\Database::getEM()->clear();
  169. return $order;
  170. }
  171. /**
  172. * Wrapper for the REPO findBestsellers method
  173. *
  174. * @param int $count ____param_comment____
  175. * @param int $cat ____param_comment____
  176. *
  177. * @return void
  178. * @access protected
  179. * @see ____func_see____
  180. * @since 1.0.0
  181. */
  182. protected function findBestsellers($count = 0, $cat = 0)
  183. {
  184. return \XLite\Core\Database::getRepo('XLite\Model\Product')->findBestsellers($count, $cat);
  185. }
  186. }