core /.dev/tests/Classes/Model/Product.php

Language PHP Lines 300
MD5 Hash c2a30835b21fc9f7cd7c050660067ee1 Estimated Cost $4,281 (why?)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
// vim: set ts=4 sw=4 sts=4 et:

/**
 * XLite\Model\Repo\Product class tests
 *
 * @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: 6e146348d3aad089510b094d831fe1e247427dd5 $
 * @link       http://www.litecommerce.com/
 * @see        ____file_see____
 * @since      3.0.0
 */

include_once __DIR__ . '/AProduct.php';

class XLite_Tests_Model_Product extends XLite_Tests_Model_AProduct
{
	/**
	 * getProductData 
	 * 
	 * @return void
	 * @access protected
	 * @see    ____func_see____
	 * @since  3.0.0
	 */
	protected function getProductData()
	{
		return array(
			'price'         => array(4.99, null),
			'sale_price'    => array(3.54, null),
			'sku'           => array('test_sku', null),
			'enabled'       => array(true, null),
			'weight'        => array(2.88, null),
			'tax_class'     => array('test_class', null),
			'free_shipping' => array(true, null),
			'clean_url'     => array('test_url', null),
			'javascript'    => array('test_js', null),
            'name'          => array('test name', null),
            'description'   => array('test description', null),
            'brief_description' => array('test brief description', null),
            'meta_tags'     => array('test meta tags', null),
            'meta_desc'     => array('test meta description', null),
            'meta_title'    => array('test meta title', null),
		);
	}

	/**
	 * testConstruct 
	 * 
	 * @return void
	 * @access public
	 * @see    ____func_see____
	 * @since  3.0.0
	 */
	public function testConstruct()
	{
		$dataToSet = array();
		$dataToCheck = array();

		foreach ($this->getProductData() as $key => $data) {
			list($actual, $expected) = $data;
			$dataToSet[$key] = $actual;
			$dataToCheck[$key] = isset($expected) ? $expected : $actual;
		}

		$entity = new \XLite\Model\Product($dataToSet);

		foreach ($dataToCheck as $key => $value) {
			$this->assertEquals(
                $value,
                $entity->{'get' . \XLite\Core\Converter::convertToCamelCase($key)}(),
                'Field "' . $key . '" does not match'
            );
		}

        // Order item
        $i = new \XLite\Model\OrderItem;
        $entity->addOrderItems($i);
        $this->assertEquals($i, $entity->getOrderItems()->get(0), 'check order item');

        // Image
        $i = new \XLite\Model\Image\Product\Image;
        $entity->addImages($i);
        $this->assertEquals($i, $entity->getImages()->get(0), 'check image');

	}

	/**
	 * testAddCategoryProducts 
	 * 
	 * @return void
	 * @access public
	 * @see    ____func_see____
	 * @since  3.0.0
	 */
	public function testAddCategoryProducts()
	{
        $c = \XLite\Core\Database::getRepo('XLite\Model\Category')->find(14015);
		$p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(15090);

        $cp = new \XLite\Model\CategoryProducts();
        $cp->setCategory($c);
        $cp->setProduct($p);

		$result = $cp;

		// Check keys
        $this->assertNotNull($result->getCategory(), 'Invalid category');
        $this->assertNotNull($result->getProduct(), 'Invalid product');
		$this->assertEquals(14015, $result->getCategory()->getCategoryId(), 'Invalid category ID');
		$this->assertEquals(15090, $result->getProduct()->getProductId(), 'Invalid product ID');
	}

	/**
	 * testAddOptionGroups 
	 * 
	 * @return void
	 * @access public
	 * @see    ____func_see____
	 * @since  3.0.0
	 */
	public function testAddOptionGroups()
    {
        $entity = new \XLite\Model\Product();

        $entity->addOptionGroups(
            new XLite\Module\CDev\ProductOptions\Model\OptionGroup(
                array(
					'type' => XLite\Module\CDev\ProductOptions\Model\OptionGroup::GROUP_TYPE,
					'view_type' => XLite\Module\CDev\ProductOptions\Model\OptionGroup::SELECT_VISIBLE,
				)
            )
        );

        $result = array_pop($entity->getOptionGroups()->toArray());

        // Check keys
        $this->assertEquals(XLite\Module\CDev\ProductOptions\Model\OptionGroup::GROUP_TYPE, $result->getType(), 'Invalid group type');
        $this->assertEquals(XLite\Module\CDev\ProductOptions\Model\OptionGroup::SELECT_VISIBLE, $result->getViewType(), 'Invalid view type');
    }

