Completed
Push — master ( 0f7b01...b73efb )
by Bukashk0zzz
02:11
created

AbstractGeneratorTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Bukashk0zzzYmlGenerator
5
 *
6
 * (c) Denis Golubovskiy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bukashk0zzz\YmlGenerator\Tests;
13
14
use Faker\Factory as Faker;
15
use Bukashk0zzz\YmlGenerator\Model\Category;
16
use Bukashk0zzz\YmlGenerator\Model\Currency;
17
use Bukashk0zzz\YmlGenerator\Model\ShopInfo;
18
use Bukashk0zzz\YmlGenerator\Settings;
19
use Bukashk0zzz\YmlGenerator\Generator;
20
21
/**
22
 * Abstract Generator test
23
 *
24
 * @author Denis Golubovskiy <[email protected]>
25
 */
26
abstract class AbstractGeneratorTest extends \PHPUnit_Framework_TestCase
27
{
28
    /**
29
     * @var \Faker\Generator
30
     */
31
    protected $faker;
32
33
    /**
34
     * @var Settings
35
     */
36
    protected $settings;
37
38
    /**
39
     * @var ShopInfo
40
     */
41
    protected $shopInfo;
42
43
    /**
44
     * @var array
45
     */
46
    protected $currencies;
47
48
    /**
49
     * @var array
50
     */
51
    protected $categories;
52
53
    /**
54
     * @var string
55
     */
56
    protected $offerType;
57
58
    /**
59
     * @return array
60
     */
61
    abstract protected function createOffers();
62
63
    /**
64
     * Test setup
65
     */
66
    public function setUp()
67
    {
68
        $this->faker = Faker::create();
69
70
        $this->settings = $this->createSettings();
71
        $this->shopInfo = $this->createShopInfo();
72
        $this->currencies = $this->createCurrencies();
73
        $this->categories = $this->createCategories();
74
    }
75
76
    /**
77
     * Test generation
78
     */
79
    protected function runGeneratorTest()
80
    {
81
        static::assertTrue((new Generator($this->settings))->generate(
82
            $this->shopInfo,
83
            $this->currencies,
84
            $this->categories,
85
            $this->createOffers()
86
        ));
87
88
        $this->validateFileWithDtd();
89
    }
90
91
    /**
92
     * Validate yml file using dtd
93
     */
94
    private function validateFileWithDtd()
95
    {
96
        $systemId = 'data://text/plain;base64,'.base64_encode(file_get_contents(__DIR__.'/dtd/'.$this->offerType.'.dtd'));
97
        $root = 'yml_catalog';
98
99
        $ymlFile = new \DOMDocument();
100
        $ymlFile->loadXML(file_get_contents($this->settings->getOutputFile()));
101
102
        $creator = new \DOMImplementation();
103
        $ymlFileWithDtd = $creator->createDocument(null, null, $creator->createDocumentType($root, null, $systemId));
104
        $ymlFileWithDtd->encoding = 'utf-8';
105
106
        $oldNode = $ymlFile->getElementsByTagName($root)->item(0);
107
        $newNode = $ymlFileWithDtd->importNode($oldNode, true);
108
        $ymlFileWithDtd->appendChild($newNode);
109
110
        try {
111
            static::assertTrue($ymlFileWithDtd->validate());
112
        } catch (\Exception $exception) {
113
            echo $exception->getMessage();
114
            static::fail('YML file not valid');
115
        }
116
    }
117
118
    /**
119
     * @return Settings
120
     */
121
    private function createSettings()
122
    {
123
        return (new Settings())
124
            ->setOutputFile(tempnam(sys_get_temp_dir(), 'YMLGeneratorTest'))
125
            ->setEncoding('utf-8')
126
            ->setIndentString("\t");
127
    }
128
129
    /**
130
     * @return ShopInfo
131
     */
132
    private function createShopInfo()
133
    {
134
        return (new ShopInfo())
135
            ->setName($this->faker->name)
136
            ->setCompany($this->faker->company)
137
            ->setUrl($this->faker->url)
138
            ->setPlatform($this->faker->name)
139
            ->setVersion($this->faker->numberBetween(1, 999))
140
            ->setAgency($this->faker->name)
141
            ->setEmail($this->faker->email)
142
        ;
143
    }
144
145
    /**
146
     * @return array
147
     */
148
    private function createCurrencies()
149
    {
150
        $currencies = [];
151
        $currencies[] = (new Currency())
152
            ->setId('UAH')
153
            ->setRate(1)
154
        ;
155
156
        return $currencies;
157
    }
158
159
    /**
160
     * @return array
161
     */
162
    private function createCategories()
163
    {
164
        $categories = [];
165
        $categories[] = (new Category())
166
            ->setId(1)
167
            ->setName($this->faker->name)
168
        ;
169
170
        $categories[] = (new Category())
171
            ->setId(2)
172
            ->setParentId(1)
173
            ->setName($this->faker->name)
174
        ;
175
176
        return $categories;
177
    }
178
}
179