Completed
Pull Request — master (#1)
by Rafał
04:48
created

TenantSubscriberSpec   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 0
cbo 6
dl 0
loc 68
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 4 1
A it_is_initializable() 0 4 1
A it_implements_event_subscriber_interface() 0 4 1
A it_subscribes_to_an_event() 0 4 1
A it_should_skip_when_tenant_is_set_on_tenant_aware_object() 0 13 1
A it_sets_the_tenant_on_pre_persist_doctrine_event() 0 19 1
A it_sets_only_tenant_aware_interface_implementation_on_pre_presist() 0 11 1
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher MultiTenancy Bundle.
5
 *
6
 * Copyright 2015 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 2015 Sourcefabric z.ú.
12
 * @license http://www.superdesk.org/license
13
 */
14
namespace spec\SWP\MultiTenancyBundle\EventListener;
15
16
use PhpSpec\ObjectBehavior;
17
use Prophecy\Argument;
18
use Doctrine\ORM\Events;
19
use Doctrine\ORM\Event\LifecycleEventArgs;
20
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
21
use SWP\Component\MultiTenancy\Model\Tenant;
22
use SWP\Component\MultiTenancy\Model\TenantAwareInterface;
23
use Symfony\Component\DependencyInjection\ContainerInterface;
24
25
class TenantSubscriberSpec extends ObjectBehavior
26
{
27
    public function let(ContainerInterface $container)
28
    {
29
        $this->beConstructedWith($container);
30
    }
31
32
    public function it_is_initializable()
33
    {
34
        $this->shouldHaveType('SWP\MultiTenancyBundle\EventListener\TenantSubscriber');
35
    }
36
37
    public function it_implements_event_subscriber_interface()
38
    {
39
        $this->shouldImplement('Doctrine\Common\EventSubscriber');
40
    }
41
42
    public function it_subscribes_to_an_event()
43
    {
44
        $this::getSubscribedEvents()->shouldReturn([Events::prePersist]);
45
    }
46
47
    public function it_should_skip_when_tenant_is_set_on_tenant_aware_object(
48
        LifecycleEventArgs $event,
49
        TenantAwareInterface $tenantAware
50
    ) {
51
        $tenant = new Tenant();
52
        $tenant->setSubdomain('example.com');
53
        $tenant->setName('Example');
54
55
        $tenantAware->getTenant()->shouldBeCalled()->willReturn($tenant);
56
        $event->getEntity()->willReturn($tenantAware);
57
58
        $this->prePersist($event)->shouldReturn(null);
59
    }
60
61
    public function it_sets_the_tenant_on_pre_persist_doctrine_event(
62
        TenantContextInterface $tenantContext,
63
        LifecycleEventArgs $event,
64
        TenantAwareInterface $tenantAware,
65
        $container
66
    ) {
67
        $tenant = new Tenant();
68
        $tenant->setSubdomain('example.com');
69
        $tenant->setName('Example');
70
71
        $tenantAware->getTenant()->shouldBeCalled()->willReturn(null);
72
        $event->getEntity()->willReturn($tenantAware);
73
        $tenantContext->getTenant()->shouldBeCalled()->willReturn($tenant);
74
        $container->get('swp_multi_tenancy.tenant_context')->willReturn($tenantContext);
75
76
        $tenantAware->setTenant($tenant)->shouldBeCalled();
77
78
        $this->prePersist($event)->shouldBeNull();
79
    }
80
81
    public function it_sets_only_tenant_aware_interface_implementation_on_pre_presist(
82
        TenantAwareInterface $tenantAware,
83
        LifecycleEventArgs $event
84
    ) {
85
        $item = new \stdClass();
86
        $event->getEntity()->willReturn($item);
87
        $tenantAware->getTenant()->shouldNotBeCalled();
88
        $tenantAware->setTenant(Argument::any())->shouldNotBeCalled();
89
90
        $this->prePersist($event)->shouldBeNull();
91
    }
92
}
93