Completed
Pull Request — master (#22)
by
unknown
01:13
created

OfferCustomElementsGeneratorTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 122
Duplicated Lines 10.66 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 6
dl 13
loc 122
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testGenerate() 0 6 1
A createOffers() 13 13 2
A createOffer() 0 37 1
A checkCustomElements() 0 28 2
A makeDescription() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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