Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed service definition + fixed testcases #29

Merged
merged 1 commit into from
Oct 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"require": {
"php": ">=5.6",
"drewm/mailchimp-api": "^2.2"
"drewm/mailchimp-api": "2.5.*"
},
"require-dev": {
"phpspec/phpspec": "~2@dev",
Expand Down
20 changes: 9 additions & 11 deletions spec/Subscriber/ListRepositorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class ListRepositorySpec extends ObjectBehavior
public function let(MailChimp $mailchimp, Subscriber $subscriber)
{
$this->prepareSubscriber($subscriber);
$this->prepareMailchimpLists($mailchimp);

$this->prepareMailchimpLists($mailchimp);
$this->beConstructedWith($mailchimp);
}

Expand Down Expand Up @@ -73,14 +73,14 @@ public function it_clean_a_subscriber(MailChimp $mailchimp, $subscriber)

public function it_delete_a_subscriber(MailChimp $mailchimp, $subscriber)
{
$mailchimp->delete("lists/ba039c6198/members/md5ofthesubscribermail")->willReturn('deleted');
$mailchimp->delete("lists/ba039c6198/members/b1a29fd58778c40c7f15f06a334dc691")->willReturn('deleted');

$this->delete('ba039c6198', $subscriber)->shouldReturn('deleted');
}

public function it_update_a_subscriber(MailChimp $mailchimp, $subscriber)
{
$mailchimp->patch("lists/ba039c6198/members/md5ofthesubscribermail", ["email_address" => "[email protected]", "merge_fields" => ["FNAME" => "Charles", "LNAME" => "Terrasse"], "language" => "fr", "email_type" => "html"])->willReturn('update');
$mailchimp->patch("lists/ba039c6198/members/b1a29fd58778c40c7f15f06a334dc691", ["email_address" => "[email protected]", "merge_fields" => ["FNAME" => "Charles", "LNAME" => "Terrasse"], "language" => "fr", "email_type" => "html"])->willReturn('update');

$this->update('ba039c6198', $subscriber)->shouldReturn('update');
}
Expand Down Expand Up @@ -194,16 +194,14 @@ protected function prepareSubscriber(Subscriber $subscriber)
}

