Skip to content

Commit

Permalink
Merge pull request #34 from WeareJH/feature/add-import-of-tracking-data
Browse files Browse the repository at this point in the history
feature/add-import-of-tracking-data
  • Loading branch information
AydinHassan committed Feb 22, 2016
2 parents 839fd5b + 029c684 commit 49f297d
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 17 deletions.
14 changes: 8 additions & 6 deletions src/Jh/DataImportMagento/Factory/ShipmentWriterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Jh\DataImportMagento\Factory;

use Jh\DataImportMagento\Writer\ShipmentWriter;
use Psr\Log\LoggerInterface;

/**
* Class ProductWriterFactory
Expand All @@ -13,19 +12,22 @@
class ShipmentWriterFactory
{
/**
* @param LoggerInterface $logger
* @return ShipmentWriter
*/
public function __invoke(LoggerInterface $logger)
public function __invoke()
{
$orderModel = \Mage::getModel('sales/order');
$transaction = \Mage::getModel('core/resource_transaction');

$trackingModel = \Mage::getModel('sales/order_shipment_track');
$options = [
'send_shipment_email' => (bool) \Mage::getStoreConfig('sales_email/shipment/enabled')
];

return new ShipmentWriter(
$transaction,
$orderModel,
$logger
$transaction,
$trackingModel,
$options
);
}
}
56 changes: 51 additions & 5 deletions src/Jh/DataImportMagento/Writer/ShipmentWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Ddeboer\DataImport\Exception\WriterException;
use Ddeboer\DataImport\Writer\AbstractWriter;
use Jh\DataImportMagento\Exception\MagentoSaveException;
use Jh\DataImportMagento\Options\OptionsParseTrait;

