Skip to content

Commit b2d8d99

Browse files
author
Marcin Wójcik
committed
Merge branch 'develop' into 'master'
Develop See merge request integrations/share-code-plugin!41
2 parents 280209f + 7f0d2f8 commit b2d8d99

10 files changed

Lines changed: 245 additions & 33 deletions

File tree

src/Cart/CartService.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function exportCart(AddCartCommand $addCartCommand)
8181
*/
8282
public function sendCart(AddCartCommand $addCartCommand)
8383
{
84-
if ($this->cartAlreadySend($addCartCommand->getCart())) {
84+
if ($this->cartAlreadySent($addCartCommand->getCart())) {
8585
return;
8686
}
8787

@@ -199,12 +199,9 @@ private function getCartHash(Cart $cart)
199199
* @param Cart $cart
200200
* @return bool
201201
*/
202-
private function cartAlreadySend(Cart $cart)
202+
private function cartAlreadySent(Cart $cart)
203203
{
204-
$cacheKey = $this->getCartCacheKey($cart);
205-
$currentProductHash = $this->getCartHash($cart);
206-
207-
return ($cachedProductHash = $this->cache->get($cacheKey)) && $currentProductHash === $cachedProductHash;
204+
return $this->getCartHash($cart) === $this->cache->get($this->getCartCacheKey($cart));
208205
}
209206

210207
}

src/Contact/AddContactCommand.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22
namespace GrShareCode\Contact;
33

4+
use GrShareCode\Validation\Assert\Assert;
5+
use GrShareCode\Validation\Assert\InvalidArgumentException;
6+
47
/**
58
* Class AddContactCommand
69
* @package GrShareCode\Contact
@@ -32,9 +35,20 @@ class AddContactCommand
3235
* @param int $dayOfCycle
3336
* @param ContactCustomFieldsCollection $customFieldsCollection
3437
* @param string $originValue
38+
* @throws InvalidArgumentException
3539
*/
3640
public function __construct($email, $name, $contactListId, $dayOfCycle, $customFieldsCollection, $originValue)
3741
{
42+
Assert::that($email, 'Email in ' . AddContactCommand::class . ' should be a not null string')
43+
->notNull()
44+
->notEmpty()
45+
->string();
46+
47+
Assert::that($contactListId, 'Contact list in ' . AddContactCommand::class . ' should be a not null string')
48+
->notNull()
49+
->notEmpty()
50+
->string();
51+
3852
$this->email = $email;
3953
$this->name = $name;
4054
$this->contactListId = $contactListId;

src/Contact/ContactService.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ private function prepareParams(AddContactCommand $addContactCommand, ContactCust
201201
]
202202
];
203203

204+
if (empty($params['name'])) {
205+
unset($params['name']);
206+
}
207+
204208
if (!empty($addContactCommand->getDayOfCycle())) {
205209
$params['dayOfCycle'] = $addContactCommand->getDayOfCycle();
206210
}

tests/Generator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,17 +231,18 @@ private static function createHistoricalOrderCollection()
231231
}
232232

