1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher MultiTenancy Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2017 Sourcefabric z.ú. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @copyright 2017 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace spec\SWP\Bundle\MultiTenancyBundle\EventListener; |
18
|
|
|
|
19
|
|
|
use Doctrine\Common\EventSubscriber; |
20
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
21
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
22
|
|
|
use Doctrine\ORM\Events; |
23
|
|
|
use PhpSpec\ObjectBehavior; |
24
|
|
|
use SWP\Bundle\MultiTenancyBundle\EventListener\OrganizationSubscriber; |
25
|
|
|
use SWP\Component\Common\Exception\UnexpectedTypeException; |
26
|
|
|
use SWP\Component\MultiTenancy\Context\TenantContextInterface; |
27
|
|
|
use SWP\Component\MultiTenancy\Model\Organization; |
28
|
|
|
use SWP\Component\MultiTenancy\Model\OrganizationAwareInterface; |
29
|
|
|
use SWP\Component\MultiTenancy\Model\Tenant; |
30
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
31
|
|
|
|
32
|
|
|
final class OrganizationSubscriberSpec extends ObjectBehavior |
33
|
|
|
{ |
34
|
|
|
public function let(ContainerInterface $container) |
35
|
|
|
{ |
36
|
|
|
$this->beConstructedWith($container); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function it_is_initializable() |
40
|
|
|
{ |
41
|
|
|
$this->shouldHaveType(OrganizationSubscriber::class); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function it_implements_event_subscriber_interface() |
45
|
|
|
{ |
46
|
|
|
$this->shouldImplement(EventSubscriber::class); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function it_subscribes_to_an_event() |
50
|
|
|
{ |
51
|
|
|
$this->getSubscribedEvents()->shouldReturn([Events::prePersist]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function it_should_skip_when_organization_is_already_set_on_organization_aware_object( |
55
|
|
|
LifecycleEventArgs $event, |
56
|
|
|
OrganizationAwareInterface $organizationAware |
57
|
|
|
) { |
58
|
|
|
$organization = new Organization(); |
59
|
|
|
$organization->setName('org1'); |
60
|
|
|
$organization->setEnabled(true); |
61
|
|
|
$organization->setCode('123456'); |
62
|
|
|
|
63
|
|
|
$organizationAware->getOrganization()->shouldBeCalled()->willReturn($organization); |
64
|
|
|
$event->getEntity()->willReturn($organizationAware); |
65
|
|
|
|
66
|
|
|
$this->prePersist($event); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function it_sets_the_organization_on_pre_persist_doctrine_event( |
70
|
|
|
TenantContextInterface $tenantContext, |
71
|
|
|
LifecycleEventArgs $event, |
72
|
|
|
OrganizationAwareInterface $organizationAware, |
73
|
|
|
ContainerInterface $container, |
74
|
|
|
ObjectManager $objectManager |
75
|
|
|
) { |
76
|
|
|
$organization = new Organization(); |
77
|
|
|
$organization->setName('org1'); |
78
|
|
|
$organization->setEnabled(true); |
79
|
|
|
$organization->setCode('123456'); |
80
|
|
|
|
81
|
|
|
$tenant = new Tenant(); |
82
|
|
|
$tenant->setSubdomain('example.com'); |
83
|
|
|
$tenant->setName('Example'); |
84
|
|
|
$tenant->setCode('avc2334'); |
85
|
|
|
$tenant->setOrganization($organization); |
86
|
|
|
|
87
|
|
|
$organizationAware->getOrganization()->shouldBeCalled()->willReturn(null); |
88
|
|
|
$event->getEntity()->willReturn($organizationAware); |
89
|
|
|
$objectManager->merge($organization)->willReturn($organization); |
90
|
|
|
$event->getObjectManager()->willReturn($objectManager); |
91
|
|
|
$tenantContext->getTenant()->shouldBeCalled()->willReturn($tenant); |
92
|
|
|
|
93
|
|
|
$organizationAware->setOrganization($organization)->shouldBeCalled(); |
94
|
|
|
|
95
|
|
|
$container->get('swp_multi_tenancy.tenant_context')->willReturn($tenantContext); |
96
|
|
|
|
97
|
|
|
$this->prePersist($event); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
View Code Duplication |
public function it_throws_exception_when_no_organization_on_pre_persist_doctrine_event( |
|
|
|
|
101
|
|
|
TenantContextInterface $tenantContext, |
102
|
|
|
LifecycleEventArgs $event, |
103
|
|
|
OrganizationAwareInterface $organizationAware, |
104
|
|
|
ContainerInterface $container |
105
|
|
|
) { |
106
|
|
|
$tenant = new Tenant(); |
107
|
|
|
$tenant->setSubdomain('example.com'); |
108
|
|
|
$tenant->setName('Example'); |
109
|
|
|
$tenant->setCode('avc2334'); |
110
|
|
|
|
111
|
|
|
$organizationAware->getOrganization()->shouldBeCalled()->willReturn(null); |
112
|
|
|
$event->getEntity()->willReturn($organizationAware); |
113
|
|
|
$tenantContext->getTenant()->shouldBeCalled()->willReturn($tenant); |
114
|
|
|
$container->get('swp_multi_tenancy.tenant_context')->willReturn($tenantContext); |
115
|
|
|
|
116
|
|
|
$this->shouldThrow(UnexpectedTypeException::class) |
117
|
|
|
->duringAddOrganization($event); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function it_sets_only_organization_aware_interface_implementation_on_pre_presist( |
121
|
|
|
OrganizationAwareInterface $organizationAware, |
122
|
|
|
LifecycleEventArgs $event |
123
|
|
|
) { |
124
|
|
|
$item = new \stdClass(); |
125
|
|
|
$event->getEntity()->willReturn($item); |
126
|
|
|
$organizationAware->getOrganization()->shouldNotBeCalled(); |
127
|
|
|
|
128
|
|
|
$this->prePersist($event); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
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.