1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2015-2017 Libre Informatique |
6
|
|
|
* |
7
|
|
|
* This file is licenced under the GNU LGPL v3. |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Blast\Bundle\CoreBundle\Tests\Unit\Generator; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use org\bovigo\vfs\vfsStream; |
16
|
|
|
use Blast\Bundle\CoreBundle\Generator\BlastGenerator; |
17
|
|
|
|
18
|
|
View Code Duplication |
class BlastGeneratorTest extends TestCase |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var BlastGenerator |
22
|
|
|
*/ |
23
|
|
|
protected $object; |
24
|
|
|
|
25
|
|
|
private $blastFile; |
26
|
|
|
private $skeletonDirectory; |
27
|
|
|
private $modelManager; |
28
|
|
|
|
29
|
|
|
private $root; |
30
|
|
|
private $file; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @todo check if it is pertinent to cover __construct and do it(or not) in setUp |
34
|
|
|
* @covers \Blast\Bundle\CoreBundle\Generator\BlastGenerator::__construct |
35
|
|
|
*/ |
36
|
|
|
protected function setUp() |
37
|
|
|
{ |
38
|
|
|
// As blast.yml is modified by the test |
39
|
|
|
$this->root = vfsStream::setup('BlastTestRessources'); |
40
|
|
|
$this->file = vfsStream::newFile('blast.yml'); |
41
|
|
|
$this->root->addChild($this->file); |
42
|
|
|
|
43
|
|
|
/* |
44
|
|
|
* @todo test with and/or without original content |
45
|
|
|
*/ |
46
|
|
|
$this->file->setContent(file_get_contents('src/Resources/config/blast.yml')); |
47
|
|
|
|
48
|
|
|
$this->blastFile = vfsStream::url('BlastTestRessources/blast.yml'); |
49
|
|
|
|
50
|
|
|
/* |
51
|
|
|
* @todo check if it should be tested with other skeleton |
52
|
|
|
*/ |
53
|
|
|
|
54
|
|
|
$this->skeletonDirectory = 'src/Resources/skeleton'; |
55
|
|
|
|
56
|
|
|
// Sonata Model Manager is used to launch getExportFields |
57
|
|
|
// from the method addResource in Blast\Bundle\CoreBundle\Generator\BlastGenerator |
58
|
|
|
// $managerType = 'sonata.admin.manager.orm'; |
|
|
|
|
59
|
|
|
$modelManagerMock = $this->getMockForAbstractClass('Sonata\AdminBundle\Model\ModelManagerInterface'); |
60
|
|
|
|
61
|
|
|
$map = [ |
62
|
|
|
['Model', ['foo', 'bar', 'not_an_id']], |
63
|
|
|
['Ledom', ['id', 'zoo', 'rab']], |
64
|
|
|
]; |
65
|
|
|
$modelManagerMock |
66
|
|
|
->expects($this->any()) |
67
|
|
|
->method('getExportFields') |
68
|
|
|
->will($this->returnValueMap($map)); |
69
|
|
|
|
70
|
|
|
$this->modelManager = $modelManagerMock; |
71
|
|
|
$this->object = new BlastGenerator( |
72
|
|
|
$this->blastFile, |
73
|
|
|
$this->modelManager, |
74
|
|
|
$this->skeletonDirectory |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function tearDown() |
79
|
|
|
{ |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @covers \Blast\Bundle\CoreBundle\Generator\BlastGenerator::addResource |
84
|
|
|
*/ |
85
|
|
|
public function testAddResource() |
86
|
|
|
{ |
87
|
|
|
$this->assertFileExists($this->blastFile); |
88
|
|
|
|
89
|
|
|
$this->object->AddResource('Model'); |
90
|
|
|
$this->object->AddResource('Ledom'); |
91
|
|
|
|
92
|
|
|
$content = $this->file->getContent(); |
93
|
|
|
|
94
|
|
|
// Model |
95
|
|
|
$this->assertContains('blast', $content); |
96
|
|
|
$this->assertContains('foo: ~', $content); |
97
|
|
|
$this->assertContains('Model:', $content); |
98
|
|
|
|
99
|
|
|
// Ledom |
100
|
|
|
$this->assertContains('Ledom:', $content); |
101
|
|
|
|
102
|
|
|
/* |
103
|
|
|
* @todo should be test for '#id: ~' when content is generated by sequence like ORM\GeneratedValue(strategy="AUTO") |
104
|
|
|
*/ |
105
|
|
|
$this->assertContains('id: ~', $content); |
106
|
|
|
|
107
|
|
|
// echo $this->file->getContent(); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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.