233233
/**
234+
* @param string $name
234235
* @return AddContactCommand
235236
*/
236-
public static function createAddContactCommand()
237+
public static function createAddContactCommand($name = 'Adam Kowalski')
237238
{
238239
$customFieldCollection = new ContactCustomFieldsCollection();
239240
$customFieldCollection->add(new ContactCustomField('id_1', 'value_1'));
240241
$customFieldCollection->add(new ContactCustomField('id_2', 'value_2'));
241242

242243
return new AddContactCommand(
243244
'adam.kowalski@getresponse.com',
244-
'Adam Kowalski',
245+
$name,
245246
'contactListId',
246247
3,
247248
$customFieldCollection,

tests/Unit/BaseTestCase.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace GrShareCode\Tests\Unit;
3+
4+
use PHPUnit\Framework\TestCase;
5+
6+
/**
7+
* Class BaseTestCase
8+
* @package GrShareCode\Tests\Unit
9+
*/
10+
class BaseTestCase extends TestCase
11+
{
12+
/**
13+
* @param string $name
14+
* @param string[] $methodsToOverride
15+
* @return \PHPUnit_Framework_MockObject_MockObject | object
16+
*/
17+
protected function getMockWithoutConstructing($name, array $methodsToOverride = [])
18+
{
19+
return $this->getMockBuilder($name)
20+
->disableOriginalConstructor()
21+
->setMethods($methodsToOverride)
22+
->getMock();
23+
}
24+
}

tests/Unit/Domain/Cart/CartServiceTest.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function shouldNotSentCartIfAlreadySentWithSameData()
6767
->expects(self::once())
6868
->method('get')
6969
->with('cart_key' . $command->getCart()->getCartId())
70-
->willReturn('f91ec96768b279d32473d372bb3ec179');
70+
->willReturn('4a3afe4fab8f35cc2d9b92b580c8d7bd');
7171

7272
$this->grApiClientMock
7373
->expects(self::never())
@@ -120,9 +120,17 @@ public function shouldCreateCartWithProductInGetResponseAndRetrieveContactFromCa
120120
$this->cacheMock
121121
->expects(self::exactly(2))
122122
->method('get')
123-
->withConsecutive(['cart_key' . $command->getCart()->getCartId()], [$command->getEmail() . '_' . $command->getContactListId()])
123+
->withConsecutive(
124+
['cart_key' . $command->getCart()->getCartId()],
125+
[$command->getEmail() . '_' . $command->getContactListId()]
126+
)
124127
->willReturnOnConsecutiveCalls('f91ec96768b279d32473d372bb3ec179XX', json_encode($contact));
125128

129+
$this->cacheMock
130+
->expects(self::once())
131+
->method('set')
132+
->with('cart_key' . $command->getCart()->getCartId(), '4a3afe4fab8f35cc2d9b92b580c8d7bd', 600);
133+
126134
$this->grApiClientMock
127135
->expects(self::never())
128136
->method('getContactByEmail');
@@ -152,11 +160,6 @@ public function shouldCreateCartWithProductInGetResponseAndRetrieveContactFromCa
152160
->method('saveCartMapping')
153161
->with($command->getShopId(), $command->getCart()->getCartId(), 'grCartId');
154162

155-
$this->cacheMock
156-
->expects(self::once())
157-
->method('set')
158-
->with('cart_key' . $command->getCart()->getCartId(), 'f91ec96768b279d32473d372bb3ec179', 600);
159-
160163
$this->sut->sendCart($command);
161164
}
162165

@@ -203,7 +206,7 @@ public function shouldCreateCartWithProductInGetResponse()
203206
->method('set')
204207
->withConsecutive(
205208
[$command->getEmail() . '_' . $command->getContactListId(), json_encode($contact), 600],
206-
['cart_key' . $command->getCart()->getCartId(), 'f91ec96768b279d32473d372bb3ec179', 600]
209+
['cart_key' . $command->getCart()->getCartId(), '4a3afe4fab8f35cc2d9b92b580c8d7bd', 600]
207210
);
208211

209212
$this->productServiceMock
@@ -275,7 +278,7 @@ public function shouldUpdateCartInGetResponse()
275278
->method('set')
276279
->withConsecutive(
277280
[$command->getEmail() . '_' . $command->getContactListId(), json_encode($contact), 600],
278-
['cart_key' . $command->getCart()->getCartId(), 'f91ec96768b279d32473d372bb3ec179', 600]
281+
['cart_key' . $command->getCart()->getCartId(), '4a3afe4fab8f35cc2d9b92b580c8d7bd', 600]
279282
);
280283

281284
$this->productServiceMock
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
namespace GrShareCode\Tests\Unit\Domain\Contact;
3+
4+
use GrShareCode\Contact\AddContactCommand;
5+
use GrShareCode\Contact\ContactCustomFieldsCollection;
6+
use GrShareCode\Tests\Unit\BaseTestCase;
7+
use GrShareCode\Validation\Assert\InvalidArgumentException;
8+
9+
/**
10+
* Class AddContactCommandTest
11+
* @package GrShareCode\Contact
12+
*/
13+
class AddContactCommandTest extends BaseTestCase
14+
{
15+
16+
/**
17+
* @test
18+
* @dataProvider validDataProvider
19+
* @param string $email
20+
* @param string $name
21+
* @param string $contactListId
22+
* @param int $dayOfCycle
23+
* @param ContactCustomFieldsCollection $customFieldsCollection
24+
* @param string $originValue
25+
*/
26+
public function shouldCreateValidInstance($email, $name, $contactListId, $dayOfCycle, $customFieldsCollection, $originValue)
27+
{
28+
self::assertInstanceOf(
29+
AddContactCommand::class,
30+
new AddContactCommand(
31+
$email,
32+
$name,
33+
$contactListId,
34+
$dayOfCycle,
35+
$customFieldsCollection,
36+
$originValue
37+
)
38+
);
39+
}
40+
41+
/**
42+
* @return array
43+
*/
44+
public function validDataProvider()
45+
{
46+
return [
47+
[
48+
'email' => 'noone@example.com',
49+
'name' => 'name',
50+
'contact_list_id' => 'va1',
51+
'day_of_cycle' => 0,
52+
new ContactCustomFieldsCollection(),
53+
'origin'
54+
],
55+
[
56+
'email' => 'noone@example.com',
57+
'name' => '',
58+
'contact_list_id' => 'va1',
59+
'day_of_cycle' => 0,
60+
new ContactCustomFieldsCollection(),
61+
'origin'
62+
],
63+
64+
];
65+
}
66+
67+
/**
68+
* @test
69+
* @dataProvider invalidDataProvider
70+
* @param string $email
71+
* @param string $name
72+
* @param string $contactListId
73+
* @param int $dayOfCycle
74+
* @param ContactCustomFieldsCollection $customFieldsCollection
75+
* @param string $originValue
76+
*/
77+
public function shouldThrowExceptionWhenInvalidParameters($email, $name, $contactListId, $dayOfCycle, $customFieldsCollection, $originValue)
78+
{
79+
self::expectException(InvalidArgumentException::class);
80+
81+
new AddContactCommand(
82+
$email,
83+
$name,
84+
$contactListId,
85+
$dayOfCycle,
86+
$customFieldsCollection,
87+
$originValue
88+
);
89+
}
90+
91+
public function invalidDataProvider()
92+
{
93+
return [
94+
[
95+
'email' => null,
96+
'name' => 'name',
97+
'contact_list_id' => 'va1',
98+
'day_of_cycle' => 0,
99+
new ContactCustomFieldsCollection(),
100+
'origin'
101+
],
102+
[
103+
'email' => '',
104+
'name' => 'name',
105+
'contact_list_id' => 'va1',
106+
'day_of_cycle' => 0,
107+
new ContactCustomFieldsCollection(),
108+
'origin'
109+
],
110+
[
111+
'email' => 'nonoe@example.com',
112+
'name' => 'name',
113+
'contact_list_id' => '',
114+
'day_of_cycle' => 0,
115+
new ContactCustomFieldsCollection(),
116+
'origin'
117+
],
118+
[
119+
'email' => 'nonoe@example.com',
120+
'name' => 'name',
121+
'contact_list_id' => null,
122+
'day_of_cycle' => 0,
123+
new ContactCustomFieldsCollection(),
124+
'origin'
125+
],
126+
[
127+
'email' => 'nonoe@example.com',
128+
'name' => 'name',
129+
'contact_list_id' => false,
130+
'day_of_cycle' => 0,
131+
new ContactCustomFieldsCollection(),
132+
'origin'
133+
],
134+
];
135+
}
136+
}

0 commit comments

Comments
 (0)