Skip to content

Commit

Permalink
Create one consignment
Browse files Browse the repository at this point in the history
  • Loading branch information
reindertvetter committed Sep 17, 2016
1 parent ec4a5da commit 7f34c2b
Show file tree
Hide file tree
Showing 6 changed files with 356 additions and 67 deletions.
3 changes: 1 addition & 2 deletions Tests/SendConsignments/SendConsignmentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public function testSendOneConsignment()
}

$myParcelAPI
->setA4([2,4])
->createConcepts();
}

Expand All @@ -74,7 +73,7 @@ public function additionProvider()
'person' => 'Reindert',
'company' => 'Big Sale BV',
'full_street_test' => 'Plein 1940-45 3b',
'full_street' => 'Plein 1940-45 3-b',
'full_street' => 'Plein 1940-45 3 b',
'street' => 'Plein 1940-45',
'number' => 3,
'number_suffix' => 'b',
Expand Down
172 changes: 172 additions & 0 deletions Tests/SendConsignments/SendOneConsignmentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php

/**
* Test create one concept
*
* LICENSE: This source file is subject to the Creative Commons License.
* It is available through the world-wide-web at this URL:
* http://creativecommons.org/licenses/by-nc-nd/3.0/nl/deed.en_US
*
* If you want to add improvements, please create a fork in our GitHub:
* https://github.com/myparcelnl
*
* @author Reindert Vetter <[email protected]>
* @copyright 2010-2016 MyParcel
* @license http://creativecommons.org/licenses/by-nc-nd/3.0/nl/deed.en_US CC BY-NC-ND 3.0 NL
* @link https://github.com/myparcelnl/sdk
* @since File available since Release 0.1.0
*/
namespace myparcelnl\sdk\tests\SendConsignments\
SendOneConsignmentTest;

use myparcelnl\sdk\Helper\MyParcelAPI;
use myparcelnl\sdk\Model\Repository\MyParcelConsignmentRepository;


