PageRenderTime 255ms CodeModel.GetById 60ms app.highlight 123ms RepoModel.GetById 59ms app.codeStats 0ms

/.dev/tests/Classes/Model/OrderItem.php

https://github.com/istran/core
PHP | 379 lines | 246 code | 107 blank | 26 comment | 0 complexity | 13f5ea88a95c0265f780527287ee4ae2 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: cfe87d80112366f9ee65bd28ab91b52277f16bf6 $
 24 * @link       http://www.litecommerce.com/
 25 * @see        ____file_see____
 26 * @since      3.0.0
 27 */
 28
 29class XLite_Tests_Model_OrderItem extends XLite_Tests_Model_OrderAbstract
 30{
 31    public function testCreate()
 32    {
 33        $order = $this->getTestOrder();
 34
 35        $this->assertEquals(1, $order->getItems()->count(), 'check items count');
 36
 37        $item = $order->getItems()->get(0);
 38
 39        $this->assertTrue(0 < $item->getItemId(), 'check items id');
 40
 41        $this->assertEquals(
 42            $this->getProduct()->getProductId(),
 43            $item->getObject()->getId(),
 44            'check product id'
 45        );
 46
 47        $this->assertEquals(
 48            $this->getProduct()->getName(),
 49            $item->getName(),
 50            'check name'
 51        );
 52
 53        $this->assertEquals(
 54            $this->getProduct()->getSKU(),
 55            $item->getSKU(),
 56            'check sku'
 57        );
 58
 59        $this->assertEquals(
 60            $this->getProduct()->getPrice(),
 61            $item->getPrice(),
 62            'check price'
 63        );
 64
 65        $this->assertEquals(
 66            1,
 67            $item->getAmount(),
 68            'check amount'
 69        );
 70
 71        // Saved modifiers
 72        $sm = new \XLite\Model\OrderItemModifier;
 73        $item->addSavedModifiers($sm);
 74        $count = count($item->getSavedModifiers());
 75        $this->assertEquals($sm, $item->getSavedModifiers()->get($count - 1), 'check saved modifier');
 76
 77    }
 78
 79    public function testUpdate()
 80    {
 81        $order = $this->getTestOrder();
 82
 83        $item = $order->getItems()->get(0);
 84
 85        $id = $item->getItemId();
 86
 87        $item->setAmount(2);
 88
 89        \XLite\Core\Database::getEM()->persist($item);
 90        \XLite\Core\Database::getEM()->flush();
 91
 92        \XLite\Core\Database::getEM()->clear();
 93
 94        $item = \XLite\Core\Database::getRepo('XLite\Model\OrderItem')->find($id);
 95
 96        $this->assertEquals(2, $item->getAmount(), 'check amount');
 97
 98        $list = \XLite\Core\Database::getRepo('XLite\Model\Product')->findByEnabled(true);
 99        $p = $list[1];
100
101        $item->setProduct($p);
102
103        \XLite\Core\Database::getEM()->persist($item);
104        \XLite\Core\Database::getEM()->flush();
105
106        \XLite\Core\Database::getEM()->clear();
107
108        $item = \XLite\Core\Database::getRepo('XLite\Model\OrderItem')->find($id);
109
110        $this->assertEquals($p->getProductId(), $item->getObject()->getId(), 'check object id');
111    }
112
113    public function testDelete()
114    {
115        $order = $this->getTestOrder();
116
117        $item = $order->getItems()->get(0);
118
119        $order->getItems()->removeElement($item);
120        \XLite\Core\Database::getEM()->remove($item);
121
122        $id = $item->getItemId();
123
124        \XLite\Core\Database::getEM()->flush();
125
126        $item = \XLite\Core\Database::getRepo('XLite\Model\OrderItem')
127            ->find($id);
128
129        $this->assertTrue(is_null($item), 'check removed item');
130    }
131
132    public function testGetProduct()
133    {
134        $order = $this->getTestOrder();
135
136        $item = $order->getItems()->get(0);
137
138        $id = $item->getItemId();
139
140        $this->assertEquals($this->getProduct(), $item->getProduct(), 'check object');
141
142        $item = new \XLite\Model\OrderItem();
143
144        $this->assertTrue(is_null($item->getProduct()), 'check empty object #2');
145
146        \XLite\Core\Database::getEM()->clear();
147
148        $item = \XLite\Core\Database::getRepo('XLite\Model\OrderItem')->find($id);
149
150        $this->assertEquals($this->getProduct(), $item->getProduct(), 'check object #2');
151
152        $item = \XLite\Core\Database::getRepo('XLite\Model\OrderItem')->find($id);
153
154        \XLite\Core\Database::getEM()->remove($this->getProduct());
155        \XLite\Core\Database::getEM()->flush();
156
157        \XLite\Core\Database::getEM()->clear();
158
159        $item = \XLite\Core\Database::getRepo('XLite\Model\OrderItem')->find($id);
160
161        $this->assertFalse(is_null($item), 'check item from DB');
162        $this->assertFalse(is_null($item->getProduct()), 'check dump object #2');
163    }
164
165    public function testsetObject()
166    {
167        $order = $this->getTestOrder();
168        $item = $order->getItems()->get(0);
169
170        $this->assertNotEquals(0, $item->getPrice(), 'check non-empty price');
171        $this->assertNotEquals('', $item->getName(), 'check non-empty name');
172        $this->assertNotEquals('', $item->getSku(), 'check non-empty sku');
173
174        $item->setProduct(null);
175
176        $this->assertEquals(0, $item->getPrice(), 'check empty price');
177        $this->assertEquals('', $item->getName(), 'check empty name');
178        $this->assertEquals('', $item->getSku(), 'check empty sku');
179    }
180
181    public function testSetAmount()
182    {
183        $order = $this->getTestOrder();
184
185        $item = $order->getItems()->get(0);
186
187        $this->assertEquals(1, $item->getAmount(), 'check amount');
188
189        $id = $item->getItemId();
190
191        $item->setAmount(2);
192
193        \XLite\Core\Database::getEM()->persist($item);
194        \XLite\Core\Database::getEM()->flush();
195
196        \XLite\Core\Database::getEM()->clear();
197
198        $item = \XLite\Core\Database::getRepo('XLite\Model\OrderItem')->find($id);
199
200        $this->assertEquals(2, $item->getAmount(), 'check amount #2');
201
202        $item->setAmount(0);
203
204        $this->assertEquals(1, $item->getAmount(), 'check amount #3');
205
206        $item->setAmount(99999);
207
208        $this->assertEquals(9999, $item->getAmount(), 'check amount #4');
209    }
210
211    public function testGetWeight()
212    {
213        $order = $this->getTestOrder();
214
215        $item = $order->getItems()->get(0);
216
217        $this->assertEquals($this->getProduct()->getWeight(), $item->getWeight(), 'check weight');
218
219        $item->setAmount(2);
220
221        $this->assertEquals(2 * $this->getProduct()->getWeight(), $item->getWeight(), 'check weight #2');
222
223        $item = new \XLite\Model\OrderItem();
224
225        $this->assertEquals(0, $item->getWeight(), 'check weight #3');
226    }
227
228    public function testHasImage()
229    {
230        $order = $this->getTestOrder();
231
232        $item = $order->getItems()->get(0);
233
234        $this->assertEquals(
235            (bool)$this->getProduct()->getImage(),
236            $item->hasImage(),
237            'check thumbnail'
238        );
239    }
240
241    public function testGetImageURL()
242    {
243        $order = $this->getTestOrder();
244
245        $item = $order->getItems()->get(0);
246
247        $this->assertEquals(
248            $this->getProduct()->getImage()->getURL(),
249            $item->getImageURL(),
250            'check thumbnail url'
251        );
252
253    }
254
255    public function testGetImage()
256    {
257        $order = $this->getTestOrder();
258
259        $item = $order->getItems()->get(0);
260
261        $this->assertEquals(
262            $this->getProduct()->getImage(),
263            $item->getImage(),
264            'check thumbnail'
265        );
266    }
267
268    public function testGetDescription()
269    {
270        $order = $this->getTestOrder();
271
272        $item = $order->getItems()->get(0);
273
274        $this->assertEquals(
275            $this->getProduct()->getName() . ' (1)',
276            $item->getDescription(),
277            'check description'
278        );
279    }
280
281    public function testGetURL()
282    {
283        $order = $this->getTestOrder();
284
285        $item = $order->getItems()->get(0);
286
287        $this->assertEquals(
288            $this->getProduct()->getURL(),
289            $item->getURL(),
290            'check object page URL'
291        );
292    }
293
294    public function testIsShipped()
295    {
296        $order = $this->getTestOrder();
297
298        $item = $order->getItems()->get(0);
299
300        $this->assertEquals(
301            !$this->getProduct()->getFreeShipping(),
302            $item->isShipped(),
303            'check shipped status'
304        );
305    }
306
307    public function testGetKey()
308    {
309        $order = $this->getTestOrder();
310
311        $item = $order->getItems()->get(0);
312
313        $this->assertEquals(
314            'product.' . $this->getProduct()->getProductId(),
315            $item->getKey(),
316            'check key'
317        );
318    }
319
320    public function testIsValid()
321    {
322        $order = $this->getTestOrder();
323
324        $item = $order->getItems()->get(0);
325
326        $this->assertTrue(
327            $item->isValid(),
328            'check validate status'
329        );
330    }
331
332    public function testGetDiscountablePrice()
333    {
334        $order = $this->getTestOrder();
335
336        $item = $order->getItems()->get(0);
337
338        $this->assertEquals(
339            $item->getDiscountablePrice(),
340            $item->getPrice(),
341            'check discountable price'
342        );
343    }
344
345    public function testGettaxableTotal()
346    {
347        $order = $this->getTestOrder();
348
349        $item = $order->getItems()->get(0);
350
351        $this->assertEquals(
352            $item->getTaxableTotal(),
353            $item->getTotal(),
354            'check taxable total'
355        );
356    }
357
358    public function testGetEventCell()
359    {
360        $order = $this->getTestOrder();
361
362        $item = $order->getItems()->get(0);
363
364        $etalon = array(
365            'item_id'     => $item->getItemId(),
366            'key'         => 'product.' . $this->getProduct()->getProductId(),
367            'object_type' => 'product',
368            'object_id'   => $this->getProduct()->getProductId(),
369            'options'     => array(),
370        );
371
372        $this->assertEquals(
373            $etalon,
374            $item->getEventCell(),
375            'check event item info'
376        );
377
378    }
379}