Completed
Push — master ( b1eb14...1aaaec )
by Grégoire
12s queued 10s
created

testLoadTwigStringExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\NewsBundle\Tests\DependencyInjection;
15
16
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
17
use Sonata\ClassificationBundle\Model\Collection;
18
use Sonata\ClassificationBundle\Model\Tag;
19
use Sonata\EasyExtendsBundle\Mapper\DoctrineCollector;
20
use Sonata\MediaBundle\Model\Media;
21
use Sonata\NewsBundle\DependencyInjection\SonataNewsExtension;
22
use Sonata\NewsBundle\Model\Comment;
23
use Sonata\NewsBundle\Model\Post;
24
use Sonata\NewsBundle\Tests\Fixtures\UserMock;
25
use Twig\Extra\String\StringExtension;
26
27
class SonataNewsExtensionTest extends AbstractExtensionTestCase
28
{
29
    protected function setUp(): void
30
    {
31
        parent::setUp();
32
        $this->container->setParameter('kernel.bundles', []);
33
    }
34
35
    /**
36
     * Test if the deprecation notice is triggered when the tag (or collection) class declaration is missing.
37
     * It should trigger a deprecation notice but doesn't break anything
38
     * You should have 0 association.
39
     *
40
     * @group legacy
41
     * @expectedDeprecation The %s class is not defined or does not exist. This is tolerated now but will be forbidden in 4.0
42
     */
43
    public function testLoadWithoutTagWithoutCollection(): void
44
    {
45
        $this->load($this->getMinimalConfiguration());
46
        $collector = DoctrineCollector::getInstance();
47
48
        $this->assertEmpty($collector->getAssociations(), 'Our models should have 0 association');
49
    }
50
51
    /**
52
     * Test if the deprecation notice is triggered when the tag (or collection) class declaration is present.
53
     * It shouldn't trigger a deprecation notice but doesn't break anything
54
     * You should have 2 associations (Post, Comment).
55
     * The Post model should have an association with (Media, User, Collection, Tag).
56
     */
57
    public function testLoadWithTagWithCollection(): void
58
    {
59
        $this->load($this->getConfigurationWithTagWithCollection());
60
        $collector = DoctrineCollector::getInstance();
61
        $this->assertCount(2, $collector->getAssociations(), 'Our models should have 2 associations (post, comment)');
62
        $postManyToOneAssociation = $collector->getAssociations()[Post::class]['mapManyToOne'];
63
        $this->assertCount(3, $postManyToOneAssociation, 'The post model should have 3 many to one associations (user, media, collection)');
64
        $postManyToManyAssociation = $collector->getAssociations()[Post::class]['mapManyToMany'];
65
        $this->assertCount(1, $postManyToManyAssociation, 'The post model should have 1 many to many association (tag)');
66
        $postOneToManyAssociation = $collector->getAssociations()[Post::class]['mapOneToMany'];
67
        $this->assertCount(1, $postOneToManyAssociation, 'The post model should have 1 one to many association (comment)');
68
    }
69
70
    public function testLoadTwigStringExtension(): void
71
    {
72
        $this->load($this->getConfigurationWithTagWithCollection());
73
74
        $this->assertContainerBuilderHasServiceDefinitionWithTag(StringExtension::class, 'twig.extension');
75
    }
76
77
    protected function getMinimalConfiguration(): array
78
    {
79
        return [
80
                'title' => 'Foo title',
81
                'link' => '/foo/bar',
82
                'description' => 'Foo description',
83
                'salt' => 'pepper',
84
                'comment' => [
85
                    'notification' => [
86
                        'emails' => ['[email protected]', '[email protected]'],
87
                        'from' => '[email protected]',
88
                        'template' => '@SonataNews/Mail/comment_notification.txt.twig',
89
                    ],
90
                ],
91
                'class' => [
92
                    'post' => Post::class,
93
                    'comment' => Comment::class,
94
                    'media' => Media::class,
95
                    'user' => UserMock::class,
96
                ],
97
            ];
98
    }
99
100
    protected function getConfigurationWithTagWithCollection(): array
101
    {
102
        $minimalConfiguration = $this->getMinimalConfiguration();
103
        $tagAndCollectionDeclaration = [
104
                'class' => [
105
                        'collection' => Collection::class,
106
                        'tag' => Tag::class,
107
                ],
108
            ];
109
110
        return array_merge($minimalConfiguration, $tagAndCollectionDeclaration);
111
    }
112
113
    protected function getContainerExtensions(): array
114
    {
115
        return [
116
            new SonataNewsExtension(),
117
        ];
118
    }
119
}
120