Skip to content
GitLab
Menu
Why GitLab
Pricing
Contact Sales
Explore
Why GitLab
Pricing
Contact Sales
Explore
Sign in
Get free trial
Commits on Source (2)
(chore) Add spec test for boost campaign urn delegate -
#631
· e491db10
Guy Thouret
authored
Oct 03, 2019
e491db10
(chore) Add spec test for boost campaign delegate NormalizeHashtagsDelegate -
#631
· 1504e911
Guy Thouret
authored
Oct 03, 2019
1504e911
Hide whitespace changes
Inline
Side-by-side
Core/Boost/Campaigns/Delegates/CampaignUrnDelegate.php
View file @
1504e911
<?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
}
"
);
}
}
Core/Boost/Campaigns/Delegates/NormalizeHashtagsDelegate.php
View file @
1504e911
<?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
);
}
}
Spec/Core/Boost/Campaigns/Delegates/CampaignUrnDelegateSpec.php
View file @
1504e911
...
...
@@ -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
);
}
}
Spec/Core/Boost/Campaigns/Delegates/NormalizeHashtagsDelegateSpec.php
View file @
1504e911
...
...
@@ -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
);
}
}