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

Language PHP Lines 229
MD5 Hash 60b9ca208cfe5f8b267b29bec1c47848
Repository https://github.com/istran/core.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
// vim: set ts=4 sw=4 sts=4 et:

/**
 * LiteCommerce
 * 
 * NOTICE OF LICENSE
 * 
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to licensing@litecommerce.com so we can send you a copy immediately.
 * 
 * @category   LiteCommerce
 * @package    Tests
 * @subpackage Classes
 * @author     Creative Development LLC <info@cdev.ru> 
 * @copyright  Copyright (c) 2010 Creative Development LLC <info@cdev.ru>. All rights reserved
 * @license    http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 * @version    GIT: $Id: 007df39432351ca5b6b52cb01a68b1754633b606 $
 * @link       http://www.litecommerce.com/
 * @see        ____file_see____
 * @since      3.0.0
 */

class XLite_Tests_Module_CDev_Bestsellers_Model_Repo_Product extends XLite_Tests_Model_OrderAbstract
{

    /**
     *  Product id constants
     */
    const PR1 = '00002';
    const PR2 = '00041';
    const PR3 = '00043';
    const PR4 = '00047';

    /**
     *  Category id for the PR1 product
     */
    const CATEGORY = 'apparel';


    /**
     * First test sequence of the bestsellers
     * 
     * @var    array
     * @access protected
     * @see    ____var_see____
     * @since  1.0.0
     */
    protected $test1 = array(
        0 => self::PR1,
        1 => self::PR2,
        2 => self::PR4,
    );

    /**
     * Second test sequence of the bestsellers
     * 
     * @var    array
     * @access protected
     * @see    ____var_see____
     * @since  1.0.0
     */
    protected $test2 = array(
        0 => self::PR1,
        1 => self::PR3,
        2 => self::PR2,
    );

    /**
     *  Test of bestseller for the root category
     * 
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  1.0.0
     */
    public function testFindBestsellersRoot()
    {
        /**
         * First order goes with processed status 
         */
        $order = $this->getLocalTestOrder(
            \XLite\Model\Order::STATUS_PROCESSED,
            array(
                self::PR1 => 500,
                self::PR2 => 40,
                self::PR4 => 30,
            )
        );

        $best = $this->findBestsellers(3);

        $this->assertEquals(count($this->test1), count($best), 'Wrong number of bestsellers was returned. (1)');

        /**
         * First sequence 
         */
        foreach ($this->test1 as $index => $id) {

            $this->assertTrue(isset($best[$index]), 'Not set #' . $index . ' product in bestsellers (1)');

            $this->assertEquals($best[$index]->getSku(), $id, 'Wrong #' . $index . ' product in bestsellers (1)');

        }

        /**
         * Second order goes with completed status 
         */
        $order = $this->getLocalTestOrder(
            \XLite\Model\Order::STATUS_COMPLETED,
            array(
                self::PR3 => 45,
            )
        );

        $best = $this->findBestsellers(3);

        $this->assertEquals(count($this->test2), count($best), 'Wrong number of bestsellers was returned. (2)');

        /**
         * Second sequence  
         */
        foreach ($this->test2 as $index => $id) {

            $this->assertTrue(isset($best[$index]), 'Not set #' . $index . ' product in bestsellers (2)');

            $this->assertEquals($best[$index]->getSku(), $id, 'Wrong #' . $index . ' product in bestsellers (2)');

        }

    }

    /**
     * Test of bestseller in some non-root category
     * 
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  1.0.0
     */
    public function testFindBestsellersCategory()
    {
        $order = $this->getLocalTestOrder(
            \XLite\Model\Order::STATUS_COMPLETED,
            array(
                self::PR1 => 500,
                self::PR2 => 400,
                self::PR4 => 300,
            )   
        );  

        $c = \XLite\Core\Database::getRepo('XLite\Model\Category')->findOneBy(array('cleanUrl' => self::CATEGORY));
        $best = $this->findBestsellers(1, $c->getCategoryId());

        $this->assertEquals(1, count($best), 'Wrong number of bestsellers was returned (1)');

        $one = $best[0];

        $this->assertEquals(self::PR1, $one->getSku(), 'Wrong root category bestsellers list');
    }


    /**
     *  FOR INNER USE ONLY
     */

    /**
     * Prepare order
     * 
     * @param mixed $status ____param_comment____
     * @param array $items  ____param_comment____
     *  
     * @return void
     * @access protected
     * @see    ____func_see____
     * @since  1.0.0
     */
    protected function getLocalTestOrder($status, array $items)
    {
        $this->orderProducts = array_keys($items);

        $order = $this->getTestOrder();

        if (!is_null($status)) {
            $order->setStatus($status);
        }   

        $order->setPaymentMethod(\XLite\Core\Database::getRepo('XLite\Model\Payment\Method')->find(3));

        foreach ($order->getItems() as $index => $item) {

            if (isset($items[$item->getSku()])) {
                $item->setAmount($items[$item->getSku()]);
            }
        }

        $order->calculate();

        \XLite\Core\Database::getEM()->flush();

        \XLite\Core\Database::getEM()->clear();

        return $order;
    }

    /**
     *  Wrapper for the REPO findBestsellers method
     * 
     * @param int $count ____param_comment____
     * @param int $cat   ____param_comment____
     *  
     * @return void
     * @access protected
     * @see    ____func_see____
     * @since  1.0.0
     */
    protected function findBestsellers($count = 0, $cat = 0)
    {   
        return \XLite\Core\Database::getRepo('XLite\Model\Product')->findBestsellers($count, $cat);
    }   


}
Back to Top