    /**
     * testAddTranslations 
     * 
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  3.0.0
     */
    public function testAddTranslations()
    {
        $entity = new \XLite\Model\Product();

        $entity->addTranslations(new \XLite\Model\ProductTranslation(array('name' => 'test')));

        $result = array_pop($entity->getTranslations()->toArray());

        // Check keys
        $this->assertEquals('test', $result->getName(), 'Invalid product name');
    }

    /**
     * testGetProductId 
     * 
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  3.0.0
     */
	public function testGetProductId()
    {
        $result = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(15090);

        // Check entity
        $this->assertNotNull($result, 'Product not found');
        $this->assertEquals(15090, $result->getProductId(), 'Product ID does not match');
    }

    public function testisAvailable()
    {
        $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(16281);

        $p->setEnabled(true);
        $this->assertTrue($p->isAvailable(), 'check enabled');

        $p->setEnabled(false);
        $this->assertTrue($p->isAvailable(), 'check disabled (is admin zone)');
    }

    public function testgetTaxedPrice()
    {
        $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(16281);

        $this->assertEquals($p->getPrice(), $p->getTaxedPrice(), 'check taxed price (equals price)');
    }

    public function testgetListPrice()
    {
        $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(16281);

        $this->assertEquals($p->getTaxedPrice(), $p->getListPrice(), 'check taxed price (equals taxed price)');
    }

    public function testhasImage()
    {
        $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(15090);

        $this->assertTrue($p->hasImage(), 'check image');

        $p->getImages()->clear();

        $this->assertFalse($p->hasImage(), 'check empty image');
    }

    public function testgetImageURL()
    {
        $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(15090);

        $this->assertRegExp('/images.product.demo_p15090\.jpeg$/Ss', $p->getImageURL(), 'check image URL');

        $p->getImages()->clear();

        $this->assertNull($p->getImageURL(), 'check empty image URL');
    }

    public function testgetCategory()
    {
        $c = \XLite\Core\Database::getRepo('XLite\Model\Category')->find(14015);
        $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(16281);

        $cp = new \XLite\Model\CategoryProducts();
        $cp->setCategory($c);
        $cp->setProduct($p);

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

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

        $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(16281);

        $cp = $p->getCategory(14015);
        $this->assertTrue($cp instanceof \XLite\Model\Category, 'check class');
        $this->assertEquals(14015, $cp->getCategoryId(), 'check category id');

        $cp = $p->getCategory(999999999);
        $this->assertTrue($cp instanceof \XLite\Model\Category, 'check class #2');
        $this->assertTrue(is_null($cp->getCategoryId()), 'check category is null');

        $cp = $p->getCategory();
        $this->assertTrue($cp instanceof \XLite\Model\Category, 'check class #3');
        $this->assertEquals(14015, $cp->getCategoryId(), 'check category id #3');
    }

    public function testgetOrderBy()
    {
        $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(16281);

        $this->assertEquals(10, $p->getOrderBy(14009), 'check order by of exist link');
        $this->assertEquals(0, $p->getOrderBy(999999999), 'check order by of NON exist link');
    }

    public function testcountImages()
    {
        $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(15090);

        $this->assertEquals(1, $p->countImages(), 'check 1 images');

        $p->getImages()->clear();

        $this->assertEquals(0, $p->countImages(), 'check zero images');
    }

    public function testgetCommonDescription()
    {
        $p = \XLite\Core\Database::getRepo('XLite\Model\Product')->find(16282);

        $this->assertEquals(
            '<h5>Cucumber</h5>
<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>
<p>&nbsp;</p>
<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>',
            $p->getCommonDescription(),
            'check as description'
        );

        $p->setBriefDescription('test brief description');
        $this->assertEquals(
            'test brief description',
            $p->getCommonDescription(),
            'check as description'
        );
    }
}
Back to Top