Initial commit

This commit is contained in:
Local Administrator
2025-04-18 10:32:42 +02:00
commit b83134aca3
29643 changed files with 3045897 additions and 0 deletions

View File

@@ -0,0 +1,237 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailjet;
use Mockery;
use PHPUnit\Framework\TestCase;
/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
final class ClientTest extends TestCase
{
/**
* @var Client
*/
private $client;
/**
* @var Request
*/
private $requestMock;
public function setUp(): void
{
$this->requestMock = Mockery::mock('overload:' . Request::class);
$responseMock = Mockery::mock(Response::class);
$responseMock->shouldReceive('getData')
->andReturn(
[
'status' => 'test',
]
);
$this->requestMock->shouldReceive('call')
->andReturn($responseMock);
$this->client = new Client('testkey', 'testsecret', false);
}
public function testPost()
{
$expectedArguments = [
[
'testkey', 'testsecret'
],
'POST',
'https://api.mailjet.com/v3/REST/testresource',
[
'fkey' => 'fvalue',
],
[
'bkey' => 'bvalue',
],
'application/json',
[
'timeout' => 15,
'connect_timeout' => 2,
],
];
$this->requestMock->shouldReceive('__construct')
->once()
->withArgs($expectedArguments);
$response = $this->client->post(
['testresource', ''],
[
'filters' => ['fkey' => 'fvalue'],
'body' => ['bkey' => 'bvalue'],
]
);
$this->assertEquals(['status' => 'test',], $response->getData());
}
public function testGet()
{
$expectedArguments = [
[
'testkey', 'testsecret'
],
'GET',
'https://api.mailjet.com/v3/REST/testresource2',
[
'fkey2' => 'fvalue2',
],
[
'bkey2' => 'bvalue2',
],
'application/json',
[
'timeout' => 10,
'connect_timeout' => 20,
],
];
$this->requestMock->shouldReceive('__construct')
->once()
->withArgs($expectedArguments);
$this->client->setTimeout(10);
$this->client->setConnectionTimeout(20);
$response = $this->client->get(
['testresource2', ''],
[
'filters' => ['fkey2' => 'fvalue2'],
'body' => ['bkey2' => 'bvalue2'],
]
);
$this->assertEquals(['status' => 'test',], $response->getData());
}
public function testPut()
{
$expectedArguments = [
[
'testkey', 'testsecret'
],
'PUT',
'https://api.mailjet.com/v3/REST/testresource3',
[
'fkey3' => 'fvalue3',
],
[
'bkey3' => 'bvalue3',
],
'application/json',
[
'timeout' => 15,
'connect_timeout' => 2,
],
];
$this->requestMock->shouldReceive('__construct')
->once()
->withArgs($expectedArguments);
$response = $this->client->put(
['testresource3', ''],
[
'filters' => ['fkey3' => 'fvalue3'],
'body' => ['bkey3' => 'bvalue3'],
]
);
$this->assertEquals(['status' => 'test',], $response->getData());
}
public function testDelete()
{
$expectedArguments = [
[
'testkey', 'testsecret'
],
'DELETE',
'http://api.mailjet.com/v3/REST/testresource4',
[
'fkey4' => 'fvalue4',
],
[
'bkey4' => 'bvalue4',
],
'application/json',
[
'timeout' => 15,
'connect_timeout' => 2,
],
];
$this->requestMock->shouldReceive('__construct')
->once()
->withArgs($expectedArguments);
$this->client->setSecureProtocol(false);
$response = $this->client->delete(
['testresource4', ''],
[
'filters' => ['fkey4' => 'fvalue4'],
'body' => ['bkey4' => 'bvalue4'],
]
);
$this->assertEquals(['status' => 'test',], $response->getData());
}
public function testSetSecureProtocol()
{
$result = $this->client->setSecureProtocol(true);
$this->assertTrue($result);
$result = $this->client->setSecureProtocol(false);
$this->assertTrue($result);
$result = $this->client->setSecureProtocol(null);
$this->assertFalse($result);
}
public function testSetTimeout()
{
$this->client->setTimeout(100);
$this->assertEquals(100, $this->client->getRequestOptions()['timeout']);
$this->assertEquals(100, $this->client->getTimeout());
}
public function testSetHttpProxy()
{
$this->client->setHttpProxy(['test']);
$this->assertEquals(['test'], $this->client->getRequestOptions()['proxy']);
}
public function testSetConnectionTimeout()
{
$this->client->setConnectionTimeout(50);
$this->assertEquals(50, $this->client->getRequestOptions()['connect_timeout']);
$this->assertEquals(50, $this->client->getConnectionTimeout());
}
public function testAddRequestOption()
{
$this->client->addRequestOption('test', 'value');
$this->assertEquals('value', $this->client->getRequestOptions()['test']);
}
}

