Commit 5d68c7c7 authored by Guy Thouret's avatar Guy Thouret

(fix) Reclaim vertical space in DispatcherSpec; Fix tests; Appropriate return...

(fix) Reclaim vertical space in DispatcherSpec; Fix tests; Appropriate return type declarations in Manager - #631
1 merge request!235WIP: Boost Campaigns (&24)
Pipeline #87919012 passed with stages
in 11 minutes and 5 seconds
......@@ -101,7 +101,7 @@ class Manager
* @return Response
* @throws Exception
*/
public function getCampaigns(array $opts = [])
public function getCampaigns(array $opts = []): Response
{
$opts = array_merge([
'useElastic' => true
......@@ -133,7 +133,7 @@ class Manager
* @param array $opts
* @return Response|null
*/
public function getCampaignsAndBoosts(array $opts = [])
public function getCampaignsAndBoosts(array $opts = []): ?Response
{
$response = null;
......@@ -153,7 +153,7 @@ class Manager
* @return Campaign|null
* @throws Exception
*/
public function getCampaignByUrn(string $urn, array $opts = [])
public function getCampaignByUrn(string $urn, array $opts = []): ?Campaign
{
$opts = array_merge([
'useElastic' => false
......@@ -183,7 +183,7 @@ class Manager
* @return Campaign
* @throws CampaignException
*/
public function createCampaign(Campaign $campaign, $paymentPayload = null)
public function createCampaign(Campaign $campaign, $paymentPayload = null): Campaign
{
$campaign = $this->campaignUrnDelegate->onCreate($campaign);
$campaign->setOwner($this->actor);
......@@ -212,8 +212,7 @@ class Manager
$campaign = $this->normalizeHashtagsDelegate->onCreate($campaign);
$campaign = $this->paymentsDelegate->onCreate($campaign, $paymentPayload);
$this->repository->putCampaign($campaign);
$this->elasticRepository->putCampaign($campaign);
$this->sync($campaign);
return $campaign;
}
......@@ -225,7 +224,7 @@ class Manager
* @throws CampaignException
* @throws Exception
*/
public function updateCampaign(Campaign $campaignRef, $paymentPayload = null)
public function updateCampaign(Campaign $campaignRef, $paymentPayload = null): Campaign
{
$campaign = $this->getCampaignByUrn($campaignRef->getUrn());
$isOwner = (string) $this->actor->guid === (string) $campaign->getOwnerGuid();
......@@ -246,6 +245,7 @@ class Manager
$campaign = $this->normalizeDatesDelegate->onUpdate($campaign, $campaignRef);
$campaign = $this->normalizeHashtagsDelegate->onUpdate($campaign, $campaignRef);
$campaign = $this->paymentsDelegate->onUpdate($campaign, $campaignRef, $paymentPayload);
$this->sync($campaign);
$this->sendToQueue($campaign);
......@@ -254,14 +254,12 @@ class Manager
/**
* @param Campaign $campaign
* @return Campaign
* @throws Exception
*/
public function sync(Campaign $campaign): Campaign
public function sync(Campaign $campaign): void
{
$this->repository->putCampaign($campaign);
$this->elasticRepository->putCampaign($campaign);
return $campaign;
}
/**
......@@ -381,7 +379,7 @@ class Manager
* @param Payment $paymentRef
* @throws Exception
*/
public function onPaymentSuccess(Payment $paymentRef)
public function onPaymentSuccess(Payment $paymentRef): void
{
$campaign = $this->getCampaignByUrn("urn:campaign:{$paymentRef->getCampaignGuid()}");
$campaign = $this->paymentsDelegate->onConfirm($campaign, $paymentRef);
......@@ -394,7 +392,7 @@ class Manager
* @param Payment $paymentRef
* @throws Exception
*/
public function onPaymentFailed(Payment $paymentRef)
public function onPaymentFailed(Payment $paymentRef): void
{
$campaign = $this->getCampaignByUrn("urn:campaign:{$paymentRef->getCampaignGuid()}");
$campaign = $this->paymentsDelegate->onFail($campaign, $paymentRef);
......
......@@ -17,10 +17,8 @@ class DispatcherSpec extends ObjectBehavior
/** @var Metrics */
protected $metrics;
public function let(
Manager $manager,
Metrics $metrics
) {
public function let(Manager $manager, Metrics $metrics)
{
$this->beConstructedWith($manager, $metrics, 10);
$this->manager = $manager;
$this->metrics = $metrics;
......@@ -31,164 +29,81 @@ class DispatcherSpec extends ObjectBehavior
$this->shouldHaveType(Dispatcher::class);
}
public function it_should_not_sync_campaign_on_lifecycle_if_below_threshold(
Campaign $initialCampaign,
Campaign $campaign
) {
$campaign->getUrn()
->willReturn('urn:campaign:1000');
$this->manager->getCampaignByUrn('urn:campaign:1000')
->shouldBeCalled()
->willReturn($initialCampaign);
$initialCampaign->isDelivering()
->shouldBeCalled()
->willReturn(true);
$initialCampaign->getImpressionsMet()
->shouldBeCalled()
->willReturn(0);
public function it_should_not_sync_campaign_on_lifecycle_if_below_threshold(Campaign $campaign, Campaign $campaignRef)
{
$campaignRef->getUrn()->willReturn('urn:campaign:1000');
$this->metrics
->setCampaign($initialCampaign)
->shouldBeCalled()
->willReturn($this->metrics);
$this->manager->getCampaignByUrn('urn:campaign:1000')->shouldBeCalled()->willReturn($campaign);
$this->metrics
->getImpressionsMet()
->shouldBeCalled()
->willReturn(1);
$campaign->isDelivering()->shouldBeCalled()->willReturn(true);
$campaign->getImpressionsMet()->shouldBeCalled()->willReturn(0);
$this->manager->sync($initialCampaign)
->shouldNotBeCalled();
$this->metrics->setCampaign($campaign)->shouldBeCalled()->willReturn($this->metrics);
$this->metrics->getImpressionsMet()->shouldBeCalled()->willReturn(1);
$initialCampaign->shouldBeCompleted(Argument::any())
->shouldBeCalled()
->willReturn(false);
$this->manager->sync($campaign)->shouldNotBeCalled();
$initialCampaign->shouldBeStarted(Argument::any())
->shouldBeCalled()
->willReturn(false);
$campaign->shouldBeCompleted(Argument::any())->shouldBeCalled()->willReturn(false);
$campaign->shouldBeStarted(Argument::any())->shouldBeCalled()->willReturn(false);
$this->onLifecycle('urn:campaign:1000');
}
public function it_should_sync_campaign_on_lifecycle_if_above_threshold(
Campaign $initialCampaign,
Campaign $campaign
) {
$initialCampaign->getUrn()
->willReturn('urn:campaign:1000');
$campaign->getUrn()
->willReturn('urn:campaign:1000');
$this->manager->getCampaignByUrn('urn:campaign:1000')
->shouldBeCalled()
->willReturn($initialCampaign);
$initialCampaign->isDelivering()
->shouldBeCalled()
->willReturn(true);
public function it_should_sync_campaign_on_lifecycle_if_above_threshold(Campaign $campaign, Campaign $campaignRef)
{
$campaign->getUrn()->willReturn('urn:campaign:1000');
$initialCampaign->getImpressionsMet()
->shouldBeCalled()
->willReturn(0);
$campaignRef->getUrn()->willReturn('urn:campaign:1000');
$this->metrics
->setCampaign($initialCampaign)
->shouldBeCalled()
->willReturn($this->metrics);
$this->manager->getCampaignByUrn('urn:campaign:1000')->shouldBeCalled()->willReturn($campaign);
$this->metrics
->getImpressionsMet()
->shouldBeCalled()
->willReturn(10);
$campaign->isDelivering()->shouldBeCalled()->willReturn(true);
$campaign->getImpressionsMet()->shouldBeCalled()->willReturn(0);
$initialCampaign
->setImpressionsMet(10)
->shouldBeCalled()
->willReturn($initialCampaign);
$this->metrics->setCampaign($campaign)->shouldBeCalled()->willReturn($this->metrics);
$this->metrics->getImpressionsMet()->shouldBeCalled()->willReturn(10);
$this->manager->sync($initialCampaign)
->shouldBeCalled()
->willReturn($initialCampaign);
$campaign->setImpressionsMet(10)->shouldBeCalled()->willReturn($campaign);
$initialCampaign->shouldBeCompleted(Argument::any())
->shouldBeCalled()
->willReturn(false);
$this->manager->sync($campaign)->shouldBeCalled();
$initialCampaign->shouldBeStarted(Argument::any())
->shouldBeCalled()
->willReturn(false);
$campaign->shouldBeCompleted(Argument::any())->shouldBeCalled()->willReturn(false);
$campaign->shouldBeStarted(Argument::any())->shouldBeCalled()->willReturn(false);
$this->onLifecycle('urn:campaign:1000');
}
public function it_should_complete_campaign(
Campaign $campaign
) {
$campaign->getUrn()
->willReturn('urn:campaign:1000');
$this->manager->getCampaignByUrn('urn:campaign:1000')
->shouldBeCalled()
->willReturn($campaign);
public function it_should_complete_campaign(Campaign $campaign)
{
$campaign->getUrn()->willReturn('urn:campaign:1000');
$this->metrics
->setCampaign($campaign)
->shouldBeCalled()
->willReturn($this->metrics);
$this->manager->getCampaignByUrn('urn:campaign:1000')->shouldBeCalled()->willReturn($campaign);
$campaign->isDelivering()
->shouldBeCalled()
->willReturn(false);
$this->metrics->setCampaign($campaign)->shouldBeCalled()->willReturn($this->metrics);
$campaign->shouldBeCompleted(Argument::any())
->shouldBeCalled()
->willReturn(true);
$campaign->isDelivering()->shouldBeCalled()->willReturn(false);
$campaign->shouldBeCompleted(Argument::any())->shouldBeCalled()->willReturn(true);
$this->manager->completeCampaign($campaign)
->shouldBeCalled()
->willReturn(true);
$this->manager->completeCampaign($campaign)->shouldBeCalled()->willReturn($campaign);
$campaign->shouldBeStarted(Argument::any())
->willReturn(false);
$campaign->shouldBeStarted(Argument::any())->willReturn(false);
$this->onLifecycle('urn:campaign:1000');
}
public function it_should_start_campaign(
Campaign $campaign
) {
$campaign->getUrn()
->willReturn('urn:campaign:1000');
$this->manager->getCampaignByUrn('urn:campaign:1000')
->shouldBeCalled()
->willReturn($campaign);
$this->metrics
->setCampaign($campaign)
->shouldBeCalled()
->willReturn($this->metrics);
public function it_should_start_campaign(Campaign $campaign)
{
$campaign->getUrn()->willReturn('urn:campaign:1000');
$campaign->isDelivering()
->shouldBeCalled()
->willReturn(false);
$this->manager->getCampaignByUrn('urn:campaign:1000')->shouldBeCalled()->willReturn($campaign);
$campaign->shouldBeCompleted(Argument::any())
->shouldBeCalled()
->willReturn(false);
$this->metrics->setCampaign($campaign)->shouldBeCalled()->willReturn($this->metrics);
$campaign->shouldBeStarted(Argument::any())
->shouldBeCalled()
->willReturn(true);
$campaign->isDelivering()->shouldBeCalled()->willReturn(false);
$campaign->shouldBeCompleted(Argument::any())->shouldBeCalled()->willReturn(false);
$campaign->shouldBeStarted(Argument::any())->shouldBeCalled()->willReturn(true);
$this->manager->start($campaign)
->shouldBeCalled()
->willReturn(true);
$this->manager->start($campaign)->shouldBeCalled()->willReturn($campaign);
$this->onLifecycle('urn:campaign:1000');
}
......
......@@ -271,7 +271,7 @@ class ManagerSpec extends ObjectBehavior
$this->repository->putCampaign($campaign)->shouldBeCalled();
$this->elasticRepository->putCampaign($campaign)->shouldBeCalled();
$this->sync($campaign)->shouldReturn($campaign);
$this->sync($campaign);
}
public function it_should_throw_exception_if_actor_present_on_start(Campaign $campaignRef)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment