Completed
Pull Request — master (#22)
by
unknown
07:29
created

makeDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
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
        return (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
            ->addCustomElement('stock_quantity', 100) // https://rozetka.com.ua/sellerinfo/pricelist/
90
            ->addCustomElement('custom_element', 100500)
91
            ->addCustomElement('custom_element', 'string value')
92
            ->addCustomElement('custom_element', true)
93
            ->addCustomElement('custom_element', false)
94
            ->addCustomElement('custom_element', null) // Should not be written
95
            ->addCustomElement('custom_element', new Cdata(self::CDATA_TEST_STRING))
96
        ;
97
    }
98
99
    /**
100
     * Load generated XML file and check custom elements
101
     */
102
    private function checkCustomElements()
103
    {
104
        // Much easier to test with SimpleXML tahn with DOM
105
        $yml = \simplexml_load_file($this->settings->getOutputFile());
106
107
        $offers = $yml->shop->offers->offer;
108
        $this->assertNotEmpty($offers);
109
        $this->assertEquals(self::OFFER_COUNT, count($offers));
110
111
        foreach ($offers as $offer) {
112
            $prop = 'stock_quantity';
113
            $this->assertSame(100, (int) $offer->$prop); // Can't use $offer->stock_quantity because of CS rules
114
115
            $prop = 'custom_element';
116
            $multipleElements = $offer->$prop; // Can't use $offer->custom_element because of CS rules
117
            $this->assertNotEmpty($multipleElements);
118
119
            // Verity each added value
120
            $this->assertSame(100500, (int) $multipleElements[0]);
121
            $this->assertSame('string value', (string) $multipleElements[1]);
122
            $this->assertSame('true', (string) $multipleElements[2]);
123
            $this->assertSame('false', (string) $multipleElements[3]);
124
125
            // ->addCustomElement('custom_element', null) must not produce an element
126
127
            $this->assertSame(self::CDATA_TEST_STRING, (string) $multipleElements[4]);
128
        }
129
    }
130
131
    /**
132
     * Create instance of Cdata class with a predefined test string
133
     *
134
     * @return \Bukashk0zzz\YmlGenerator\Cdata
135
     */
136
    private function makeDescription()
137
    {
138
        return new Cdata(self::CDATA_TEST_STRING);
139
    }
140
}
141