View File

@@ -0,0 +1,224 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailjet;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
final class MailjetApiv3Test extends TestCase
{
const API_BASE_URL = 'https://api.mailjet.com/';
const VERSION = 'v3';
private $publicKey = 'apikey';
private $secretKey = 'secretkey';
public function testGet()
{
$client = new Client($this->publicKey, $this->secretKey, true);
$this->assertUrl('/REST/contact', $client->get(Resources::$Contact));
$this->assertFilters(
['id' => 2], $client->get(
Resources::$Contact, [
'filters' => ['id' => 2],
], ['version' => 'v3.1']
)
);
$response = $client->get(Resources::$ContactGetcontactslists, ['id' => 2]);
$this->assertUrl('/REST/contact/2/getcontactslists', $response);
// error on sort !
$response = $client->get(
Resources::$Contact, [
'filters' => ['sort' => 'email+DESC'],
]
);
$this->assertUrl('/REST/contact', $response);
$this->assertUrl('/REST/contact/2', $client->get(Resources::$Contact, ['id' => 2]));
$this->assertUrl(
'/REST/contact/test@mailjet.com',
$client->get(Resources::$Contact, ['id' => 'test@mailjet.com'])
);
$this->assertHttpMethod('GET', $response);
$this->assertGetAuth($response);
$this->assertGetStatus(401, $response);
$this->assertGetBody(null, '', $response);
$this->assertGetData(null, '', $response);
$this->assertGetCount(null, $response);
$this->assertGetReasonPhrase('Unauthorized', $response);
$this->assertGetTotal(null, $response);
$this->assertSuccess(false, $response);
$this->assertSetSecureProtocol($client);
}
public function testPost()
{
$client = new Client($this->publicKey, $this->secretKey, true);
$email = [
'FromName' => 'Mailjet PHP test',
'FromEmail' => 'gbadi@student.42.fr',
'Text-Part' => 'Simple Email test',
'Subject' => 'PHPunit',
'Html-Part' => '<h3>Simple Email Test</h3>',
'Recipients' => [['Email' => 'test@mailjet.com']],
'MJ-custom-ID' => 'Hello ID',
];
$ret = $client->post(Resources::$Email, ['body' => $email]);
$this->assertUrl('/send', $ret);
$this->assertPayload($email, $ret);
$this->assertHttpMethod('POST', $ret);
$this->assertGetAuth($ret);
$this->assertGetStatus(401, $ret);
$this->assertGetBody(null, 'StatusCode', $ret);
$this->assertGetData(null, 'StatusCode', $ret);
$this->assertGetCount(null, $ret);
$this->assertGetReasonPhrase('Unauthorized', $ret);
$this->assertGetTotal(null, $ret);
$this->assertSuccess(false, $ret);
}
public function testPostV31()
{
$client = new Client($this->publicKey, $this->secretKey, false, ['version' => 'v3.1']);
$email = [
'Messages' => [[
'From' => ['Email' => 'test@mailjet.com', 'Name' => 'Mailjet PHP test'],
'TextPart' => 'Simple Email test',
'To' => [['Email' => 'test@mailjet.com', 'Name' => 'Test']],
]],
];
$ret = $client->post(Resources::$Email, ['body' => $email], ['timeout' => 1]);
$this->assertUrl('/send', $ret, 'v3.1');
$this->assertPayload($email, $ret);
$this->assertHttpMethod('POST', $ret);
$this->assertGetAuth($ret);
$this->assertGetStatus(401, $ret);
$this->assertGetBody(401, 'StatusCode', $ret);
$this->assertGetData(401, 'StatusCode', $ret);
$this->assertGetCount(null, $ret);
$this->assertGetReasonPhrase('Unauthorized', $ret);
$this->assertGetTotal(null, $ret);
$this->assertSuccess(false, $ret);
}
public function testClientHasOptions()
{
$client = new Client($this->publicKey, $this->secretKey, false);
$client->setTimeout(3);
$client->setConnectionTimeout(5);
$client->addRequestOption('delay', 23);
static::assertSame(3, $client->getTimeout());
static::assertSame(5, $client->getConnectionTimeout());
static::assertSame(23, $client->getRequestOptions()['delay']);
}
private function assertUrl($url, $response, $version = self::VERSION)
{
static::assertSame(self::API_BASE_URL.$version.$url, $response->getRequest()->getUrl());
}
private function assertPayload($payload, $response)
{
static::assertSame($payload, $response->getRequest()->getBody());
}
private function assertFilters($shouldBe, $response)
{
static::assertSame($shouldBe, $response->getRequest()->getFilters());
}
private function assertHttpMethod($payload, $response)
{
static::assertSame($payload, $response->getRequest()->getMethod());
}
private function assertGetAuth($response)
{
static::assertSame($this->publicKey, $response->getRequest()->getAuth()[0]);
static::assertSame($this->secretKey, $response->getRequest()->getAuth()[1]);
}
private function assertGetStatus($payload, $response)
{
static::assertSame($payload, $response->getStatus());
}
private function assertGetBody($payload, $keyName, $response)
{
$result = null;
if (false === empty($response->getBody()[$keyName])) {
$result = $response->getBody()[$keyName];
}
static::assertSame($payload, $result);
}
private function assertGetData($payload, $keyName, $response)
{
$result = null;
if (false === empty($response->getData()[$keyName])) {
$result = $response->getData()[$keyName];
}
static::assertSame($payload, $result);
}
private function assertGetCount($payload, $response)
{
static::assertSame($payload, $response->getCount());
}
private function assertGetReasonPhrase($payload, $response)
{
static::assertSame($payload, $response->getReasonPhrase());
}
private function assertGetTotal($payload, $response)
{
static::assertSame($payload, $response->getTotal());
}
private function assertSuccess($payload, $response)
{
static::assertSame($payload, $response->success());
}
private function assertSetSecureProtocol($client)
{
static::assertTrue($client->setSecureProtocol(true));
static::assertFalse($client->setSecureProtocol('not boolean type'));
}
}

View File

@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailjet;
use PHPUnit\Framework\TestCase;
/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
final class RequestTest extends TestCase
{
public function testRequest()
{
$request = new Request(['test', 'test2'], 'GET', 'test.com', ['fkey' => 'fvalue'], ['bkey' => 'bvalue'], 'test', ['rok' => 'rov']);
$this->assertEquals(['fkey' => 'fvalue'], $request->getFilters());
$this->assertEquals('GET', $request->getMethod());
$this->assertEquals('test.com', $request->getUrl());
$this->assertEquals(['bkey' => 'bvalue'], $request->getBody());
$this->assertEquals(['test', 'test2'], $request->getAuth());
}
}

View File

@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailjet;
use PHPUnit\Framework\TestCase;
/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
final class ResponseTest extends TestCase
{
public function testResponse()
{
$request = new Request(['test', 'test2'], 'GET', 'test.com', [], [], 'test', []);
$response = new Response(
$request,
new \GuzzleHttp\Psr7\Response(200, ['X-Foo' => 'Bar'], '{"Data": {"test": true}, "Count": 100, "Total": 200}')
);
$this->assertEquals(200, $response->getStatus());
$this->assertEquals(['Data' => ['test' => true], 'Count' => 100, 'Total' => 200], $response->getBody());
$this->assertEquals(['test' => true], $response->getData());
$this->assertEquals('OK', $response->getReasonPhrase());
$this->assertEquals(200, $response->getTotal());
}
public function testNullResponse()
{
$request = new Request(['test', 'test2'], 'GET', 'test.com', [], [], 'test', []);
// Response without a response interface as second parameter
$response = new Response($request, null);
$this->assertNull($response->getStatus());
$this->assertEquals([], $response->getBody());
$this->assertEquals([], $response->getData());
$this->assertNull($response->getReasonPhrase());
$this->assertNull($response->getTotal());
}
}

View File

