Completed
Push — master ( 6ae4bd...3df790 )
by Bukashk0zzz
01:13
created

OfferCdataGeneratorTest::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 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
     * @see \Bukashk0zzz\YmlGenerator\Tests\AbstractGeneratorTest::createOffers()
39
     */
40
    protected function createOffers()
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->makeDescription())
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
    }
90
91
    /**
92
     * Retreive and check CDATA from the generated file
93
     */
94
    private function checkCdata()
95
    {
96
        $ymlFile = new \DOMDocument();
97
        $ymlFile->loadXML(\file_get_contents($this->settings->getOutputFile()));
98
99
        $xpath = new \DOMXPath($ymlFile);
100
        $descriptionNodes = $xpath->query('//yml_catalog/shop/offers/offer/description');
101
        self::assertNotFalse($descriptionNodes);
102
103
        // One description per offer is expected
104
        self::assertEquals(self::OFFER_COUNT, $descriptionNodes->length);
105
106
        foreach ($descriptionNodes as $descriptionNode) {
107
            $description = $descriptionNode->nodeValue;
108
109
            self::assertSame(self::CDATA_TEST_STRING, $description);
110
        }
111
    }
112
113
    /**
114
     * Create instance of Cdata class with a predefined test string
115
     *
116
     * @return \Bukashk0zzz\YmlGenerator\Cdata
117
     */
118
    private function makeDescription()
119
    {
120
        return new Cdata(self::CDATA_TEST_STRING);
121
    }
122
}
123