OfferCdataGeneratorTest::checkCdata()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
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\Cdata;
13
use Bukashk0zzz\YmlGenerator\Model\Offer\OfferSimple;
14
15
/**
16
 * Generator test
17
 */
18
class OfferCdataGeneratorTest 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
        $this->offerType = 'Simple';
29
        $this->runGeneratorTest();
30
        $this->checkCdata();
31
    }
32
33
    /**
34
     * Need to override parent::createOffers() in order to avoid setting description
35
     * after calling self::createOffer()
36
     *
37
     * {@inheritdoc}
38
     *
39
     * @see \Bukashk0zzz\YmlGenerator\Tests\AbstractGeneratorTest::createOffers()
40
     */
41 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...
42
    {
43
        $offers = [];
44
        foreach (\range(1, self::OFFER_COUNT) as $id) {
45
            $offers[] =
46
                $this->createOffer()
47
                    ->setId($id)
48
                    ->setCategoryId($id)
49
                ;
50
        }
51
52
        return $offers;
53
    }
54
55
    /**
56
     * Set the test description with CDATA here
57
     *
58
     * {@inheritdoc}
59
     *
60
     * @see \Bukashk0zzz\YmlGenerator\Tests\AbstractGeneratorTest::createOffer()
61
     */
62
    protected function createOffer()
63
    {
64
        return (new OfferSimple())
65
            ->setAvailable($this->faker->boolean)
66
            ->setUrl($this->faker->url)
67
            ->setPrice($this->faker->numberBetween(1, 9999))
68
            ->setOldPrice($this->faker->numberBetween(1, 9999))
69
            ->setWeight($this->faker->numberBetween(1, 9999))
70
            ->setCurrencyId('UAH')
71
            ->setDelivery($this->faker->boolean)
72
            ->setLocalDeliveryCost($this->faker->numberBetween(1, 9999))
73
            ->setSalesNotes($this->faker->text(45))
74
            ->setManufacturerWarranty($this->faker->boolean)
75
            ->setCountryOfOrigin('Украина')
76
            ->setDownloadable($this->faker->boolean)
77
            ->setAdult($this->faker->boolean)
78
            ->setMarketCategory($this->faker->word)
79
            ->setCpa($this->faker->numberBetween(0, 1))
80
            ->setBarcodes([$this->faker->ean13, $this->faker->ean13])
81
82
            ->setName($this->faker->name)
83
            ->setVendor($this->faker->company)
84
            ->setDescription($this->makeDescription())
85
            ->setVendorCode(null)
86
            ->setPickup(true)
87
            ->setGroupId($this->faker->numberBetween())
88
            ->addPicture('http://example.com/example.jpeg')
89
            ->addBarcode($this->faker->ean13)
90
        ;
91
    }
92
93
    /**
94
     * Retreive and check CDATA from the generated file
95
     */
96
    private function checkCdata()
97
    {
98
        $ymlFile = new \DOMDocument();
99
        $ymlFile->loadXML(\file_get_contents($this->settings->getOutputFile()));
100
101
        $xpath = new \DOMXPath($ymlFile);
102
        $descriptionNodes = $xpath->query('//yml_catalog/shop/offers/offer/description');
103
        self::assertNotFalse($descriptionNodes);
104
105
        // One description per offer is expected
106
        self::assertEquals(self::OFFER_COUNT, $descriptionNodes->length);
107
108
        foreach ($descriptionNodes as $descriptionNode) {
109
            $description = $descriptionNode->nodeValue;
110
111
            self::assertSame(self::CDATA_TEST_STRING, $description);
112
        }
113
    }
114
115
    /**
116
     * Create instance of Cdata class with a predefined test string
117
     *
118
     * @return \Bukashk0zzz\YmlGenerator\Cdata
119
     */
120
    private function makeDescription()
121
    {
122
        return new Cdata(self::CDATA_TEST_STRING);
123
    }
124
}
125