Skip to content

Commit

Permalink
feat: add redjepakketje
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Nov 8, 2021
1 parent 7c5c3c6 commit 43cfb5b
Show file tree
Hide file tree
Showing 61 changed files with 2,686 additions and 1,978 deletions.
784 changes: 20 additions & 764 deletions README.md

Large diffs are not rendered by default.

86 changes: 86 additions & 0 deletions Tests/DropOffPointTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

use MyParcelNL\Sdk\src\Factory\ConsignmentFactory;
use MyParcelNL\Sdk\src\Helper\MyParcelCollection;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierRedJePakketje;
use MyParcelNL\Sdk\src\Model\Consignment\DropOffPoint;
use MyParcelNL\Sdk\src\Services\Web\RedJePakketjeDropOffPointWebService;
use PHPUnit\Framework\TestCase;

class DropOffPointTest extends TestCase
{
public function provideTestDropOffPointData(): array
{
return [
'RedJePakketje' => [
[
'cc' => 'NL',
'company' => 'MyParcel',
'person' => 'Mr. Parcel',
'full_street' => 'Meander 631',
'postal_code' => '6825ME',
'city' => 'Arnhem',
'phone' => '123456',
],
],
];
}

/**
* @throws \MyParcelNL\Sdk\src\Exception\AccountNotActiveException
* @throws \MyParcelNL\Sdk\src\Exception\ApiException
* @throws \MyParcelNL\Sdk\src\Exception\MissingFieldException
* @throws \Exception
* @dataProvider provideTestDropOffPointData
*/
public function testDropOffPoint(array $consignmentTest): void
{
$consignment = ConsignmentFactory::createByCarrierId(CarrierRedJePakketje::getId())
->setApiKey(getenv('API_KEY'))
->setCountry($consignmentTest['cc'])
->setPerson($consignmentTest['person'])
->setCompany($consignmentTest['company'])
->setFullStreet($consignmentTest['full_street'])
->setPostalCode($consignmentTest['postal_code'])
->setCity($consignmentTest['city'])
->setEmail('[email protected]')
->setPhone($consignmentTest['phone']);

$dropOffPoints = (new RedJePakketjeDropOffPointWebService())
->setApiKey(getenv('API_KEY'))
->getDropOffPoints($consignmentTest['postal_code']);

self::assertNotEmpty($dropOffPoints);

$dropOffPoint = (new DropOffPoint())
->setBoxNumber()
->setCc($dropOffPoints[0]['cc'] ?? null)
->setCity($dropOffPoints[0]['city'] ?? null)
->setLocationCode($dropOffPoints[0]['location_code'] ?? null)
->setLocationName($dropOffPoints[0]['location_name'] ?? null)
->setNumber($dropOffPoints[0]['number'] ?? null)
->setNumberSuffix($dropOffPoints[0]['number_suffix'] ?? null)
->setPostalCode($dropOffPoints[0]['postal_code'] ?? null)
->setRegion($dropOffPoints[0]['region'] ?? null)
->setRetailNetworkId($dropOffPoints[0]['retail_network_id'] ?? null)
->setState($dropOffPoints[0]['state'] ?? null)
->setStreet($dropOffPoints[0]['street'] ?? null);

$consignment->setDropOffPoint($dropOffPoint);

$collection = new MyParcelCollection();
$collection->addConsignment($consignment);
$collection->setLinkOfLabels();

self::assertEquals(
true,
preg_match(
"#^https://api(\.[a-z]+)?\.myparcel\.nl/pdfs#",
$collection->getLinkOfLabels()
),
'Can\'t get link of PDF'
);
}
}
2 changes: 1 addition & 1 deletion Tests/SendConsignments/SendAgeCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function testSendAgeCheck()

/** @var AbstractConsignment $consignment */
$consignment = $myParcelCollection->getOneConsignment();
$this->assertEquals(true, preg_match("#^3SMYPA#", $consignment->getBarcode()), 'Barcode is not set');
$this->assertStringMatchesFormat('3SMYPA', preg_match("#^3SMYPA#", $consignment->getBarcode()), 'Barcode is not set');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use MyParcelNL\Sdk\src\Factory\ConsignmentFactory;
use MyParcelNL\Sdk\src\Helper\MyParcelCollection;
use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;
use MyParcelNL\Sdk\src\Model\Consignment\BaseConsignment;
use MyParcelNL\Sdk\src\Model\Consignment\PostNLConsignment;

