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