Completed
Push — master ( 3df790...9b1caf )
by Bukashk0zzz
01:16
created

OfferCustomElementsGeneratorTest::testGenerate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Bukashk0zzzYmlGenerator
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Bukashk0zzz\YmlGenerator\Tests;
11
12
use Bukashk0zzz\YmlGenerator\Model\Offer\OfferSimple;
13
use Bukashk0zzz\YmlGenerator\Cdata;
14
15
/**
16
 * Generator test
17
 */
18
class OfferCustomElementsGeneratorTest extends AbstractGeneratorTest
19
{
20
    const CDATA_TEST_STRING = '<p>Simple HTML</p></description></offer><![CDATA[';
21
    const OFFER_COUNT = 2;
22
23
    /**
24
     * Test generate
25
     */
26
    public function testGenerate()
27
    {
28
        // Don't call $this->validateFileWithDtd() here because custom elements are not included into the default DTD
29
        $this->generateFile();
30
        $this->checkCustomElements();
31
    }
32
33
    /**
34
     * Need to override parent::createOffers() in order to avoid setting description
35
     * after calling self::createOffer()
36
     *
37
     * {@inheritDoc}
38
     * @see \Bukashk0zzz\YmlGenerator\Tests\AbstractGeneratorTest::createOffers()
39
     */
40 View Code Duplication
    protected function createOffers()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        $offers = [];
43
        foreach (\range(1, self::OFFER_COUNT) as $id) {
44
            $offers[] =
45
                $this->createOffer()
46
                    ->setId($id)
47
                    ->setCategoryId($id)
48
                ;
49
        }
50
51
        return $offers;
52
    }
53
54
    /**
55
     * Set the test description with CDATA here
56
     *
57
     * {@inheritDoc}
58
     * @see \Bukashk0zzz\YmlGenerator\Tests\AbstractGeneratorTest::createOffer()
59
     */
60
    protected function createOffer()
61
    {
62
        $offer = (new OfferSimple())
63
            ->setAvailable($this->faker->boolean)
64
            ->setUrl($this->faker->url)
65
            ->setPrice($this->faker->numberBetween(1, 9999))
66
            ->setOldPrice($this->faker->numberBetween(1, 9999))
67
            ->setWeight($this->faker->numberBetween(1, 9999))
68
            ->setCurrencyId('UAH')
69
            ->setDelivery($this->faker->boolean)
70
            ->setLocalDeliveryCost($this->faker->numberBetween(1, 9999))
71
            ->setSalesNotes($this->faker->text(45))
72
            ->setManufacturerWarranty($this->faker->boolean)
73
            ->setCountryOfOrigin('Украина')
74
            ->setDownloadable($this->faker->boolean)
75
            ->setAdult($this->faker->boolean)
76
            ->setMarketCategory($this->faker->word)
77
            ->setCpa($this->faker->numberBetween(0, 1))
78
            ->setBarcodes([$this->faker->ean13, $this->faker->ean13])
79
80
            ->setName($this->faker->name)
81
            ->setVendor($this->faker->company)
82
            ->setDescription($this->faker->sentence)
83
            ->setVendorCode(null)
84
            ->setPickup(true)
85
            ->setGroupId($this->faker->numberBetween())
86
            ->addPicture('http://example.com/example.jpeg')
87
            ->addBarcode($this->faker->ean13)
88
89
            ->setCustomElements(['custom_element' => [100500, 'string value']])
90
            ->addCustomElement('custom_element', true)
91
            ->addCustomElement('custom_element', false)
92
            ->addCustomElement('custom_element', null) // Should not be written
93
            ->addCustomElement('custom_element', $cdata = new Cdata(self::CDATA_TEST_STRING))
94
            ->addCustomElement('stock_quantity', 100) // https://rozetka.com.ua/sellerinfo/pricelist/
95
        ;
96
97
        $this->assertSame([100500, 'string value', true, false, $cdata], $offer->getCustomElementByType('custom_element'));
98
        $this->assertSame([100], $offer->getCustomElementByType('stock_quantity'));
99
        $this->assertSame([], $offer->getCustomElementByType('non_existent_element'));
100
101
        return $offer;
102
    }
103
104
    /**
105
     * Load generated XML file and check custom elements
106
     */
107
    private function checkCustomElements()
108
    {
109
        // Much easier to test with SimpleXML tahn with DOM
110
        $yml = \simplexml_load_file($this->settings->getOutputFile());
111
112
        $offers = $yml->shop->offers->offer;
113
        $this->assertNotEmpty($offers);
114
        $this->assertEquals(self::OFFER_COUNT, count($offers));
115
116
        foreach ($offers as $offer) {
117
            $prop = 'stock_quantity';
118
            $this->assertSame(100, (int) $offer->$prop); // Can't use $offer->stock_quantity because of CS rules
119
120
            $prop = 'custom_element';
121
            $multipleElements = $offer->$prop; // Can't use $offer->custom_element because of CS rules
122
            $this->assertNotEmpty($multipleElements);
123
124
            // Verity each added value
125
            $this->assertSame(100500, (int) $multipleElements[0]);
126
            $this->assertSame('string value', (string) $multipleElements[1]);
127
            $this->assertSame('true', (string) $multipleElements[2]);
128
            $this->assertSame('false', (string) $multipleElements[3]);
129
130
            // ->addCustomElement('custom_element', null) must not produce an element
131
132
            $this->assertSame(self::CDATA_TEST_STRING, (string) $multipleElements[4]);
133
        }
134
    }
135
136
    /**
137
     * Create instance of Cdata class with a predefined test string
138
     *
139
     * @return \Bukashk0zzz\YmlGenerator\Cdata
140
     */
141
    private function makeDescription()
142
    {
143
        return new Cdata(self::CDATA_TEST_STRING);
144
    }
145
}
146