/**
Expand Down Expand Up @@ -61,9 +62,9 @@ public function testSendReferenceIdentifierConsignment()
$myParcelCollection->createConcepts();

/**
* @var $savedConsignment AbstractConsignment
* @var AbstractConsignment $savedConsignment
*/
$savedConsignment = (new AbstractConsignment())
$savedConsignment = (new BaseConsignment())
->setApiKey($consignmentTest['api_key'])
->setReferenceId($consignmentTest['reference_identifier']);
$savedCollection = (new MyParcelCollection())
Expand Down
27 changes: 27 additions & 0 deletions Tests/Services/AccountServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Sdk\src\Services;

use MyParcelNL\Sdk\src\Services\Web\AccountWebService;
use PHPUnit\Framework\TestCase;

class AccountServiceTest extends TestCase
{
/**
* @throws \MyParcelNL\Sdk\src\Exception\ApiException
* @throws \MyParcelNL\Sdk\src\Exception\AccountNotActiveException
* @throws \MyParcelNL\Sdk\src\Exception\MissingFieldException
*/
public function testGetAccount(): void
{
$result = (new AccountWebService())
->setApiKey(getenv('API_KEY'))
->getAccount();

self::assertArrayHasKey('id', $result->toArray());
self::assertArrayHasKey('platform_id', $result->toArray());
self::assertArrayHasKey('shops', $result->toArray());
}
}
47 changes: 47 additions & 0 deletions Tests/Services/CarrierConfigurationServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Sdk\src\Services;

use MyParcelNL\Sdk\src\Model\Account\Shop;
use MyParcelNL\Sdk\src\Services\Web\AccountWebService;
use MyParcelNL\Sdk\src\Services\Web\CarrierConfigurationWebService;
use MyParcelNL\Sdk\src\Services\Web\CarrierOptionsWebService;
use PHPUnit\Framework\TestCase;

class CarrierConfigurationServiceTest extends TestCase
{
/**
* @throws \MyParcelNL\Sdk\src\Exception\ApiException
* @throws \MyParcelNL\Sdk\src\Exception\AccountNotActiveException
* @throws \MyParcelNL\Sdk\src\Exception\MissingFieldException
*/
public function testGetConfiguration(): void
{
$accountService = (new AccountWebService())->setApiKey(getenv('API_KEY'));
$carrierOptionsService = (new CarrierOptionsWebService())->setApiKey(getenv('API_KEY'));

$accountService->getAccount()
->getShops()
->first(static function (Shop $shop) use ($carrierOptionsService) {
$carriers = $carrierOptionsService->getCarrierOptions($shop->getId());
$carrierConfigurationService = (new CarrierConfigurationWebService())->setApiKey(getenv('API_KEY'));

foreach ($carriers as $carrierOptions) {
$carrierId = $carrierOptions->getCarrierId();
if (5 !== $carrierId) {
continue;
}
$result = $carrierConfigurationService->getCarrierConfigurations($shop->getId(), $carrierId);
break;
}

if (! isset($result)) {
throw new \Exception('carrier with carrier id 5 must be present to assert');
}

self::assertNotEmpty($result->getDefaultDropOffPointExternalIdentifier());
});
}
}
32 changes: 32 additions & 0 deletions Tests/Services/CarrierOptionsServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Sdk\src\Services;

use MyParcelNL\Sdk\src\Model\Account\Shop;
use MyParcelNL\Sdk\src\Services\Web\AccountWebService;
use MyParcelNL\Sdk\src\Services\Web\CarrierOptionsWebService;
use PHPUnit\Framework\TestCase;

class CarrierOptionsServiceTest extends TestCase
{
/**
* @throws \MyParcelNL\Sdk\src\Exception\ApiException
* @throws \MyParcelNL\Sdk\src\Exception\AccountNotActiveException
* @throws \MyParcelNL\Sdk\src\Exception\MissingFieldException
*/
public function testGetAccounts(): void
{
$accountService = (new AccountWebService())->setApiKey(getenv('API_KEY'));
$carrierOptionsService = (new CarrierOptionsWebService())->setApiKey(getenv('API_KEY'));

$accountService->getAccount()
->getShops()
->first(static function (Shop $shop) use ($carrierOptionsService) {
$result = $carrierOptionsService->getCarrierOptions($shop->getId());

self::assertEquals('MyParcelNL\Sdk\src\Model\Account\CarrierOptions', get_class($result->first()));
});
}
}
43 changes: 43 additions & 0 deletions Tests/Services/RedJePakketjeDropOffPointServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Sdk\Tests\Services;