@@ -0,0 +1,206 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailjet;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
final class test extends TestCase
{
public function assertPayload($payload, $response)
{
static::assertSame($payload, $response->request->getBody());
}
public function assertFilters($shouldBe, $response)
{
static::assertSame($shouldBe, $response->request->getFilters());
}
public function assertHttpMethod($payload, $response)
{
static::assertSame($payload, $response->request->getMethod());
}
public function assertGetAuth($payload, $response)
{
static::assertSame($payload, $response->request->getAuth()[0]);
static::assertSame($payload, $response->request->getAuth()[1]);
}
public function assertGetStatus($payload, $response)
{
static::assertSame($payload, $response->getStatus());
}
public function assertGetBody($payload, $keyName, $response)
{
static::assertSame($payload, $response->getBody()[$keyName]);
}
public function assertGetData($payload, $keyName, $response)
{
static::assertSame($payload, $response->getData()[$keyName]);
}
public function assertGetCount($payload, $response)
{
static::assertSame($payload, $response->getCount());
}
public function assertGetReasonPhrase($payload, $response)
{
static::assertSame($payload, $response->getReasonPhrase());
}
public function assertGetTotal($payload, $response)
{
static::assertSame($payload, $response->getTotal());
}
public function assertSuccess($payload, $response)
{
static::assertSame($payload, $response->success());
}
public function assertSetSecureProtocol($client)
{
static::assertTrue($client->setSecureProtocol(true));
static::assertFalse($client->setSecureProtocol('not boolean type'));
}
public function testGet()
{
$client = new Client('', '', false);
$this->assertUrl('/REST/contact', $client->get(Resources::$Contact));
$this->assertFilters(
['id' => 2], $client->get(
Resources::$Contact, [
'filters' => ['id' => 2],
], ['version' => 'v3.1']
)
);
$response = $client->get(Resources::$ContactGetcontactslists, ['id' => 2]);
$this->assertUrl('/REST/contact/2/getcontactslists', $response);
// error on sort !
$response = $client->get(
Resources::$Contact, [
'filters' => ['sort' => 'email+DESC'],
]
);
$this->assertUrl('/REST/contact', $response);
$this->assertUrl('/REST/contact/2', $client->get(Resources::$Contact, ['id' => 2]));
$this->assertUrl(
'/REST/contact/test@mailjet.com',
$client->get(Resources::$Contact, ['id' => 'test@mailjet.com'])
);
$this->assertHttpMethod('GET', $response);
$this->assertGetAuth('', $response);
$this->assertGetStatus(401, $response);
$this->assertGetBody('', '', $response);
$this->assertGetData('', '', $response);
$this->assertGetCount('', $response);
$this->assertGetReasonPhrase('Unauthorized', $response);
$this->assertGetTotal('', $response);
$this->assertSuccess('', $response);
$this->assertSetSecureProtocol($client);
}
public function testPost()
{
$client = new Client('', '', false);
$email = [
'FromName' => 'Mailjet PHP test',
'FromEmail' => 'gbadi@student.42.fr',
'Text-Part' => 'Simple Email test',
'Subject' => 'PHPunit',
'Html-Part' => '<h3>Simple Email Test</h3>',
'Recipients' => [['Email' => 'test@mailjet.com']],
'MJ-custom-ID' => 'Hello ID',
];
$ret = $client->post(Resources::$Email, ['body' => $email]);
$this->assertUrl('/send', $ret);
$this->assertPayload($email, $ret);
$this->assertHttpMethod('POST', $ret);
$this->assertGetAuth('', $ret);
$this->assertGetStatus(401, $ret);
$this->assertGetBody('', 'StatusCode', $ret);
$this->assertGetData('', 'StatusCode', $ret);
$this->assertGetCount('', $ret);
$this->assertGetReasonPhrase('Unauthorized', $ret);
$this->assertGetTotal('', $ret);
$this->assertSuccess('', $ret);
}
public function testPostV31()
{
$client = new Client('', '', false);
$email = [
'Messages' => [[
'From' => ['Email' => 'test@mailjet.com', 'Name' => 'Mailjet PHP test'],
'TextPart' => 'Simple Email test',
'To' => [['Email' => 'test@mailjet.com', 'Name' => 'Test']],
]],
];
$ret = $client->post(Resources::$Email, ['body' => $email], ['version' => 'v3.1']);
$this->assertUrl('/send', $ret, 'v3.1');
$this->assertPayload($email, $ret);
$this->assertHttpMethod('POST', $ret);
$this->assertGetAuth('', $ret);
$this->assertGetStatus(401, $ret);
$this->assertGetBody(401, 'StatusCode', $ret);
$this->assertGetData(401, 'StatusCode', $ret);
$this->assertGetCount('', $ret);
$this->assertGetReasonPhrase('Unauthorized', $ret);
$this->assertGetTotal('', $ret);
$this->assertSuccess('', $ret);
}
public function testClientHasOptions()
{
$client = new Client('', '', false);
$client->setTimeout(3);
$client->setConnectionTimeout(5);
$client->addRequestOption('delay', 23);
static::assertSame(3, $client->getTimeout());
static::assertSame(5, $client->getConnectionTimeout());
static::assertSame(23, $client->getRequestOptions()['delay']);
}
private function assertUrl($url, $response, $version = 'v3')
{
static::assertSame('https://api.mailjet.com/'.$version.$url, $response->request->getUrl());
}
}