Skip to content

Commit 162864e

Browse files
committed
tests: updated orm tests
1 parent 5928aef commit 162864e

2 files changed

Lines changed: 233 additions & 0 deletions

File tree

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
3+
namespace spec\Ivoz\Provider\Domain\Service\Webhook;
4+
5+
use Ivoz\Provider\Domain\Model\Brand\BrandInterface;
6+
use Ivoz\Provider\Domain\Model\Company\CompanyInterface;
7+
use Ivoz\Provider\Domain\Model\Ddi\DdiInterface;
8+
use Ivoz\Provider\Domain\Model\Webhook\WebhookInterface;
9+
use Ivoz\Provider\Domain\Model\Webhook\WebhookRepository;
10+
use Ivoz\Provider\Domain\Service\Webhook\WebhookResolver;
11+
use PhpSpec\ObjectBehavior;
12+
use spec\HelperTrait;
13+
14+
class WebhookResolverSpec extends ObjectBehavior
15+
{
16+
use HelperTrait;
17+
18+
/**
19+
* @var WebhookRepository
20+
*/
21+
protected $webhookRepository;
22+
23+
/**
24+
* @var WebhookInterface
25+
*/
26+
protected $brandWebhook;
27+
28+
/**
29+
* @var WebhookInterface
30+
*/
31+
protected $companyWebhook;
32+
33+
/**
34+
* @var WebhookInterface
35+
*/
36+
protected $ddiWebhook;
37+
38+
public function let(WebhookRepository $webhookRepository)
39+
{
40+
$this->webhookRepository = $webhookRepository;
41+
$this->beConstructedWith($webhookRepository);
42+
43+
$this->prepareWebhooks();
44+
}
45+
46+
public function it_is_initializable()
47+
{
48+
$this->shouldHaveType(WebhookResolver::class);
49+
}
50+
51+
public function it_returns_null_when_no_webhooks_match()
52+
{
53+
$this
54+
->webhookRepository
55+
->findMatchingWebhooks(1, 10, 100)
56+
->willReturn([]);
57+
58+
$this->execute(1, 10, 100, 'start')->shouldReturn(null);
59+
}
60+
61+
public function it_returns_null_when_no_webhook_matches_event()
62+
{
63+
// Webhook only enabled for "answer", but we ask for "start"
64+
$this->brandWebhook->getEventStart()->willReturn(false);
65+
$this->brandWebhook->getEventAnswer()->willReturn(true);
66+
67+
$this
68+
->webhookRepository
69+
->findMatchingWebhooks(1, null, null)
70+
->willReturn([$this->brandWebhook]);
71+
72+
$this->execute(1, null, null, 'start')->shouldReturn(null);
73+
}
74+
75+
public function it_returns_brand_webhook_as_fallback()
76+
{
77+
$this->brandWebhook->getEventStart()->willReturn(true);
78+
$this->brandWebhook->getDdi()->willReturn(null);
79+
$this->brandWebhook->getCompany()->willReturn(null);
80+
81+
$this
82+
->webhookRepository
83+
->findMatchingWebhooks(1, null, null)
84+
->willReturn([$this->brandWebhook]);
85+
86+
$this->execute(1, null, null, 'start')->shouldReturn($this->brandWebhook);
87+
}
88+
89+
public function it_prefers_company_webhook_over_brand(
90+
CompanyInterface $company,
91+
) {
92+
$company->getId()->willReturn(10);
93+
94+
$this->brandWebhook->getEventStart()->willReturn(true);
95+
$this->brandWebhook->getDdi()->willReturn(null);
96+
$this->brandWebhook->getCompany()->willReturn(null);
97+
98+
$this->companyWebhook->getEventStart()->willReturn(true);
99+
$this->companyWebhook->getDdi()->willReturn(null);
100+
$this->companyWebhook->getCompany()->willReturn($company);
101+
102+
$this
103+
->webhookRepository
104+
->findMatchingWebhooks(1, 10, null)
105+
->willReturn([$this->brandWebhook, $this->companyWebhook]);
106+
107+
$this->execute(1, 10, null, 'start')->shouldReturn($this->companyWebhook);
108+
}
109+
110+
public function it_prefers_ddi_webhook_over_company_and_brand(
111+
CompanyInterface $company,
112+
DdiInterface $ddi,
113+
) {
114+
$company->getId()->willReturn(10);
115+
$ddi->getId()->willReturn(100);
116+
117+
$this->brandWebhook->getEventStart()->willReturn(true);
118+
$this->brandWebhook->getDdi()->willReturn(null);
119+
$this->brandWebhook->getCompany()->willReturn(null);
120+
121+
$this->companyWebhook->getEventStart()->willReturn(true);
122+
$this->companyWebhook->getDdi()->willReturn(null);
123+
$this->companyWebhook->getCompany()->willReturn($company);
124+
125+
$this->ddiWebhook->getEventStart()->willReturn(true);
126+
$this->ddiWebhook->getDdi()->willReturn($ddi);
127+
128+
$this
129+
->webhookRepository
130+
->findMatchingWebhooks(1, 10, 100)
131+
->willReturn([
132+
$this->brandWebhook,
133+
$this->companyWebhook,
134+
$this->ddiWebhook,
135+
]);
136+
137+
$this->execute(1, 10, 100, 'start')->shouldReturn($this->ddiWebhook);
138+
}
139+
140+
public function it_falls_back_to_brand_when_ddi_webhook_does_not_match_event(
141+
CompanyInterface $company,
142+
DdiInterface $ddi,
143+
) {
144+
$company->getId()->willReturn(10);
145+
$ddi->getId()->willReturn(100);
146+
147+
// Brand-level matches the event
148+
$this->brandWebhook->getEventEnd()->willReturn(true);
149+
$this->brandWebhook->getDdi()->willReturn(null);
150+
$this->brandWebhook->getCompany()->willReturn(null);
151+
152+
// DDI-level webhook exists but does not have eventEnd enabled
153+
$this->ddiWebhook->getEventEnd()->willReturn(false);
154+
155+
$this
156+
->webhookRepository
157+
->findMatchingWebhooks(1, 10, 100)
158+
->willReturn([$this->brandWebhook, $this->ddiWebhook]);
159+
160+
$this->execute(1, 10, 100, 'end')->shouldReturn($this->brandWebhook);
161+
}
162+
163+
protected function prepareWebhooks()
164+
{
165+
$this->brandWebhook = $this->getTestDouble(WebhookInterface::class);
166+
$this->companyWebhook = $this->getTestDouble(WebhookInterface::class);
167+
$this->ddiWebhook = $this->getTestDouble(WebhookInterface::class);
168+
}
169+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Tests\Provider\Webhook;
4+
5+
use Ivoz\Provider\Domain\Model\Webhook\Webhook;
6+
use Ivoz\Provider\Domain\Model\Webhook\WebhookRepository;
7+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
8+
use Tests\DbIntegrationTestHelperTrait;
9+
10+
class WebhookRepositoryTest extends KernelTestCase
11+
{
12+
use DbIntegrationTestHelperTrait;
13+
14+
/**
15+
* @test
16+
*/
17+
public function test_runner()
18+
{
19+
$this->it_finds_brand_level_webhooks();
20+
$this->it_finds_company_level_webhooks();
21+
$this->it_finds_ddi_level_webhooks();
22+
$this->it_returns_empty_for_unknown_brand();
23+
}
24+
25+
private function it_finds_brand_level_webhooks()
26+
{
27+
/** @var WebhookRepository $repository */
28+
$repository = $this->em->getRepository(Webhook::class);
29+
30+
$result = $repository->findMatchingWebhooks(1, null, null);
31+
32+
$this->assertCount(1, $result);
33+
}
34+
35+
private function it_finds_company_level_webhooks()
36+
{
37+
/** @var WebhookRepository $repository */
38+
$repository = $this->em->getRepository(Webhook::class);
39+
40+
$result = $repository->findMatchingWebhooks(1, 1, null);
41+
42+
$this->assertCount(3, $result);
43+
}
44+
45+
private function it_finds_ddi_level_webhooks()
46+
{
47+
/** @var WebhookRepository $repository */
48+
$repository = $this->em->getRepository(Webhook::class);
49+
50+
$result = $repository->findMatchingWebhooks(1, 1, 1);
51+
52+
$this->assertCount(4, $result);
53+
}
54+
55+
private function it_returns_empty_for_unknown_brand()
56+
{
57+
/** @var WebhookRepository $repository */
58+
$repository = $this->em->getRepository(Webhook::class);
59+
60+
$result = $repository->findMatchingWebhooks(999, null, null);
61+
62+
$this->assertEmpty($result);
63+
}
64+
}

0 commit comments

Comments
 (0)