Completed
Pull Request — master (#1)
by Rafał
03:36 queued 01:22
created

TenantSubscriberSpec   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 5
dl 0
loc 66
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_event() 0 4 1
A it_should_skip_when_tenant_is_set_on_tenant_aware_object() 0 13 1
A it_set_the_tenant_on_pre_persist_doctrine_event() 0 17 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 SWP\Component\MultiTenancy\Context\TenantContextInterface;
19
use Doctrine\ORM\Events;
20
use Doctrine\ORM\Event\LifecycleEventArgs;
21
use SWP\Component\MultiTenancy\Model\Tenant;
22
use SWP\Component\MultiTenancy\Model\TenantAwareInterface;
23
24
class TenantSubscriberSpec extends ObjectBehavior
25
{
26
    public function let(TenantContextInterface $tenantContext)
27
    {
28
        $this->beConstructedWith($tenantContext);
29
    }
30
31
    public function it_is_initializable()
32
    {
33
        $this->shouldHaveType('SWP\MultiTenancyBundle\EventListener\TenantSubscriber');
34
    }
35
36
    public function it_implements_event_subscriber_interface()
37
    {
38
        $this->shouldImplement('Doctrine\Common\EventSubscriber');
39
    }
40
41
    public function it_subscribes_to_event()
42
    {
43
        $this::getSubscribedEvents()->shouldReturn(array(Events::prePersist));
44
    }
45
46
    public function it_should_skip_when_tenant_is_set_on_tenant_aware_object(
47
        LifecycleEventArgs $event,
48
        TenantAwareInterface $tenantAware
49
    ) {
50
        $tenant = new Tenant();
51
        $tenant->setSubdomain('example.com');
52
        $tenant->setName('Example');
53
54
        $tenantAware->getTenant()->shouldBeCalled()->willReturn($tenant);
55
        $event->getEntity()->willReturn($tenantAware);
56
57
        $this->prePersist($event)->shouldReturn(null);
58
    }
59
60
    public function it_set_the_tenant_on_pre_persist_doctrine_event(
61
        $tenantContext,
62
        LifecycleEventArgs $event,
63
        TenantAwareInterface $tenantAware
64
    ) {
65
        $tenant = new Tenant();
66
        $tenant->setSubdomain('example.com');
67
        $tenant->setName('Example');
68
69
        $tenantAware->getTenant()->shouldBeCalled()->willReturn(null);
70
        $event->getEntity()->willReturn($tenantAware);
71
        $tenantContext->getTenant()->shouldBeCalled()->willReturn($tenant);
72
73
        $tenantAware->setTenant($tenant)->shouldBeCalled();
74
75
        $this->prePersist($event)->shouldBeNull();
76
    }
77
78
    public function it_sets_only_tenant_aware_interface_implementation_on_pre_presist(
79
        TenantAwareInterface $tenantAware,
80
        LifecycleEventArgs $event
81
    ) {
82
        $item = new \stdClass();
83
        $event->getEntity()->willReturn($item);
84
        $tenantAware->getTenant()->shouldNotBeCalled();
85
        $tenantAware->setTenant(Argument::any())->shouldNotBeCalled();
86
87
        $this->prePersist($event)->shouldBeNull();
88
    }
89
}
90