Completed
Push — master ( e368e1...02adfd )
by Rafał
02:45
created

TenantSubscriberSpec::it_subscribes_to_events()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
15
namespace spec\SWP\Bundle\MultiTenancyBundle\EventListener;
16
17
use Doctrine\Common\EventSubscriber;
18
use Doctrine\ORM\Event\LifecycleEventArgs;
19
use Doctrine\ORM\Events;
20
use PhpSpec\ObjectBehavior;
21
use Prophecy\Argument;
22
use SWP\Bundle\MultiTenancyBundle\EventListener\TenantSubscriber;
23
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
24
use SWP\Component\MultiTenancy\Model\Tenant;
25
use SWP\Component\MultiTenancy\Model\TenantAwareInterface;
26
use Symfony\Component\DependencyInjection\ContainerInterface;
27
28
/**
29
 * @mixin TenantSubscriber
30
 */
31
class TenantSubscriberSpec extends ObjectBehavior
32
{
33
    public function let(ContainerInterface $container)
34
    {
35
        $this->beConstructedWith($container);
36
    }
37
38
    public function it_is_initializable()
39
    {
40
        $this->shouldHaveType(TenantSubscriber::class);
41
    }
42
43
    public function it_implements_event_subscriber_interface()
44
    {
45
        $this->shouldImplement(EventSubscriber::class);
46
    }
47
48
    public function it_subscribes_to_events()
49
    {
50
        $this->getSubscribedEvents()->shouldReturn([Events::prePersist, Events::preUpdate]);
51
    }
52
53
    public function it_should_skip_when_tenant_code_is_already_set_on_tenant_aware_object(
54
        LifecycleEventArgs $event,
55
        TenantAwareInterface $tenantAware
56
    ) {
57
        $tenant = new Tenant();
58
        $tenant->setSubdomain('example.com');
59
        $tenant->setName('Example');
60
        $tenant->setCode('123456');
61
62
        $tenantAware->getTenantCode()->shouldBeCalled()->willReturn($tenant);
63
        $event->getEntity()->willReturn($tenantAware);
64
65
        $this->prePersist($event)->shouldReturn(null);
66
    }
67
68 View Code Duplication
    public function it_sets_the_tenant_code_on_pre_persist_doctrine_event(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
        TenantContextInterface $tenantContext,
70
        LifecycleEventArgs $event,
71
        TenantAwareInterface $tenantAware,
72
        ContainerInterface $container
73
    ) {
74
        $tenant = new Tenant();
75
        $tenant->setSubdomain('example.com');
76
        $tenant->setName('Example');
77
        $tenant->setCode('123456');
78
79
        $tenantAware->getTenantCode()->shouldBeCalled()->willReturn(null);
80
        $event->getEntity()->willReturn($tenantAware);
81
        $tenantContext->getTenant()->shouldBeCalled()->willReturn($tenant);
82
83
        $tenantAware->setTenantCode('123456')->shouldBeCalled();
84
85
        $container->get('swp_multi_tenancy.tenant_context')->willReturn($tenantContext);
86
87
        $this->prePersist($event)->shouldBeNull();
88
    }
89
90
    public function it_sets_only_tenant_aware_interface_implementation_on_pre_presist(
91
        TenantAwareInterface $tenantAware,
92
        LifecycleEventArgs $event
93
    ) {
94
        $item = new \stdClass();
95
        $event->getEntity()->willReturn($item);
96
        $tenantAware->getTenantCode()->shouldNotBeCalled();
97
        $tenantAware->setTenantCode(Argument::any())->shouldNotBeCalled();
98
99
        $this->prePersist($event)->shouldBeNull();
100
    }
101
}
102