/**
* Class ShipmentWriter
Expand All @@ -12,6 +13,8 @@
*/
class ShipmentWriter extends AbstractWriter
{
use OptionsParseTrait;

/**
* @var \Mage_Core_Model_Resource_Transaction
*/
Expand All @@ -23,15 +26,41 @@ class ShipmentWriter extends AbstractWriter
protected $orderModel;

/**
* @param \Mage_Core_Model_Resource_Transaction $transactionResourceModel
* @var \Mage_Sales_Model_Order_Shipment_Track
*/
protected $trackingModel;

/**
* @var array
*/
protected $options = [
'send_shipment_email' => false
];

/**
* @param \Mage_Sales_Model_Order $order
* @param \Mage_Core_Model_Resource_Transaction $transactionResourceModel
* @param \Mage_Sales_Model_Order_Shipment_Track $trackingModel
* @param array $options
*/
public function __construct(
\Mage_Sales_Model_Order $order,
\Mage_Core_Model_Resource_Transaction $transactionResourceModel,
\Mage_Sales_Model_Order $order
\Mage_Sales_Model_Order_Shipment_Track $trackingModel,
array $options
) {
$this->transactionResourceModel = $transactionResourceModel;
$this->orderModel = $order;
$this->transactionResourceModel = $transactionResourceModel;
$this->trackingModel = $trackingModel;
$this->setOptions($options);
}

/**
* @param array $options
*/
public function setOptions(array $options)
{
$this->options = $this->parseOptions($this->options, $options);
}

/**
Expand All @@ -45,10 +74,8 @@ public function writeItem(array $item)
if (!isset($item['order_id'])) {
throw new WriterException('order_id must be set');
}

$order = clone $this->orderModel;
$order->loadByIncrementId($item['order_id']);

if (!$order->getId()) {
throw new WriterException(sprintf('Order with ID: "%s" cannot be found', $item['order_id']));
}
Expand All @@ -64,8 +91,27 @@ public function writeItem(array $item)
->addObject($shipment->getOrder())
->save();

if (array_key_exists('tracks', $item) && is_array($item['tracks'])) {
foreach ($item['tracks'] as $currentTrack) {
$tracking = clone $this->trackingModel;
$tracking->setShipment($shipment);
$tracking->setData(
[
'title' => $currentTrack['carrier'],
'number' => $currentTrack['tracking_number'],
'carrier_code' => 'custom',
'order_id' => $order->getId()
]
);
$tracking->save();
}
}
} catch (\Exception $e) {
throw new MagentoSaveException($e->getMessage());
}

if ($this->options['send_shipment_email']) {
$shipment->sendEmail(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ class ShipmentWriterFactoryTest extends \PHPUnit_Framework_TestCase
public function testFactoryReturnsInstance()
{
$factory = new ShipmentWriterFactory();
$this->assertInstanceOf('\Jh\DataImportMagento\Writer\ShipmentWriter', $factory->__invoke(
$this->getMock('\Psr\Log\LoggerInterface')
));
$this->assertInstanceOf('\Jh\DataImportMagento\Writer\ShipmentWriter', $factory->__invoke());
}
}
157 changes: 154 additions & 3 deletions test/DataImportMagentoTest/Writer/ShipmentWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class ShipmentWriterTest extends \PHPUnit_Framework_TestCase
protected $orderModel;
protected $transactionResourceModel;
protected $shipmentWriter;
protected $trackingModel;
protected $options;

public function setUp()
{
Expand All @@ -28,7 +30,20 @@ public function setUp()
->setMethods([])
->getMock();

$this->shipmentWriter = new ShipmentWriter($this->transactionResourceModel, $this->orderModel);
$this->trackingModel = $this->getMockBuilder('Mage_Sales_Model_Order_Shipment_Track')
->setMethods([])
->getMock();

$this->options = [
'send_shipment_email' => 1
];

$this->shipmentWriter = new ShipmentWriter(
$this->orderModel,
$this->transactionResourceModel,
$this->trackingModel,
$this->options
);
}

public function testExceptionIsThrownIfNoOrderId()
Expand Down Expand Up @@ -56,7 +71,104 @@ public function testExceptionIsThrownIfOrderCannotBeFound()
$this->shipmentWriter->writeItem(['order_id' => 5]);
}

public function testShipmentCanBeCreated()
public function testShipmentCanBeCreatedWithTracking()
{
$this->orderModel
->expects($this->once())
->method('loadByIncrementId')
->with(5);

$this->orderModel
->expects($this->any())
->method('getId')
->will($this->returnValue(5));

$shipment = $this->getMock('Mage_Sales_Model_Order_Shipment');

$this->orderModel
->expects($this->once())
->method('prepareShipment')
->will($this->returnValue($shipment));

$shipment
->expects($this->once())
->method('register');

$shipment
->expects($this->any())
->method('getOrder')
->will($this->returnValue($this->orderModel));

$this->orderModel
->expects($this->once())
->method('setData')
->with('is_in_process', true);

$this->transactionResourceModel
->expects($this->at(0))
->method('addObject')
->with($shipment)
->will($this->returnSelf());

$this->transactionResourceModel
->expects($this->at(1))
->method('addObject')
->with($this->orderModel)
->will($this->returnSelf());

$this->transactionResourceModel
->expects($this->once())
->method('save');

$this->trackingModel
->expects($this->once())
->method('setShipment')
->with($shipment);

$this->trackingModel
->expects($this->once())
->method('setData')
->with(
[
'title' => 'Test Carrier',
'number' => '782773742',
'carrier_code' => 'custom',
'order_id' => 5
]
);

$this->trackingModel
->expects($this->once())
->method('save');

$this->shipmentWriter->writeItem(
[
'tracks' => [
0 => [
'carrier' => 'Test Carrier',
'tracking_number' => '782773742'
]
],
'items' => [
'items' => [
0 => [
'LineNo' => 70000,
'SKU' => 'FILAM317RL1',
'Qty' => 1
],
1 => [
'LineNo' => 70001,
'SKU' => 'FILAM317RL2',
'Qty' => 1
]
]
],
'order_id' => 5
]
);
}

public function testShipmentCanBeCreatedWithoutTracking()
{
$this->orderModel
->expects($this->once())
Expand Down Expand Up @@ -106,7 +218,46 @@ public function testShipmentCanBeCreated()
->expects($this->once())
->method('save');

$this->shipmentWriter->writeItem(['order_id' => 5]);
$this->trackingModel
->expects($this->never())
->method('setShipment')
->with($shipment);

$this->trackingModel
->expects($this->never())
->method('setData')
->with(
[
'title' => 'Test Carrier',
'number' => '782773742',
'carrier_code' => 'custom',
'order_id' => 5
]
);

$this->trackingModel
->expects($this->never())
->method('save');

$this->shipmentWriter->writeItem(
[
'items' => [
'items' => [
0 => [
'LineNo' => 70000,
'SKU' => 'FILAM317RL1',
'Qty' => 1
],
1 => [
'LineNo' => 70001,
'SKU' => 'FILAM317RL2',
'Qty' => 1
]
]
],
'order_id' => 5
]
);
}

public function testMagentoSaveExceptionIsThrownIfSaveFails()
Expand Down

0 comments on commit 49f297d

Please sign in to comment.