...
 
<?php
/**
* CampaignUrnDelegate
* @author edgebal
*/
namespace Minds\Core\Boost\Campaigns\Delegates;
......@@ -20,9 +16,8 @@ class CampaignUrnDelegate
* CampaignUrnDelegate constructor.
* @param GuidBuilder $guid
*/
public function __construct(
$guid = null
) {
public function __construct(?GuidBuilder $guid = null)
{
$this->guid = $guid ?: Di::_()->get('Guid');
}
......@@ -31,18 +26,13 @@ class CampaignUrnDelegate
* @return Campaign
* @throws CampaignException
*/
public function onCreate(Campaign $campaign)
public function onCreate(Campaign $campaign): Campaign
{
if ($campaign->getUrn()) {
throw new CampaignException('Campaign already has an URN');
}
$guid = $this->guid->build();
$urn = "urn:campaign:{$guid}";
$campaign
->setUrn($urn);
return $campaign;
return $campaign->setUrn("urn:campaign:{$guid}");
}
}
<?php
/**
* NormalizeHashtagsDelegate
* @author edgebal
*/
namespace Minds\Core\Boost\Campaigns\Delegates;
......@@ -16,7 +12,7 @@ class NormalizeHashtagsDelegate
* @return Campaign
* @throws CampaignException
*/
public function onCreate(Campaign $campaign)
public function onCreate(Campaign $campaign): Campaign
{
$hashtags = $campaign->getHashtags();
......@@ -29,13 +25,10 @@ class NormalizeHashtagsDelegate
}, $hashtags))));
if (count($hashtags) > 5) {
throw new CampaignException('Campaigns should have 5 hashtags or less');
throw new CampaignException('Campaigns have a maximum of 5 hashtags');
}
$campaign
->setHashtags($hashtags);
return $campaign;
return $campaign->setHashtags($hashtags);
}
/**
......@@ -44,11 +37,9 @@ class NormalizeHashtagsDelegate
* @return Campaign
* @throws CampaignException
*/
public function onUpdate(Campaign $campaign, Campaign $campaignRef)
public function onUpdate(Campaign $campaign, Campaign $campaignRef): Campaign
{
$campaign
->setHashtags($campaignRef->getHashtags());
$campaign->setHashtags($campaignRef->getHashtags());
return $this->onCreate($campaign);
}
}
......@@ -2,14 +2,40 @@
namespace Spec\Minds\Core\Boost\Campaigns\Delegates;
use Minds\Core\Boost\Campaigns\Campaign;
use Minds\Core\Boost\Campaigns\CampaignException;
use Minds\Core\Boost\Campaigns\Delegates\CampaignUrnDelegate;
use Minds\Core\GuidBuilder;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class CampaignUrnDelegateSpec extends ObjectBehavior
{
/** @var GuidBuilder */
protected $guidBuilder;
public function let(GuidBuilder $guidBuilder)
{
$this->beConstructedWith($guidBuilder);
$this->guidBuilder = $guidBuilder;
}
public function it_is_initializable()
{
$this->shouldHaveType(CampaignUrnDelegate::class);
}
public function it_should_throw_an_exception_if_urn_already_set(Campaign $campaign)
{
$campaign->getUrn()->shouldBeCalled()->willReturn('urn:campaign:1234');
$this->shouldThrow(CampaignException::class)->during('onCreate', [$campaign]);
}
public function it_should_create_and_set_a_urn(Campaign $campaign)
{
$campaign->getUrn()->shouldBeCalled()->willReturn(null);
$this->guidBuilder->build()->shouldBeCalled()->willReturn(12345);
$campaign->setUrn('urn:campaign:12345')->shouldBeCalled()->willReturn($campaign);
$this->onCreate($campaign);
}
}
......@@ -2,6 +2,8 @@
namespace Spec\Minds\Core\Boost\Campaigns\Delegates;
use Minds\Core\Boost\Campaigns\Campaign;
use Minds\Core\Boost\Campaigns\CampaignException;
use Minds\Core\Boost\Campaigns\Delegates\NormalizeHashtagsDelegate;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
......@@ -12,4 +14,84 @@ class NormalizeHashtagsDelegateSpec extends ObjectBehavior
{
$this->shouldHaveType(NormalizeHashtagsDelegate::class);
}
public function it_should_throw_exception_if_more_than_5_hashtags_on_create(Campaign $campaign)
{
$hashtags = [
'one',
'two',
'three',
'four',
'five',
'six'
];
$campaign->getHashtags()->shouldBeCalled()->willReturn($hashtags);
$this->shouldThrow(CampaignException::class)->during('onCreate', [$campaign]);
}
public function it_should_throw_exception_if_more_than_5_hashtags_on_update(Campaign $campaign, Campaign $campaignRef)
{
$hashtags = [
'one',
'two',
'three',
'four',
'five',
'six'
];
$campaignRef->getHashtags()->shouldBeCalled()->willReturn($hashtags);
$campaign->setHashtags($hashtags)->shouldBeCalled()->willReturn($campaign);
$campaign->getHashtags()->shouldBeCalled()->willReturn($hashtags);
$this->shouldThrow(CampaignException::class)->during('onUpdate', [$campaign, $campaignRef]);
}
public function it_should_normalise_hashtags_on_create(Campaign $campaign)
{
$hashtags = [
'on e',
'two',
'th*ree',
'four',
'five'
];
$hashtagsNormal = [
'one',
'two',
'three',
'four',
'five'
];
$campaign->getHashtags()->shouldBeCalled()->willReturn($hashtags);
$campaign->setHashtags($hashtagsNormal)->shouldBeCalled()->willReturn($campaign);
$this->onCreate($campaign);
}
public function it_should_normalise_hashtags_on_update(Campaign $campaign, Campaign $campaignRef)
{
$hashtags = [
'on e',
'two',
'th*ree',
'four',
'five'
];
$hashtagsNormal = [
'one',
'two',
'three',
'four',
'five'
];
$campaignRef->getHashtags()->shouldBeCalled()->willReturn($hashtags);
$campaign->setHashtags($hashtags)->shouldbeCalled()->willReturn($campaign);
$campaign->getHashtags()->shouldBeCalled()->willReturn($hashtags);
$campaign->setHashtags($hashtagsNormal)->shouldBeCalled()->willReturn($campaign);
$this->onUpdate($campaign, $campaignRef);
}
}