Skip to content

Commit

Permalink
feat: factory static to controller + change version of phpunit + add …
Browse files Browse the repository at this point in the history
…new test
  • Loading branch information
eliot488995568 committed Sep 26, 2024
1 parent f0b2254 commit 37b87de
Show file tree
Hide file tree
Showing 13 changed files with 424 additions and 88 deletions.
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":{"Unit\\MailchimpMailerTest::testSubscribe":3,"Unit\\MailjetMailerTest::testSubscribe":4,"Unit\\MailchimpMailerTest::testSubscribeWithMemberExist":3,"Unit\\MailchimpMailerTest::testSubscribeWithoutId":3,"Unit\\BrevoMailerTest::testSubscribeWithoutContactExist":3,"Unit\\BrevoMailerTest::testSubscribeWithContactExist":3,"Unit\\YmlpMailerTest::testSubscribe":3,"Unit\\YmlpMailerTest::testSubscribeWithoutCode":4},"times":{"Unit\\BrevoMailerTestCase::testSubscribe":0.003,"Unit\\BrevoMailerTestCase::testSendTransactionalEmail":0,"Unit\\BrevoMailerTestCase::testExceptionApiKey":0,"Unit\\BrevoMailerTest::testSubscribe":0.003,"Unit\\BrevoMailerTest::testSendTransactionalEmail":0,"Unit\\BrevoMailerTest::testExceptionApiKey":0,"Unit\\MailchimpMailerTest::testSubscribe":0,"Unit\\MailchimpMailerTest::testSendTransactionalEmail":0,"Unit\\MailchimpMailerTest::testExceptionApiKey":0,"Unit\\MailjetMailerTest::testSubscribe":0,"Unit\\MailjetMailerTest::testSendTransactionalEmail":0,"Unit\\MailjetMailerTest::testExceptionApiKey":0,"Unit\\YmlpMailerTest::testSubscribe":0,"Unit\\YmlpMailerTest::testSendTransactionalEmail":0,"Unit\\MailchimpMailerTest::testSubscribeWithMemberExist":0,"Unit\\MailchimpMailerTest::testSubscribeWithoutId":0,"Unit\\MailjetMailerTest::testSubscribeWithCodeError":0,"Unit\\BrevoMailerTest::testSubscribeWithoutContactExist":0,"Unit\\BrevoMailerTest::testSubscribeWithoutId":0,"Unit\\BrevoMailerTest::testSubscribeWithContactExist":0,"Unit\\YmlpMailerTest::testSubscribeWithoutCode":0}}
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
test:
vendor/bin/phpcbf -p
vendor/bin/phpstan analyse -c phpstan.neon
vendor/bin/phpunit

changelog:
git-cliff -o CHANGELOG.md
Expand Down
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@
"require-dev": {
"phpstan/phpstan": "^1.9.2",
"squizlabs/php_codesniffer": "^3.5.4",
"phpunit/phpunit": "^11.3",
"phpunit/phpunit": "^9.6",
"php-http/mock-client": "^1.6",
"nyholm/psr7": "^1.8"
},
"config": {
"allow-plugins": {
"php-http/discovery": true
}
},
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
}
}
37 changes: 37 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
colors="true"
convertDeprecationsToExceptions="false"
>
<php>
<ini name="display_errors" value="1" />
<ini name="error_reporting" value="-1" />
<server name="SHELL_VERBOSITY" value="-1" />
</php>

<testsuites>
<testsuite name="Project Test Suite">
<directory>tests/Unit</directory>
</testsuite>
</testsuites>

<coverage cacheDirectory="coverage"
includeUncoveredFiles="true"
processUncoveredFiles="true"
pathCoverage="false"
ignoreDeprecatedCodeUnits="true"
disableCodeCoverageIgnore="true">
<include>
<directory suffix=".php">src</directory>
</include>
<report>
<text outputFile="php://stdout"/>
<clover outputFile="coverage/clover.xml"/>
<cobertura outputFile="coverage/cobertura.xml"/>
</report>
</coverage>
</phpunit>
28 changes: 14 additions & 14 deletions src/SubscribeMe/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@
use SubscribeMe\Subscriber\SubscriberInterface;
use SubscribeMe\Subscriber\YmlpSubscriber;

