Skip to content
This repository was archived by the owner on Dec 16, 2021. It is now read-only.
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
25 changes: 15 additions & 10 deletions src/Ctct/ConstantContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Ctct\Services\EmailMarketingService;
use Ctct\Services\LibraryService;
use Ctct\Services\ListService;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;

/**
* Exposes all implemented Constant Contact API functionality
Expand Down Expand Up @@ -78,16 +80,19 @@ class ConstantContact {
* Class constructor
* Registers the API key with the ConstantContact class that will be used for all API calls.
* @param string $apiKey - Constant Contact API Key
* @param ClientInterface|null $client - GuzzleHttp Client
*/
public function __construct($apiKey) {
$this->contactService = new ContactService($apiKey);
$this->emailMarketingService = new EmailMarketingService($apiKey);
$this->activityService = new ActivityService($apiKey);
$this->campaignTrackingService = new CampaignTrackingService($apiKey);
$this->contactTrackingService = new ContactTrackingService($apiKey);
$this->campaignScheduleService = new CampaignScheduleService($apiKey);
$this->listService = new ListService($apiKey);
$this->accountService = new AccountService($apiKey);
$this->libraryService = new LibraryService($apiKey);
public function __construct($apiKey, ClientInterface $client = null) {
$client = $client ?: new Client();

$this->contactService = new ContactService($apiKey, $client);
$this->emailMarketingService = new EmailMarketingService($apiKey, $client);
$this->activityService = new ActivityService($apiKey, $client);
$this->campaignTrackingService = new CampaignTrackingService($apiKey, $client);
$this->contactTrackingService = new ContactTrackingService($apiKey, $client);
$this->campaignScheduleService = new CampaignScheduleService($apiKey, $client);
$this->listService = new ListService($apiKey, $client);
$this->accountService = new AccountService($apiKey, $client);
$this->libraryService = new LibraryService($apiKey, $client);
}
}
6 changes: 4 additions & 2 deletions src/Ctct/Services/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Ctct\Exceptions\CtctException;
use Ctct\Util\Config;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Exception\TransferException;
Expand All @@ -30,10 +31,11 @@ abstract class BaseService {
/**
* Constructor with the option to to supply an alternative rest client to be used
* @param string $apiKey - Constant Contact API Key
* @param ClientInterface|null $client - GuzzleHttp Client
*/
public function __construct($apiKey) {
public function __construct($apiKey, ClientInterface $client = null) {
$this->apiKey = $apiKey;
$this->client = new Client();
$this->client = $client ?: new Client();
}

protected static function getHeadersForMultipart($accessToken) {
Expand Down
6 changes: 6 additions & 0 deletions test/Json/Contacts/error_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"error_key": "http.status.error",
"error_message": "Error message."
}
]
4 changes: 4 additions & 0 deletions test/Json/JsonLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public static function getContactsJson() {
return file_get_contents(__DIR__ . self::CONTACTS_FOLDER . "/get_contacts.json");
}

public static function getErrorResponseJson() {
return file_get_contents(__DIR__ . self::CONTACTS_FOLDER . "/error_response.json");
}

public static function getContactsNoNextJson() {
return file_get_contents(__DIR__ . self::CONTACTS_FOLDER . "/get_contacts_no_next.json");
}
Expand Down
45 changes: 21 additions & 24 deletions test/Services/ContactServiceUnitTest.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?php

use Ctct\Components\Contacts\Contact;
use Ctct\Components\ResultSet;
use Ctct\ConstantContact;
use Ctct\Exceptions\CtctException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;

class ContactServiceUnitTest extends PHPUnit_Framework_TestCase {
/**
* @var Client
* @var ConstantContact
*/
private static $client;
private static $constantContact;

public static function setUpBeforeClass() {
$contactJson = JsonLoader::getContactJson();
Expand All @@ -22,21 +22,20 @@ public static function setUpBeforeClass() {
new Response(200, array(), $contactJson),
new Response(201, array(), $contactJson),
new Response(204, array()),
new Response(400, array()),
new Response(400, array(), JsonLoader::getErrorResponseJson()),
new Response(200, array(), $contactJson)
]);
$handler = HandlerStack::create($mock);
self::$client = new Client(['handler' => $handler]);
$client = new Client(['handler' => $handler]);
self::$constantContact = new ConstantContact('API_KEY', $client);
}

public function testGetContacts() {
$response = json_decode(self::$client->request('GET', '/')->getBody(), true);
$result = new ResultSet($response['results'], $response['meta']);

$result = self::$constantContact->contactService->getContacts('ACCESS_TOKEN');
$this->assertInstanceOf('Ctct\Components\ResultSet', $result);
$this->assertEquals('c3RhcnRBdD0zJmxpbWl0PTI', $result->next);

$contact = Contact::create($result->results[1]);
$contact = $result->results[1];
$this->assertInstanceOf('Ctct\Components\Contacts\Contact', $contact);
$this->assertEquals(231, $contact->id);
$this->assertEquals("ACTIVE", $contact->status);
Expand Down Expand Up @@ -88,13 +87,12 @@ public function testGetContacts() {
}

public function testGetContactsNoNextLink() {
$response = json_decode(self::$client->request('GET', '/')->getBody(), true);
$result = new ResultSet($response['results'], $response['meta']);
$result = self::$constantContact->contactService->getContacts('ACCESS_TOKEN');

$this->assertInstanceOf('Ctct\Components\ResultSet', $result);
$this->assertEquals(null, $result->next);

$contact = Contact::create($result->results[1]);
$contact = $result->results[1];
$this->assertInstanceOf('Ctct\Components\Contacts\Contact', $contact);
$this->assertEquals(231, $contact->id);
$this->assertEquals("ACTIVE", $contact->status);
Expand Down Expand Up @@ -146,9 +144,8 @@ public function testGetContactsNoNextLink() {
}

public function testGetContact() {
$response = self::$client->request('GET', '/');
$contact = self::$constantContact->contactService->getContact('ACCESS_TOKEN', 238);

$contact = Contact::create(json_decode($response->getBody(), true));
$this->assertInstanceOf('Ctct\Components\Contacts\Contact', $contact);
$this->assertEquals(238, $contact->id);
$this->assertEquals("ACTIVE", $contact->status);
Expand Down Expand Up @@ -206,9 +203,9 @@ public function testGetContact() {
}

public function testAddContact() {
$response = self::$client->request('POST', '/');
$sourceContact = Contact::create((array) JsonLoader::getContactJson());
$contact = self::$constantContact->contactService->addContact('ACCESS_TOKEN', $sourceContact, true);

$contact = Contact::create(json_decode($response->getBody(), true));
$this->assertInstanceOf('Ctct\Components\Contacts\Contact', $contact);
$this->assertEquals(238, $contact->id);
$this->assertEquals("ACTIVE", $contact->status);
Expand Down Expand Up @@ -266,24 +263,24 @@ public function testAddContact() {
}

public function testDeleteContact() {
$response = self::$client->request('DELETE', '/');
$success = self::$constantContact->contactService->unsubscribeContact('ACCESS_TOKEN', 238);

$this->assertEquals(204, $response->getStatusCode());
$this->assertTrue($success);
}

public function testDeleteContactFailed() {
try {
self::$client->request('DELETE', '/');
self::$constantContact->contactService->unsubscribeContact('ACCESS_TOKEN', 238);
$this->fail("Delete call didn't fail");
} catch (ClientException $e) {
$this->assertEquals(400, $e->getCode());
} catch (CtctException $exception) {
$this->assertEquals(400, $exception->getCode());
}
}

public function testUpdateContact() {
$response = self::$client->request('PUT', '/');
$sourceContact = Contact::create((array) JsonLoader::getContactJson());
$contact = self::$constantContact->contactService->updateContact('ACCESS_TOKEN', $sourceContact, true);

$contact = Contact::create(json_decode($response->getBody(), true));
$this->assertInstanceOf('Ctct\Components\Contacts\Contact', $contact);
$this->assertEquals(238, $contact->id);
$this->assertEquals("ACTIVE", $contact->status);
Expand Down