use MyParcelNL\Sdk\src\Exception\ApiException;
use MyParcelNL\Sdk\src\Services\Web\RedJePakketjeDropOffPointWebService;
use PHPUnit\Framework\TestCase;

class RedJePakketjeDropOffPointServiceTest extends TestCase
{
/**
* @throws \MyParcelNL\Sdk\src\Exception\AccountNotActiveException
* @throws \MyParcelNL\Sdk\src\Exception\ApiException
* @throws \MyParcelNL\Sdk\src\Exception\MissingFieldException
*/
public function testGetDropOffPoints(): void
{
$service = (new RedJePakketjeDropOffPointWebService())->setApiKey(getenv('API_KEY'));
$result = $service->getDropOffPoints('6825ME');

self::assertNotEmpty($result);
}

/**
* @throws \MyParcelNL\Sdk\src\Exception\ApiException
* @throws \MyParcelNL\Sdk\src\Exception\AccountNotActiveException
* @throws \MyParcelNL\Sdk\src\Exception\MissingFieldException
* @throws \Exception
*/
public function testGetDropOffPoint(): void
{
$service = (new RedJePakketjeDropOffPointWebService())->setApiKey(getenv('API_KEY'));
$result = $service->getDropOffPoint('e02158ab-7307-434b-956c-0aeb60ef1046');

if ($result) {
self::assertEquals('e02158ab-7307-434b-956c-0aeb60ef1046', $result->getLocationCode());
} else {
Throw new \Exception('Not one drop off point returned for external identifier');
}
}
}
91 changes: 91 additions & 0 deletions Tests/Validator/AbstractValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Validator;

use MyParcelNL\Sdk\src\Exception\ValidationException;
use MyParcelNL\Sdk\src\Rule\Rule;
use MyParcelNL\Sdk\src\Validator\AbstractValidator;
use PHPUnit\Framework\TestCase;

class TestRule extends Rule
{
/**
* @param bool $validationSubject
*/
public function validate($validationSubject): void
{
if ($validationSubject) {
$this->addError('Error');
}
}
}

class AbstractValidatorTest extends TestCase
{
/**
* @return array
*/
public function provideValidatorData(): array
{
return [
[
[
new TestRule(),
new TestRule(),
new TestRule(),
],
],
];
}

/**
* @param array $rules
*
* @throws \Exception
* @dataProvider provideValidatorData
*/
public function testValidator(array $rules): void
{
$testValidator = $this->createAbstractValidator();
$testValidator::$staticRules = $rules;

(new $testValidator())
->validateAll(false)
->report();
// Expecting no error to be thrown

try {
(new $testValidator())
->validateAll(true)
->report();
} catch (ValidationException $e) {
self::assertSame(['Error', 'Error', 'Error'], $e->getErrors());
$this->addToAssertionCount(1);
}

// Make sure the catch assertion has been executed.
self::assertEquals(1, $this->getNumAssertions());
}

/**
* @return \MyParcelNL\Sdk\src\Validator\AbstractValidator
*/
protected function createAbstractValidator()
{
return new class extends AbstractValidator {
/**
* @see https://stackoverflow.com/a/49038436/10225966
* @var \MyParcelNL\Sdk\src\Rule\Rule[]
*/
public static $staticRules;

/**
* @return \MyParcelNL\Sdk\src\Rule\Rule[]
*/
protected function getRules(): array
{
return self::$staticRules;
}
};
}
}
14 changes: 8 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "myparcelnl/sdk",
"version": "v6.0.0-beta.1",
"version": "v6.0.0-beta.2",
"description": "This package is designed to send and receive data from MyParcel by means of an API.",
"homepage": "https://www.myparcel.nl",
"keywords": [
Expand Down Expand Up @@ -35,12 +35,14 @@
"php": ">=7.1.0"
},
"require-dev": {
"fakerphp/faker": "^1.14",
"phpunit/phpunit": "^5.5"
"fakerphp/faker": "^1.15.0",
"phpunit/phpunit": "^5.5",
"psr/container": "~1.0.0"
},
"autoload": {
"files": [
"src/AutoLoader.php"
]
"psr-4": {
"MyParcelNL\\Sdk\\src\\": "src",
"MyParcelNL\\Sdk\\": "src"
}
}
}
Loading

0 comments on commit 43cfb5b

Please sign in to comment.