/**
* Class SendOneConsignmentTest
* @package myparcelnl\sdk\tests\SendOneConsignmentTest
*/
class SendOneConsignmentTest extends \PHPUnit_Framework_TestCase
{

/**
* Test one shipment with createConcepts()
*/
public function testSendOneConsignment()
{
foreach ($this->additionProvider() as $consignmentTest) {

$consignment = new MyParcelConsignmentRepository();
$consignment
->setApiKey($consignmentTest['api_key'])
->setCountry($consignmentTest['cc'])
->setPerson($consignmentTest['person'])
->setCompany($consignmentTest['company'])
->setFullStreet($consignmentTest['full_street_test'])
->setPostalCode($consignmentTest['postal_code'])
->setPackageType(1)
->setCity($consignmentTest['city'])
->setEmail('[email protected]')
->setPhone($consignmentTest['phone']);

if (key_exists('package_type', $consignmentTest))
$consignment->setPackageType($consignmentTest['package_type']);

if (key_exists('large_format', $consignmentTest))
$consignment->setLargeFormat($consignmentTest['large_format']);

if (key_exists('only_recipient', $consignmentTest))
$consignment->setOnlyRecipient($consignmentTest['only_recipient']);

if (key_exists('signature', $consignmentTest))
$consignment->setSignature($consignmentTest['signature']);

if (key_exists('return', $consignmentTest))
$consignment->setReturn($consignmentTest['return']);

if (key_exists('label_description', $consignmentTest))
$consignment->setLabelDescription($consignmentTest['label_description']);

/**
* Create concept
*/
$consignment->createConcept();

$this->assertEquals(true, $consignment->getId() > 1, 'No id found');
$this->assertEquals($consignmentTest['api_key'], $consignment->getApiKey(), 'getApiKey()');
$this->assertEquals($consignmentTest['cc'], $consignment->getCountry(), 'getCountry()');
$this->assertEquals($consignmentTest['person'], $consignment->getPerson(), 'getPerson()');
$this->assertEquals($consignmentTest['company'], $consignment->getCompany(), 'getCompany()');
$this->assertEquals($consignmentTest['full_street'], $consignment->getFullStreet(), 'getFullStreet()');
$this->assertEquals($consignmentTest['number'], $consignment->getNumber(), 'getNumber()');
$this->assertEquals($consignmentTest['number_suffix'], $consignment->getNumberSuffix(), 'getNumberSuffix()');
$this->assertEquals($consignmentTest['postal_code'], $consignment->getPostalCode(), 'getPostalCode()');
$this->assertEquals($consignmentTest['city'], $consignment->getCity(), 'getCity()');
$this->assertEquals($consignmentTest['phone'], $consignment->getPhone(), 'getPhone()');

if (key_exists('package_type', $consignmentTest))
$this->assertEquals($consignmentTest['package_type'], $consignment->getPackageType(), 'getPackageType()');

if (key_exists('large_format', $consignmentTest))
$this->assertEquals($consignmentTest['large_format'], $consignment->isLargeFormat(), 'isLargeFormat()');

if (key_exists('only_recipient', $consignmentTest))
$this->assertEquals($consignmentTest['only_recipient'], $consignment->isOnlyRecipient(), 'isOnlyRecipient()');

if (key_exists('signature', $consignmentTest))
$this->assertEquals($consignmentTest['signature'], $consignment->isSignature(), 'isSignature()');

if (key_exists('return', $consignmentTest))
$this->assertEquals($consignmentTest['return'], $consignment->isReturn(), 'isReturn()');

if (key_exists('label_description', $consignmentTest))
$this->assertEquals($consignmentTest['label_description'], $consignment->getLabelDescription(), 'getLabelDescription()');
}
}

/**
* Data for the test
*
* @return array
*/
public function additionProvider()
{
return [
[
'api_key' => 'MYSNIzQWqNrYaDeFxJtVrujS9YEuF9kiykBxf8Sj',
'cc' => 'NL',
'person' => 'Reindert',
'company' => 'Big Sale BV',
'full_street_test' => 'Plein 1940-45 3b',
'full_street' => 'Plein 1940-45 3 b',
'street' => 'Plein 1940-45',
'number' => 3,
'number_suffix' => 'b',
'postal_code' => '2231JE',
'city' => 'Rijnsburg',
'phone' => '123456',
],
[
'api_key' => 'a5cbbf2a81e3a7fe51752f51cedb157acffe6f1f',
'cc' => 'NL',
'person' => 'Piet',
'company' => 'Mega Store',
'full_street_test' => 'Koestraat 55',
'full_street' => 'Koestraat 55',
'street' => 'Koestraat',
'number' => 55,
'number_suffix' => '',
'postal_code' => '2231JE',
'city' => 'Katwijk',
'phone' => '123-45-235-435',
'package_type' => 1,
'large_format' => false,
'only_recipient' => false,
'signature' => false,
'return' => false,
'label_description' => 'Label description',
],
[
'api_key' => 'a5cbbf2a81e3a7fe51752f51cedb157acffe6f1f',
'cc' => 'NL',
'person' => 'Piet',
'company' => 'Mega Store',
'full_street_test' => 'Koestraat 55',
'full_street' => 'Koestraat 55',
'street' => 'Koestraat',
'number' => 55,
'number_suffix' => '',
'postal_code' => '2231JE',
'city' => 'Katwijk',
'phone' => '123-45-235-435',
'package_type' => 1,
'large_format' => true,
'only_recipient' => true,
'signature' => true,
'return' => true,
'label_description' => 1234,
]
];
}
}
58 changes: 6 additions & 52 deletions src/Helper/MyParcelAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,6 @@
*/
class MyParcelAPI
{
/**
* Supported request types.
*/
const REQUEST_TYPE_CREATE_CONSIGNMENT = 'shipments';

/**
* API headers
*/
const REQUEST_HEADER_SHIPMENT = 'Content-Type: application/vnd.shipment+json; ';
const REQUEST_HEADER_RETURN = 'Content-Type: application/vnd.return_shipment+json; ';

/**
* @var array
*/
Expand Down Expand Up @@ -99,20 +88,16 @@ public function addConsignment(MyParcelConsignmentRepository $consignment)

public function createConcepts()
{
$consignmentsSortedByKey = $this->getConsignmentsOrderByKey();
$consignmentsSortedByKey = $this->getConsignmentsSortedByKey();

foreach ($consignmentsSortedByKey as $key => $consignments) {

$data = $this->getData($consignments);
$data = $this->apiEncode($consignments);
$request = new MyParcelRequest();
$request
->setRequestParameters($data, $key, 'shipments', self::REQUEST_HEADER_SHIPMENT)
->setRequestParameters($data, $key, 'shipments', $request::REQUEST_HEADER_SHIPMENT)
->sendRequest();

if($request->getError()) {
throw new \Exception('Error in createConcepts(): ' . $request->getError());
}

var_dump($request->getResult());
}

Expand Down Expand Up @@ -160,43 +145,12 @@ public function setA4($positions = 1)
return $this;
}

private function getData($consignments)
private function apiEncode($consignments)
{
$data = [];
/** @var $consignment MyParcelConsignmentRepository */
foreach ($consignments as $consignment) {
$aConsignment = array(
'recipient' => array(
'cc' => $consignment->getCountry(),
'person' => $consignment->getPerson(),
'company' => $consignment->getCompany(),
'postal_code' => $consignment->getPostalCode(),
'street' => $consignment->getStreet(),
'number' => $consignment->getNumber(),
'number_suffix' => $consignment->getNumberSuffix(),
'city' => $consignment->getCity(),
'email' => $consignment->getEmail(),
'phone' => $consignment->getPhone(),
),
'options' => [
'package_type' => $consignment->getPackageType(),
'large_format' => $consignment->isLargeFormat() ? 1 : 0,
'only_recipient' => $consignment->isOnlyRecipient() ? 1 : 0,
'signature' => $consignment->isSignature() ? 1 : 0,
'return' => $this->return ? 1 : 0,
'label_description' => $consignment->getLabelDescription(),
'delivery_type' => $consignment->getDeliveryType(),
],
'carrier' => 1
);

if ($consignment->getInsurance() > 1) {
$aConsignment['options']['insurance'] = ['amount' => $consignment->getInsurance(), 'currency' => 'EUR'];
}
if($consignment->getDeliveryDate())
$aConsignment['options']['delivery_date'] = $consignment->getDeliveryDate();

$data['data']['shipments'][] = $aConsignment;
$data['data']['shipments'][] = $consignment->apiEncode();

}
return json_encode($data);
Expand Down Expand Up @@ -227,7 +181,7 @@ private function getPositions($start)
return implode(';', $aPositions);
}

private function getConsignmentsOrderByKey()
private function getConsignmentsSortedByKey()
{
$aConsignments = [];
/** @var $consignment MyParcelConsignment */
Expand Down
30 changes: 27 additions & 3 deletions src/Model/MyParcelConsignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class MyParcelConsignment
*/
private $apiKey;

/**
* @var null
*/
private $barcode = null;

/**
* @var int
*/
Expand Down Expand Up @@ -211,6 +216,25 @@ public function setApiKey($apiKey)
return $this;
}

/**
* @return null
*/
public function getBarcode()
{
return $this->barcode;
}

/**
* @param null $barcode
*
* @return $this
*/
public function setBarcode($barcode)
{
$this->barcode = $barcode;
return $this;
}

/**
* Get the status of the consignment
*
Expand Down Expand Up @@ -727,13 +751,13 @@ public function getLabelDescription()
*
* Required: No
*
* @param mixed $label_description
* @param string $label_description
*
* @return $this
*/
public function setLabelDescription($label_description)
{
$this->label_description = $label_description;
$this->label_description = (string)$label_description;
return $this;
}

Expand Down Expand Up @@ -839,4 +863,4 @@ public function addItems($item)
$this->items[] = $item;
}

}
}
Loading

0 comments on commit 7f34c2b

Please sign in to comment.