Completed
Push — master ( 68f942...d69058 )
by Rafał
18:18
created

nPHPCRDisabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher MultiTenancy Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2016 Sourcefabric z.ú.
12
 * @license http://www.superdesk.org/license
13
 */
14
namespace SWP\Bundle\MultiTenancyBundle\Tests\DependencyInjection;
15
16
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
17
use SWP\Bundle\MultiTenancyBundle\DependencyInjection\SWPMultiTenancyExtension;
18
use SWP\Bundle\MultiTenancyBundle\Doctrine\PHPCR\TenantRepository;
19
use SWP\Component\MultiTenancy\Factory\TenantFactory;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
22
23
class SWPMultiTenancyExtensionTest extends AbstractExtensionTestCase
24
{
25
    /**
26
     * @covers SWP\Bundle\MultiTenancyBundle\SWPMultiTenancyBundle
27
     * @covers SWP\Bundle\MultiTenancyBundle\DependencyInjection\SWPMultiTenancyExtension::load
28
     * @covers SWP\Bundle\MultiTenancyBundle\DependencyInjection\Configuration::getConfigTreeBuilder
29
     * @covers SWP\Bundle\MultiTenancyBundle\DependencyInjection\SWPMultiTenancyExtension::loadPhpcr
30
     */
31
    public function testLoad()
32
    {
33
        $container = $this->createContainer();
34
        $loader = $this->createLoader();
35
        $config = $this->getConfig();
36
37
        $loader->load([$config], $container);
38
39
        $this->assertTrue($container->getParameter('swp_multi_tenancy.backend_type_phpcr'));
40
        $this->assertEquals('/swp', $container->getParameter('swp_multi_tenancy.persistence.phpcr.basepath'));
41
42
        $this->assertEquals('content', $container->getParameter('swp_multi_tenancy.persistence.phpcr.content_basepath'));
43
        $this->assertEquals(
44
            'SWP\Bundle\MultiTenancyBundle\Routing\TenantAwareRouter',
45
            $container->getParameter('swp_multi_tenancy.persistence.phpcr.router.class')
46
        );
47
48
        $this->assertEquals(
49
            ['routes1', 'routes2'],
50
            $container->getParameter('swp_multi_tenancy.persistence.phpcr.route_basepaths')
51
        );
52
53
        $this->assertEquals(
54
            ['routes1', 'routes2', 'content', 'menu', 'media'],
55
            $container->getParameter('swp_multi_tenancy.persistence.phpcr.base_paths')
56
        );
57
58
        $this->assertEquals(
59
            'SWP\Component\MultiTenancy\Model\Tenant',
60
            $container->getParameter('swp.model.tenant.class')
61
        );
62
63
        $this->assertEquals(
64
            'SWP\Component\MultiTenancy\Factory\TenantFactory',
65
            $container->getParameter('swp.factory.tenant.class')
66
        );
67
68
        $this->assertTrue($container->hasParameter('swp_multi_tenancy.backend_type_phpcr'));
69
    }
70
71
    protected function createLoader()
72
    {
73
        return new SWPMultiTenancyExtension();
74
    }
75
76
    protected function getConfig()
77
    {
78
        return [
79
            'persistence' => [
80
                'phpcr' => [
81
                    'enabled' => true,
82
                    'content_basepath' => 'content',
83
                    'route_basepaths' => ['routes1', 'routes2'],
84
                ],
85
            ],
86
        ];
87
    }
88
89
    protected function createContainer(array $data = [])
90
    {
91
        return new ContainerBuilder(new ParameterBag($data));
92
    }
93
94
    /**
95
     * @test
96
     */
97
    public function if_orm_backend_is_enabled()
98
    {
99
        $this->load(['persistence' => ['orm' => ['enabled' => true]]]);
100
101
        $this->assertContainerBuilderHasParameter('swp_multi_tenancy.backend_type_orm', true);
102
    }
103
104
    /**
105
     * @test
106
     */
107
    public function if_phpcr_backend_is_enabled()
108
    {
109
        $this->load(['persistence' => ['phpcr' => ['enabled' => true]]]);
110
111
        $this->assertContainerBuilderHasParameter('swp_multi_tenancy.backend_type_phpcr', true);
112
    }
113
114
    /**
115
     * @test
116
     * @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
117
     */
118
    public function if_persistence_backend_is_not_enabled()
119
    {
120
        $this->load();
121
    }
122
123
    /**
124
     * @test
125
     */
126
    public function the_orm_listeners_are_disabled_by_default()
127
    {
128
        $this->load(['persistence' => ['phpcr' => ['enabled' => true]]]);
129
130
        $this->assertContainerBuilderNotHasService('swp_multi_tenancy.tenant_listener');
131
        $this->assertContainerBuilderNotHasService('swp_multi_tenancy.tenant_subscriber');
132
    }
133
134
    /**
135
     * @test
136
     */
137
    public function the_orm_listeners_are_enabled()
138
    {
139
        $this->load(['use_orm_listeners' => true, 'persistence' => ['phpcr' => ['enabled' => true]]]);
140
141
        $this->assertContainerBuilderHasService('swp_multi_tenancy.tenant_listener');
142
        $this->assertContainerBuilderHasService('swp_multi_tenancy.tenant_subscriber');
143
    }
144
145
    /**
146
     * @test
147
     */
148
    public function if_loads_all_needed_services_by_default()
149
    {
150
        $this->load(['persistence' => ['phpcr' => ['enabled' => true]]]);
151
152
        $this->assertContainerBuilderHasService('swp.repository.tenant', TenantRepository::class);
153
        $this->assertContainerBuilderHasService('swp.factory.tenant', TenantFactory::class);
154
        $this->assertContainerBuilderHasService('swp.object_manager.tenant');
155
    }
156
157
    /**
158
     * @test
159
     */
160
    public function when_phpcr_backend_enabeled()
161
    {
162
        $this->load(['persistence' => ['phpcr' => ['enabled' => true]]]);
163
164
        $this->assertContainerBuilderHasService('swp.repository.tenant', TenantRepository::class);
165
        $this->assertContainerBuilderHasService('swp.factory.tenant', TenantFactory::class);
166
        $this->assertContainerBuilderHasService('swp.object_manager.tenant');
167
        $this->assertContainerBuilderHasService('swp_multi_tenancy.phpcr.generic_initializer');
168
        $this->assertContainerBuilderHasService('swp_multi_tenancy.phpcr.initializer');
169
        $this->assertContainerBuilderHasService('swp_multi_tenancy.path_builder');
170
        $this->assertContainerBuilderHasService('swp_multi_tenancy.tenant_aware_router');
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    protected function getContainerExtensions()
177
    {
178
        return [
179
            new SWPMultiTenancyExtension(),
180
        ];
181
    }
182
}
183