protected function prepareMailchimpLists(MailChimp $mailchimp)
{
// subscriber hash
$mailchimp->subscriberHash('[email protected]')->willReturn('md5ofthesubscribermail');
{
// success
$mailchimp->success()->willReturn(true);
// get the list
$mailchimp->get("lists/ba039c6198")->willReturn(['id' => 'ba039c6198', 'name' => 'myList']);
$mailchimp->get("lists/notfound")->willReturn(null);
// subscribe member
$mailchimp->put("lists/ba039c6198/members/md5ofthesubscribermail", [
$mailchimp->put("lists/ba039c6198/members/b1a29fd58778c40c7f15f06a334dc691", [
'email_address' => '[email protected]',
'merge_fields' => ['FNAME' => 'Charles', 'LNAME' => 'Terrasse'],
'language' => 'fr',
Expand All @@ -217,23 +215,23 @@ protected function prepareMailchimpLists(MailChimp $mailchimp)
'status' => 'subscribed'
]);

$mailchimp->put("lists/ba039c6198/members/md5ofthesubscribermail", [
$mailchimp->put("lists/ba039c6198/members/b1a29fd58778c40c7f15f06a334dc691", [
'email_address' => '[email protected]',
'merge_fields' => ['FNAME' => 'Charles', 'LNAME' => 'Terrasse'],
'language' => 'fr',
'email_type' => 'html',
'status' => 'unsubscribed'
])->willReturn('unsubscribed');

$mailchimp->put("lists/ba039c6198/members/md5ofthesubscribermail", [
$mailchimp->put("lists/ba039c6198/members/b1a29fd58778c40c7f15f06a334dc691", [
'email_address' => '[email protected]',
'merge_fields' => ['FNAME' => 'Charles', 'LNAME' => 'Terrasse'],
'language' => 'fr',
'email_type' => 'html',
'status' => 'pending'
])->willReturn('pending');

$mailchimp->put("lists/ba039c6198/members/md5ofthesubscribermail", [
$mailchimp->put("lists/ba039c6198/members/b1a29fd58778c40c7f15f06a334dc691", [
'email_address' => '[email protected]',
'merge_fields' => ['FNAME' => 'Charles', 'LNAME' => 'Terrasse'],
'language' => 'fr',
Expand Down
9 changes: 6 additions & 3 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,17 @@ services:
- { name: kernel.event_listener, event: welp.mailchimp.delete, method: onDelete }

# Commands
Welp\MailchimpBundle\Command\SynchronizeMergeFieldsCommand:
welp_mailchimp.command.synchronize_merge_fields:
Nightbr marked this conversation as resolved.
Show resolved Hide resolved
class: Welp\MailchimpBundle\Command\SynchronizeMergeFieldsCommand
tags:
- { name: 'console.command', command: 'welp:mailchimp:synchronize-merge-fields' }

Welp\MailchimpBundle\Command\SynchronizeSubscribersCommand:
welp_mailchimp.command.synchronize_subscribers:
class: Welp\MailchimpBundle\Command\SynchronizeSubscribersCommand
tags:
- { name: 'console.command', command: 'welp:mailchimp:synchronize-subscribers' }

Welp\MailchimpBundle\Command\WebhookCommand:
welp_mailchimp.command.webhook:
class: Welp\MailchimpBundle\Command\WebhookCommand
tags:
- { name: 'console.command', command: 'welp:mailchimp:webhook' }
16 changes: 8 additions & 8 deletions src/Subscriber/ListRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function putSubscriberInList($listId, Subscriber $subscriber, $status)
if (!in_array($status, ['subscribed', 'unsubscribed', 'cleaned', 'pending', 'transactional'])) {
throw new \RuntimeException('$status must be one of these values: subscribed, unsubscribed, cleaned, pending, transactional');
}
$subscriberHash = $this->mailchimp->subscriberHash($subscriber->getEmail());
$subscriberHash = MailChimp::subscriberHash($subscriber->getEmail());
$result = $this->mailchimp->put("lists/$listId/members/$subscriberHash",
array_merge(
$subscriber->formatMailChimp(),
Expand Down Expand Up @@ -147,7 +147,7 @@ public function transactional($listId, Subscriber $subscriber)
*/
public function update($listId, Subscriber $subscriber)
{
$subscriberHash = $this->mailchimp->subscriberHash($subscriber->getEmail());
$subscriberHash = MailChimp::subscriberHash($subscriber->getEmail());
$result = $this->mailchimp->patch("lists/$listId/members/$subscriberHash", $subscriber->formatMailChimp());

if (!$this->mailchimp->success()) {
Expand All @@ -174,7 +174,7 @@ public function changeEmailAddress($listId, Subscriber $newSubscriber, $oldEmail
# 2. old email address not exists in list
# 3. old or new email address doesn't exists in list

$subscriberHash = $this->mailchimp->subscriberHash($oldEmailAddress);
$subscriberHash = MailChimp::subscriberHash($oldEmailAddress);
$oldMember = $this->mailchimp->get("lists/$listId/members/$subscriberHash");
if (!$this->mailchimp->success()) {
// problem with the oldSubcriber (doest not exist, ...)
Expand Down Expand Up @@ -204,7 +204,7 @@ public function changeEmailAddress($listId, Subscriber $newSubscriber, $oldEmail
}

// add/update the new member
$subscriberHash = $this->mailchimp->subscriberHash($newSubscriber->getEmail());
$subscriberHash = MailChimp::subscriberHash($newSubscriber->getEmail());
$result = $this->mailchimp->put("lists/$listId/members/$subscriberHash", $newMember);
if (!$this->mailchimp->success()) {
$this->throwMailchimpError($this->mailchimp->getLastResponse());
Expand All @@ -220,7 +220,7 @@ public function changeEmailAddress($listId, Subscriber $newSubscriber, $oldEmail
*/
public function delete($listId, Subscriber $subscriber)
{
$subscriberHash = $this->mailchimp->subscriberHash($subscriber->getEmail());
$subscriberHash = MailChimp::subscriberHash($subscriber->getEmail());
$result = $this->mailchimp->delete("lists/$listId/members/$subscriberHash");

if (!$this->mailchimp->success()) {
Expand All @@ -244,7 +244,7 @@ public function batchSubscribe($listId, array $subscribers)
foreach ($subscriberChunks as $subscriberChunk) {
$Batch = $this->mailchimp->new_batch();
foreach ($subscriberChunk as $index => $newsubscribers) {
$subscriberHash = $this->mailchimp->subscriberHash($newsubscribers->getEmail());
$subscriberHash = MailChimp::subscriberHash($newsubscribers->getEmail());
$Batch->put("op$index", "lists/$listId/members/$subscriberHash", array_merge(
$newsubscribers->formatMailChimp(),
['status' => 'subscribed']
Expand All @@ -271,7 +271,7 @@ public function batchUnsubscribe($listId, array $emails)
foreach ($emailsChunks as $emailsChunk) {
$Batch = $this->mailchimp->new_batch();
foreach ($emailsChunk as $index => $email) {
$emailHash = $this->mailchimp->subscriberHash($email);
$emailHash = MailChimp::subscriberHash($email);
$Batch->patch("op$index", "lists/$listId/members/$emailHash", [
'status' => 'unsubscribed'
]);
Expand All @@ -297,7 +297,7 @@ public function batchDelete($listId, array $emails)
foreach ($emailsChunks as $emailsChunk) {
$Batch = $this->mailchimp->new_batch();
foreach ($emailsChunk as $index => $email) {
$emailHash = $this->mailchimp->subscriberHash($email);
$emailHash = MailChimp::subscriberHash($email);
$Batch->delete("op$index", "lists/$listId/members/$emailHash");
}
$result = $Batch->execute();
Expand Down
4 changes: 2 additions & 2 deletions tests/Subscriber/ListRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testDelete()
{
$subscriber = new Subscriber('[email protected]', ['FNAME' => 'Toto', 'LNAME' => 'TEST'], ['language' => 'fr']);
$result = $this->listRepository->delete(self::LIST_ID, $subscriber);
$this->assertEmpty($result);
$this->assertTrue($result);
}

public function testBatchSubscribe()
Expand Down Expand Up @@ -132,7 +132,7 @@ public function testMergeTags()
$this->assertNotEmpty($result2);
$this->assertEquals($result2['tag'], 'SECONDTEST');
$result3 = $this->listRepository->deleteMergeField(self::LIST_ID, $result2['merge_id']);
$this->assertEmpty($result3);
$this->assertTrue($result3);
}

public function testWebhook()
Expand Down