class Factory
final class Factory implements SubscriberFactoryInterface
{
/**
* @param string $platform
* @param ClientInterface $client
* @param RequestFactoryInterface $requestFactory
* @param StreamFactoryInterface $streamFactory
* @return SubscriberInterface
*/
public static function createFor(string $platform, ClientInterface $client, RequestFactoryInterface $requestFactory, StreamFactoryInterface $streamFactory): SubscriberInterface
public function __construct(
private ClientInterface $client,
private RequestFactoryInterface $requestFactory,
private StreamFactoryInterface $streamFactory
) {
}

public function createFor(string $platform): SubscriberInterface
{
switch (strtolower($platform)) {
case 'mailjet':
return new MailjetSubscriber($client, $requestFactory, $streamFactory);
return new MailjetSubscriber($this->client, $this->requestFactory, $this->streamFactory);
case 'mailchimp':
return new MailchimpSubscriber($client, $requestFactory, $streamFactory);
return new MailchimpSubscriber($this->client, $this->requestFactory, $this->streamFactory);
case 'sendinblue':
case 'brevo':
return new BrevoSubscriber($client, $requestFactory, $streamFactory);
return new BrevoSubscriber($this->client, $this->requestFactory, $this->streamFactory);
case 'sendinblue-doi':
case 'brevo-doi':
return new BrevoDoubleOptInSubscriber($client, $requestFactory, $streamFactory);
return new BrevoDoubleOptInSubscriber($this->client, $this->requestFactory, $this->streamFactory);
case 'ymlp':
return new YmlpSubscriber($client, $requestFactory, $streamFactory);
return new YmlpSubscriber($this->client, $this->requestFactory, $this->streamFactory);
}
throw new \InvalidArgumentException('No subscriber class found for ' . $platform);
}
Expand Down
16 changes: 7 additions & 9 deletions src/SubscribeMe/Subscriber/SendInBlueSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,20 @@ protected function doSubscribe(string $uri, array $body): bool|int
// https://developers.sendinblue.com/reference/createcontact
if ($res->getStatusCode() === 200 ||
$res->getStatusCode() === 201 ||
$res->getStatusCode() === 204
$res->getStatusCode() === 204 ||
$res->getStatusCode() === 400
) {
/** @var array $body */
$body = json_decode($res->getBody()->getContents(), true);
if (isset($body['id'])) {
return (int) $body['id'];
}
}

if ($res->getStatusCode() === 400 &&
isset($body['message']) &&
$body['message'] == 'Contact already exist') {
/*
* Do not throw exception if subscriber already exists
*/
return true;
if ($res->getStatusCode() === 400 &&
isset($body['message']) &&
$body['message'] == 'Contact already exist') {
return true;
}
}
} catch (ClientExceptionInterface $exception) {
throw new CannotSubscribeException($exception->getMessage(), $exception);
Expand Down
2 changes: 0 additions & 2 deletions src/SubscribeMe/Subscriber/YmlpSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ public function subscribe(string $email, array $options, array $userConsents = [
* Do not throw exception if subscriber already exists
*/
return true;
} elseif (isset($body['Output']) && is_string($body['Output'])) {
throw new CannotSubscribeException($body['Output']);
} elseif (isset($body['Output']) &&
$body['Output'] == 'Email address already in selected groups') {
/*
Expand Down
16 changes: 16 additions & 0 deletions src/SubscribeMe/SubscriberFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace SubscribeMe;

use SubscribeMe\Subscriber\SubscriberInterface;

interface SubscriberFactoryInterface
{
/**
* @param string $platform
* @return SubscriberInterface
*/
public function createFor(string $platform): SubscriberInterface;
}
106 changes: 104 additions & 2 deletions tests/Unit/BrevoMailerTestCase.php → tests/Unit/BrevoMailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
use Http\Discovery\Psr17Factory;
use Http\Mock\Client;
use JsonException;
use Nyholm\Psr7\Response;
use PHPUnit\Framework\TestCase;
use SubscribeMe\Exception\MissingApiCredentialsException;
use SubscribeMe\Subscriber\BrevoSubscriber;
use SubscribeMe\ValueObject\EmailAddress;

class BrevoMailerTestCase extends TestCase
class BrevoMailerTest extends TestCase
{
/**
* @throws JsonException
Expand All @@ -21,6 +22,106 @@ public function testSubscribe(): void
{
$client = new Client();
$factory = new Psr17Factory();

$response = new Response(200, [], json_encode(['id' => 1], JSON_THROW_ON_ERROR));
$client->setDefaultResponse($response);

$brevoSubscriber = new BrevoSubscriber($client, $factory, $factory);

$email = '[email protected]';
$options = [
'FNAME' => 'Elly',
'LNAME' => 'Roger',
'COUNTRY' => [
'India',
'China'
]
];

$brevoSubscriber->setContactListId('3,5');
$brevoSubscriber->setApiKey('928f601b-5476-4480-8eb0-c8d979f3b68f');
$returnCode = $brevoSubscriber->subscribe($email, $options);

$requests = $client->getRequests();

$body = [
'updateEnabled' => true,
'email' => $email,
'listIds' => [3,5],
'attributes' => $options
];
$body = json_encode($body);

$this->assertEquals(1, $returnCode);
$this->assertCount(1, $requests);
$content = $requests[0]->getBody()->getContents();
$this->assertEquals('application/json', $requests[0]->getHeaders()['Content-Type'][0]);
$this->assertEquals('928f601b-5476-4480-8eb0-c8d979f3b68f', $requests[0]->getHeaders()['api-key'][0]);
$this->assertEquals('rezozero/subscribeme', $requests[0]->getHeaders()['User-Agent'][0]);
$this->assertEquals('POST', $requests[0]->getMethod());
$this->assertJsonStringEqualsJsonString($body ?: '{}', $content);
$this->assertEquals('api.brevo.com', $requests[0]->getUri()->getHost());
}

/**
* @throws JsonException
*/
public function testSubscribeWithContactExist(): void
{
$client = new Client();
$factory = new Psr17Factory();

$response = new Response(400, [], json_encode(['message' => 'Contact already exist'], JSON_THROW_ON_ERROR));
$client->setDefaultResponse($response);

$brevoSubscriber = new BrevoSubscriber($client, $factory, $factory);

$email = '[email protected]';
$options = [
'FNAME' => 'Elly',
'LNAME' => 'Roger',
'COUNTRY' => [
'India',
'China'
]
];

$brevoSubscriber->setContactListId('3,5');
$brevoSubscriber->setApiKey('928f601b-5476-4480-8eb0-c8d979f3b68f');
$returnCode = $brevoSubscriber->subscribe($email, $options);

$requests = $client->getRequests();

$body = [
'updateEnabled' => true,
'email' => $email,
'listIds' => [3,5],
'attributes' => $options
];
$body = json_encode($body);

$this->assertTrue($returnCode);
$this->assertCount(1, $requests);
$content = $requests[0]->getBody()->getContents();
$this->assertEquals('application/json', $requests[0]->getHeaders()['Content-Type'][0]);
$this->assertEquals('928f601b-5476-4480-8eb0-c8d979f3b68f', $requests[0]->getHeaders()['api-key'][0]);
$this->assertEquals('rezozero/subscribeme', $requests[0]->getHeaders()['User-Agent'][0]);
$this->assertEquals('POST', $requests[0]->getMethod());
$this->assertJsonStringEqualsJsonString($body ?: '{}', $content);
$this->assertEquals('api.brevo.com', $requests[0]->getUri()->getHost());
}

/**
* @throws JsonException
*/
public function testSubscribeWithoutId(): void
{
$client = new Client();
$factory = new Psr17Factory();

$response = new Response(400);
$client->setDefaultResponse($response);

$brevoSubscriber = new BrevoSubscriber($client, $factory, $factory);

$email = '[email protected]';
Expand All @@ -35,7 +136,7 @@ public function testSubscribe(): void

$brevoSubscriber->setContactListId('3,5');
$brevoSubscriber->setApiKey('928f601b-5476-4480-8eb0-c8d979f3b68f');
$brevoSubscriber->subscribe($email, $options);
$returnCode = $brevoSubscriber->subscribe($email, $options);

$requests = $client->getRequests();

Expand All @@ -47,6 +148,7 @@ public function testSubscribe(): void
];
$body = json_encode($body);

$this->assertFalse($returnCode);
$this->assertCount(1, $requests);
$content = $requests[0]->getBody()->getContents();
$this->assertEquals('application/json', $requests[0]->getHeaders()['Content-Type'][0]);
Expand Down
Loading

0 comments on commit 37b87de

Please sign in to comment.