Passed
Push — master ( 1ac8b7...8e16ff )
by Yo!
13:35 queued 10:17
created

ContentManagerTest   C

Complexity

Total Complexity 18

Size/Duplication

Total Lines 246
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 19

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 18
c 4
b 1
f 1
lcom 1
cbo 19
dl 0
loc 246
ccs 156
cts 156
cp 1
rs 6.875

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testParseSite() 0 59 3
A testParseDraft() 0 12 1
B testRelationships() 0 46 3
A testMultipleExtension() 0 16 1
A getContentManager() 0 15 1
A getCollectionManager() 0 16 1
A getConverterManager() 0 8 1
A getGeneratorManager() 0 9 1
A getFilesystemDataSourceManager() 0 16 1
A getMemoryDataSourceManager() 0 12 1
A getRenderizer() 0 7 1
A getPluginManager() 0 11 2
1
<?php
2
3
/*
4
 * This file is part of the Yosymfony\Spress.
5
 *
6
 * (c) YoSymfony <http://github.com/yosymfony>
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 Yosymfony\Spress\Core\Tests\ContentManager;
13
14
use Symfony\Component\EventDispatcher\EventDispatcher;
15
use Yosymfony\Spress\Core\ContentManager\ContentManager;
16
use Yosymfony\Spress\Core\ContentManager\Generator\GeneratorManager;
17
use Yosymfony\Spress\Core\ContentManager\Generator\Pagination\PaginationGenerator;
18
use Yosymfony\Spress\Core\ContentManager\Collection\CollectionManagerBuilder;
19
use Yosymfony\Spress\Core\ContentManager\Converter\ConverterManager;
20
use Yosymfony\Spress\Core\ContentManager\Converter\MichelfMarkdownConverter;
21
use Yosymfony\Spress\Core\ContentManager\Converter\MapConverter;
22
use Yosymfony\Spress\Core\ContentManager\Permalink\PermalinkGenerator;
23
use Yosymfony\Spress\Core\ContentManager\Renderizer\TwigRenderizer;
24
use Yosymfony\Spress\Core\ContentManager\SiteAttribute\SiteAttribute;
25
use Yosymfony\Spress\Core\DataSource\DataSourceManagerBuilder;
26
use Yosymfony\Spress\Core\DataSource\Item;
27
use Yosymfony\Spress\Core\DataWriter\MemoryDataWriter;
28
use Yosymfony\Spress\Core\IO\NullIO;
29
use Yosymfony\Spress\Core\Plugin\PluginManager;
30
use Yosymfony\Spress\Core\Tester\PluginTester;
31
32
class ContentManagerTest extends \PHPUnit_Framework_TestCase
33
{
34 4
    public function setUp()
35
    {
36 4
        \Twig_Autoloader::register();
37 4
    }
38
39 1
    public function testParseSite()
40
    {
41
        $attributes = [
42 1
            'site_name' => 'My tests site',
43 1
        ];
44
45
        $spressAttributes = [
46 1
            'version' => '2.0.0',
47 1
            'version_id' => '20000',
48 1
            'major_version' => '2',
49 1
            'minor_version' => '0',
50 1
            'release_version' => '0',
51 1
            'extra_version' => 'dev',
52 1
        ];
53
54 1
        $dw = new MemoryDataWriter();
55
56 1
        $testPlugin = new PluginTester('acme');
57
        $testPlugin->setListenerToBeforeRenderPageEvent(function ($event) {
58 1
            if ($event->getId() === 'about/index.html') {
59 1
                $this->assertStringEndsNotWith('</html>', $event->getContent());
60 1
            }
61 1
        });
62
        $testPlugin->setListenerToAfterRenderPageEvent(function ($event) {
63 1
            if ($event->getId() === 'about/index.html') {
64 1
                $this->assertStringEndsWith('</html>', $event->getContent());
65 1
            }
66 1
        });
67
68 1
        $cm = $this->getContentManager($this->getFilesystemDataSourceManager(), $dw, [$testPlugin]);
69 1
        $cm->parseSite($attributes, $spressAttributes);
70
71 1
        $this->assertCount(14, $dw->getItems());
72
73 1
        $this->assertTrue($dw->hasItem('about/index.html'));
74 1
        $this->assertTrue($dw->hasItem('about/me/index.html'));
75 1
        $this->assertTrue($dw->hasItem('index.html'));
76 1
        $this->assertTrue($dw->hasItem('LICENSE'));
77 1
        $this->assertTrue($dw->hasItem('category-1/category-2/2020/01/01/new-post-example/index.html'));
78 1
        $this->assertTrue($dw->hasItem('2013/08/12/post-example-2/index.html'));
79 1
        $this->assertTrue($dw->hasItem('books/2013/08/11/best-book/index.html'));
80 1
        $this->assertTrue($dw->hasItem('projects/index.html'));
81 1
        $this->assertTrue($dw->hasItem('robots.txt'));
82 1
        $this->assertTrue($dw->hasItem('sitemap.xml'));
83 1
        $this->assertTrue($dw->hasItem('pages/index.html'));
84 1
        $this->assertTrue($dw->hasItem('pages/page2/index.html'));
85 1
        $this->assertTrue($dw->hasItem('pages/page3/index.html'));
86 1
        $this->assertTrue($dw->hasItem('pages/page4/index.html'));
87
88 1
        $this->assertContains('<!DOCTYPE HTML>', $dw->getItem('about/index.html')->getContent());
89 1
        $this->assertContains('<!DOCTYPE HTML>', $dw->getItem('pages/index.html')->getContent());
90 1
        $this->assertContains('<!DOCTYPE HTML>', $dw->getItem('pages/page2/index.html')->getContent());
91
92 1
        $attributes = $dw->getItem('books/2013/08/11/best-book/index.html')->getAttributes();
93 1
        $this->assertArrayHasKey('author', $attributes);
94 1
        $this->assertArrayHasKey('categories', $attributes);
95 1
        $this->assertContains('books', $attributes['categories']);
96 1
        $this->assertEquals('Yo! Symfony', $attributes['author']);
97 1
    }
98
99 2
    public function testParseDraft()
100
    {
101 1
        $dw = new MemoryDataWriter();
102 1
        $cm = $this->getContentManager($this->getFilesystemDataSourceManager(), $dw);
103 1
        $cm->parseSite([], [], true);
104
105 1
        $this->assertCount(15, $dw->getItems());
106
107 2
        $this->assertTrue($dw->hasItem('books/2013/09/19/new-book/index.html'));
108
109 1
        $this->assertContains('<!DOCTYPE HTML>', $dw->getItem('books/2013/09/19/new-book/index.html')->getContent());
110 1
    }
111
112 2
    public function testRelationships()
113
    {
114 1
        $dw = new MemoryDataWriter();
115
116 1
        $testPlugin = new PluginTester('acme');
117
        $testPlugin->setListenerToBeforeRenderPageEvent(function ($event) {
118 2
            if ($event->getId() === 'about/index.html') {
119 2
                $this->assertStringEndsNotWith('</html>', $event->getContent());
120 2
            }
121 1
        });
122 1
        $testPlugin->setListenerToAfterRenderPageEvent(function ($event) {
123 1
            if ($event->getId() === 'about/index.html') {
124 1
                $this->assertStringEndsWith('</html>', $event->getContent());
125 1
            }
126 1
        });
127
128 1
        $cm = $this->getContentManager($this->getFilesystemDataSourceManager(), $dw, [$testPlugin]);
129 1
        $cm->parseSite([], [], true);
130
131 1
        $item = $dw->getItem('category-1/category-2/2020/01/01/new-post-example/index.html');
132
133 1
        $relationshipCollection = $item->getRelationshipCollection();
134
135 1
        $this->assertEquals(1, $relationshipCollection->count());
136 1
        $this->assertEquals(1, count($relationshipCollection->get('next')));
137
138 1
        $nextItem = current($relationshipCollection->get('next'));
139
140 2
        $this->assertEquals('posts/books/2013-09-19-new-book.md', $nextItem->getId());
141
142 1
        $item = $dw->getItem('books/2013/09/19/new-book/index.html');
143
144 1
        $relationshipCollection = $item->getRelationshipCollection();
145
146 1
        $this->assertEquals(2, $relationshipCollection->count());
147 1
        $this->assertEquals(1, count($relationshipCollection->get('prior')));
148 1
        $this->assertEquals(1, count($relationshipCollection->get('next')));
149
150 1
        $priorItem = current($relationshipCollection->get('prior'));
151
152 1
        $this->assertEquals('posts/2013-08-12-post-example-1.md', $priorItem->getId());
153
154 1
        $nextItem = current($relationshipCollection->get('next'));
155
156 1
        $this->assertEquals('posts/2013-08-12-post-example-2.mkd', $nextItem->getId());
157 1
    }
158
159 1
    public function testMultipleExtension()
160
    {
161 1
        $dw = new MemoryDataWriter();
162 1
        $dsm = $this->getMemoryDataSourceManager();
163 1
        $cm = $this->getContentManager($dsm, $dw);
164
165 1
        $item = new Item('', 'about.html.twig');
166 1
        $item->setPath('about.html.twig', Item::SNAPSHOT_PATH_RELATIVE);
167
168 1
        $memoryDataSource = $dsm->getDataSource('memory');
169 1
        $memoryDataSource->addItem($item);
170
171 1
        $cm->parseSite([], [], true);
172
173 1
        $item = $dw->getItem('about/index.html');
0 ignored issues
show
Unused Code introduced by
$item is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
174 1
    }
175
176 4
    protected function getContentManager($dataSourceManager, $dataWriter, array $plugins = [])
177
    {
178 4
        $dsm = $dataSourceManager;
179 4
        $gm = $this->getGeneratorManager();
180 4
        $cm = $this->getConverterManager();
181 4
        $com = $this->getCollectionManager();
182 4
        $pg = new PermalinkGenerator('pretty');
183 4
        $renderizer = $this->getRenderizer();
184 4
        $siteAttribute = new SiteAttribute();
185 4
        $dispatcher = new EventDispatcher();
186 4
        $pm = $this->getPluginManager($dispatcher, $plugins);
187 4
        $io = new NullIO();
188
189 4
        return new ContentManager($dsm, $dataWriter, $gm, $cm, $com, $pg, $renderizer, $siteAttribute, $pm, $dispatcher, $io);
190
    }
191
192 4
    protected function getCollectionManager()
193
    {
194
        $config = [
195
            'posts' => [
196 4
                'output' => true,
197 4
                'author' => 'Yo! Symfony',
198 4
                'sort_by' => 'date',
199 4
                'sort_type' => 'descending',
200 4
            ],
201 4
        ];
202
203 4
        $builder = new CollectionManagerBuilder();
204 4
        $cm = $builder->buildFromConfigArray($config);
205
206 4
        return $cm;
207
    }
208
209 4
    protected function getConverterManager()
210
    {
211 4
        $cm = new ConverterManager();
212 4
        $cm->addConverter(new MapConverter(['html.twig' => 'html']));
213 4
        $cm->addConverter(new MichelfMarkdownConverter(['markdown', 'mkd', 'mkdn', 'md']));
214
215 4
        return $cm;
216
    }
217
218 4
    protected function getGeneratorManager()
219
    {
220 4
        $generator = new PaginationGenerator();
221
222 4
        $gm = new GeneratorManager();
223 4
        $gm->addGenerator('pagination', $generator);
224
225 4
        return $gm;
226
    }
227
228 3
    protected function getFilesystemDataSourceManager()
229
    {
230
        $config = [
231
            'filesystem' => [
232 3
                'class' => 'Yosymfony\Spress\Core\DataSource\Filesystem\FilesystemDataSource',
233
                'arguments' => [
234 3
                    'source_root' => __dir__.'/../fixtures/project/src',
235 3
                    'text_extensions' => ['htm', 'html', 'html.twig', 'twig.html', 'js', 'less', 'markdown', 'md', 'mkd', 'mkdn', 'coffee', 'css', 'txt', 'xhtml', 'xml'],
236 3
                ],
237 3
            ],
238 3
        ];
239
240 3
        $builder = new DataSourceManagerBuilder();
241
242 3
        return $builder->buildFromConfigArray($config);
243
    }
244
245 1
    protected function getMemoryDataSourceManager()
246
    {
247
        $config = [
248
            'memory' => [
249 1
                'class' => 'Yosymfony\Spress\Core\DataSource\Memory\MemoryDataSource',
250 1
            ],
251 1
        ];
252
253 1
        $builder = new DataSourceManagerBuilder();
254
255 1
        return $builder->buildFromConfigArray($config);
256
    }
257
258 4
    protected function getRenderizer()
259
    {
260 4
        $twigLoader = new \Twig_Loader_Array([]);
261 4
        $twig = new \Twig_Environment($twigLoader, ['autoescape' => false]);
262
263 4
        return new TwigRenderizer($twig, $twigLoader, ['twig', 'html.twig', 'twig.html', 'html']);
264
    }
265
266 4
    protected function getPluginManager(EventDispatcher $dispatcher, array $plugins)
267
    {
268 4
        $pm = new PluginManager($dispatcher);
269 4
        $pluginCollection = $pm->getPluginCollection();
270
271 4
        foreach ($plugins as $index => $Plugin) {
272 2
            $pluginCollection->add($index, $Plugin);
273 4
        }
274
275 4
        return $pm;
276
    }
277
}
278