Completed
Pull Request — develop (#640)
by Narcotic
08:00 queued 01:02
created

ResourceGeneratorTest::testAddXmlParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
/**
3
 * test resource generator
4
 */
5
6
namespace Graviton\GeneratorBundle\Generator;
7
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Graviton;
10
use Graviton\TestBundle\Test\GravitonTestCase;
11
12
/**
13
 * Test the ResourceGenerator
14
 *
15
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license  https://opensource.org/licenses/MIT MIT License
17
 * @link     http://swisscom.ch
18
 */
19
class ResourceGeneratorTest extends GravitonTestCase
20
{
21
    /**
22
     * Dir to put the generated files in
23
     *
24
     * @var string
25
     */
26
    const GRAVITON_TMP_DIR = "/tmp/generateDocumentTest";
27
28
    /**
29
     * (non-PHPdoc)
30
     *
31
     * @see PHPUnit_Framework_TestCase::setUp()
32
     *
33
     * @return void
34
     */
35
    public function setUp()
36
    {
37
        // Make sure the temp dir exists
38
        $target = self::GRAVITON_TMP_DIR . "/Resources/config/";
39
40
        if (!is_dir($target)) {
41
            mkdir($target, 0777, true);
42
        }
43
    }
44
45
    /**
46
     * (non-PHPdoc)
47
     *
48
     * @see PHPUnit_Framework_TestCase::tearDown()
49
     *
50
     * @return void
51
     */
52
    public function tearDown()
53
    {
54
        $target = self::GRAVITON_TMP_DIR;
55
56
        if (!is_dir($target)) {
57
            rmdir($target);
58
        }
59
    }
60
61
    /**
62
     * Verify addParam
63
     *
64
     * @return void
65
     */
66
    public function testAddParam()
67
    {
68
        $xml = '<container>' .
69
            '<parameters>' .
70
            '<parameter key="graviton.test.class">GravitonDyn\TestBundle\Test</parameter>' .
71
            '</parameters>' .
72
            '</container>';
73
74
        $dom = new \DOMDocument();
75
        $dom->loadXML('<container/>');
76
        $paramKey = 'graviton.test.class';
77
        $value = 'GravitonDyn\TestBundle\Test';
78
79
        $generator = $this->createMock('\Graviton\GeneratorBundle\Generator\ResourceGenerator');
80
        $protectedMethod = $this->getPrivateClassMethod($generator, 'addParam');
81
82
        $newDom = $protectedMethod->invokeArgs($generator, [$dom, $paramKey, $value]);
83
84
        $this->assertXmlStringEqualsXmlString($xml, $newDom->saveXML());
85
    }
86
87
    /**
88
     * Verify addParam
89
     *
90
     * @return void
91
     */
92
    public function testAddParamDuplicateKey()
93
    {
94
        $xml = '<container>' .
95
            '<parameters>' .
96
            '<parameter key="graviton.test.class">GravitonDyn\TestBundle\Test</parameter>' .
97
            '</parameters>' .
98
            '</container>';
99
100
        $dom = new \DOMDocument();
101
        $dom->loadXML('<container/>');
102
        $paramKey = 'graviton.test.class';
103
        $value = 'GravitonDyn\TestBundle\Test';
104
105
        $generator = $this->createMock('\Graviton\GeneratorBundle\Generator\ResourceGenerator');
106
        $protectedMethod = $this->getPrivateClassMethod($generator, 'addParam');
107
108
        $dom = $protectedMethod->invokeArgs($generator, [$dom, $paramKey, $value]);
109
        $dom = $protectedMethod->invokeArgs($generator, [$dom, $paramKey, $value]);
110
111
        $this->assertXmlStringEqualsXmlString($xml, $dom->saveXML());
112
    }
113
114
    /**
115
     * Validates behavior of addRolesParameter
116
     *
117
     * @return void
118
     */
119
    public function testAddCollectionParam()
120
    {
121
        $xml = '<container>' .
122
            '<parameters>' .
123
            '<parameter key="graviton.test.roles"  type="collection">' .
124
            '<parameter>GRAVITON_USER</parameter>' .
125
            '<parameter>GRAVITON_ADMIN</parameter>' .
126
            '</parameter>' .
127
            '</parameters>' .
128
            '</container>';
129
130
        $dom = new \DOMDocument();
131
        $dom->loadXML('<container/>');
132
        $key = 'graviton.test.roles';
133
        $roles = array('GRAVITON_USER', 'GRAVITON_ADMIN');
134
135
        $generator = $this->createMock('\Graviton\GeneratorBundle\Generator\ResourceGenerator');
136
        $protectedMethod = $this->getPrivateClassMethod($generator, 'addCollectionParam');
137
138
        $protectedMethod->invokeArgs($generator, [$dom, $key, $roles]);
139
        $this->assertXmlStringEqualsXmlString($xml, $dom->saveXML());
140
    }
141
142
    /**
143
     * @return void
144
     */
145
    public function testLoadServices()
146
    {
147
        // generate dummy services.xml
148
        file_put_contents(
149
            self::GRAVITON_TMP_DIR . '/Resources/config/services.xml',
150
            '<?xml version="1.0"?><container/>'
151
        );
152
153
        $generator = $this->createMock('\Graviton\GeneratorBundle\Generator\ResourceGenerator');
154
        $protectedMethod = $this->getPrivateClassMethod($generator, 'loadServices');
155
156
        $services = $protectedMethod->invokeArgs($generator, [self::GRAVITON_TMP_DIR]);
157
158
        $this->assertInstanceOf('\DomDocument', $services);
159
        $this->assertSame($services, $protectedMethod->invokeArgs($generator, [self::GRAVITON_TMP_DIR]));
160
    }
161
162
    /**
163
     * @return void
164
     */
165
    public function testAddXmlParameter()
166
    {
167
        $value = 'the fox jumps over the lazy dog.';
168
        $key = 'some_document.class';
169
        $type = 'string';
170
171
        $element = array(
172
            'content' => $value,
173
            'key' => $key,
174
            'type' => strtolower($type),
175
        );
176
177
        $generator = $this->createMock('\Graviton\GeneratorBundle\Generator\ResourceGenerator');
178
        $protectedMethod = $this->getPrivateClassMethod($generator, 'addXmlParameter');
179
        $protectedProp = $this->getPrivateClassProperty($generator, 'xmlParameters');
180
181
        $protectedProp->setValue($generator, new ArrayCollection());
182
        $protectedMethod->invokeArgs($generator, [$value, $key, $type]);
183
184
        $this->assertTrue($protectedProp->getValue($generator)->contains($element));
185
    }
186
187
    /**
188
     * @return void
189
     */
190
    public function testGenerateParameters()
191
    {
192
        // generate dummy services.xml
193
        file_put_contents(
194
            self::GRAVITON_TMP_DIR . '/Resources/config/services.xml',
195
            '<?xml version="1.0"?><container/>'
196
        );
197
198
        $xml = '<container>' .
199
            '<parameters>' .
200
            '<parameter key="graviton.test.parameter">some ext</parameter>' .
201
            '<parameter key="graviton.test.collection" type="collection">' .
202
            '<parameter>item1</parameter>' .
203
            '<parameter>item2</parameter>' .
204
            '</parameter>' .
205
            '</parameters>' .
206
            '</container>';
207
208
        $parameters = array(
209
            array(
210
                'content' => 'some ext',
211
                'key' => 'graviton.test.parameter',
212
                'type' => 'string',
213
            ),
214
            array(
215
                'content' => array('item1', 'item2'),
216
                'key' => 'graviton.test.collection',
217
                'type' => 'collection',
218
            ),
219
        );
220
221
        $generator = $this->createMock('\Graviton\GeneratorBundle\Generator\ResourceGenerator');
222
        $protectedGenParameters = $this->getPrivateClassMethod($generator, 'generateParameters');
223
        $protectedLoadServices = $this->getPrivateClassMethod($generator, 'loadServices');
224
        $protectedProp = $this->getPrivateClassProperty($generator, 'xmlParameters');
225
226
        $protectedProp->setValue($generator, new ArrayCollection($parameters));
227
        $protectedGenParameters->invokeArgs($generator, [self::GRAVITON_TMP_DIR]);
228
        $services = $protectedLoadServices->invokeArgs($generator, [self::GRAVITON_TMP_DIR]);
229
230
        $this->assertXmlStringEqualsXmlString($xml, $services->saveXML());
231
    }
232
233
    /**
234
     * Test the generateDocument method (without repository creation)
235
     *
236
     * @dataProvider baseNameProvider
237
     *
238
     * @param string $base Basepath
239
     *
240
     * @return void
241
     */
242
    public function testGenerateDocument($base)
243
    {
244
        $servicesMock = $this->getMockBuilder('\DOMDocument')
245
            ->setMethods(['saveXml', 'getElementsByTagName'])
246
            ->getMock();
247
248
        $jsonDefinitionMock = $this->getMockBuilder('\Graviton\GeneratorBundle\Definition\JsonDefinition')
249
            ->disableOriginalConstructor()
250
            ->getMock();
251
252
        $parameters = array(
253
            'base' => $base,
254
            'document' => 'DocumentTest',
255
            'bundle' => 'MyTestBundle',
256
            'json' => $jsonDefinitionMock
257
        );
258
259
        $dir = self::GRAVITON_TMP_DIR;
260
        $document = 'DocumentTest';
261
262
        $generator = $this->getMockBuilder('\Graviton\GeneratorBundle\Tests\Generator\ResourceGeneratorProxy')
263
            ->disableOriginalConstructor()
264
            ->setMethods(array('renderFile', 'loadServices', 'addXmlParameter', 'addService'))
265
            ->getMock();
266
267
        $generator
268
            ->expects($this->exactly(7))
269
            ->method('renderFile');
270
271
        $generator
272
            ->expects($this->exactly(2))
273
            ->method('loadServices')
274
            ->will($this->returnValue($servicesMock));
275
276
        $generator
277
            ->expects($this->exactly(2))
278
            ->method('addXmlParameter');
279
280
        $containerNodeMock = $this->getMockBuilder('\DOMNode')
281
            ->setMethods(['item', 'appendChild'])
282
            ->getMock();
283
284
        $servicesMock
285
            ->expects($this->any())
286
            ->method('getElementsByTagName')
287
            ->willReturn($containerNodeMock);
288
289
        $containerNodeMock->length = 0;
290
        $containerNodeMock
291
            ->expects($this->exactly(2))
292
            ->method('item')
293
            ->with(0)
294
            ->willReturn($containerNodeMock);
295
296
        $generator
297
            ->expects($this->exactly(3))
298
            ->method('addService')
299
            ->with(
300
                $this->equalTo($servicesMock)//,
301
                //$this->equalTo($docName)
302
            )
303
            ->will($this->returnValue($servicesMock));
304
305
        $generator->generateDocument($parameters, $dir, $document);
306
    }
307
308
    /**
309
     * Return the base names the test should cover
310
     *
311
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[][].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
312
     */
313
    public function baseNameProvider()
314
    {
315
        return array(
316
            array('Graviton\BundleNameBundle\\'),
317
            array('Graviton\BundleName\\'),
318
            array('Graviton\BundleNamebundle\\'),
319
        );
320
    }
321
}
322