PageRenderTime 144ms CodeModel.GetById 70ms app.highlight 12ms RepoModel.GetById 58ms app.codeStats 1ms

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