|
| 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 | +} |
0 commit comments