Skip to content
Snippets Groups Projects
Commit eb1bd76c authored by Matt Glaman's avatar Matt Glaman Committed by Matt Glaman
Browse files

Issue #3174993 by mglaman: Provide a data type for shipment items to support normalizations

parent 38d1d335
No related branches found
No related tags found
No related merge requests found
......@@ -93,3 +93,8 @@ services:
commerce_shipping.shipment_confirmation_mail:
class: Drupal\commerce_shipping\Mail\ShipmentConfirmationMail
arguments: ['@entity_type.manager', '@commerce.mail_handler']
commerce_shipping.normalizer.shipment_item:
class: Drupal\commerce_shipping\Normalizer\ShipmentItemNormalizer
tags:
- { name: normalizer }
<?php
namespace Drupal\commerce_shipping\Normalizer;
use Drupal\commerce_shipping\Plugin\DataType\ShipmentItem as ShipmentItemDataType;
use Drupal\serialization\Normalizer\NormalizerBase;
class ShipmentItemNormalizer extends NormalizerBase {
/**
* {@inheritdoc}
*/
protected $supportedInterfaceOrClass = ShipmentItemDataType::class;
/**
* {@inheritdoc}
*/
public function normalize($object, $format = NULL, array $context = []) {
assert($object instanceof ShipmentItemDataType);
return $object->toArray();
}
}
<?php
namespace Drupal\commerce_shipping\Plugin\DataType;
use Drupal\Core\TypedData\TypedData;
/**
* @DataType(
* id = "shipment_item",
* label = @Translation("Shipment item"),
* description = @Translation("Shipment item."),
* definition_class = "\Drupal\commerce_shipping\TypedData\ShipmentItemDataDefinition"
* )
*/
final class ShipmentItem extends TypedData {
/**
* The data value.
*
* @var \Drupal\commerce_shipping\ShipmentItem
*/
protected $value;
/**
* Gets the array representation of the shipment item.
*
* @return array|null
* The array.
*/
public function toArray() {
return $this->value ? $this->value->toArray() : NULL;
}
}
......@@ -26,7 +26,7 @@ class ShipmentItem extends FieldItemBase {
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('any')
$properties['value'] = DataDefinition::create('shipment_item')
->setLabel(t('Value'))
->setRequired(TRUE);
......
......@@ -148,4 +148,21 @@ final class ShipmentItem {
return $this->tariffCode;
}
/**
* Gets the array representation of the shipment item.
*
* @return array
* The array representation of the shipment item.
*/
public function toArray() {
return [
'order_item_id' => $this->getOrderItemId(),
'title' => $this->getTitle(),
'quantity' => $this->getQuantity(),
'weight' => $this->getWeight()->toArray(),
'declared_value' => $this->getDeclaredValue()->toArray(),
'tariff_code' => $this->getTariffCode(),
];
}
}
<?php
namespace Drupal\Tests\commerce_shipping\Kernel;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_price\Price;
use Drupal\commerce_shipping\Entity\Shipment;
use Drupal\commerce_shipping\Entity\ShippingMethod;
use Drupal\commerce_shipping\ShipmentItem;
use Drupal\Component\Serialization\Json;
use Drupal\physical\Weight;
use Drupal\profile\Entity\Profile;
/**
* Tests serialization of shipments.
*
* @group commerce_shipping
*/
class SerializationTest extends ShippingKernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'serialization',
'jsonapi',
];
/**
* Tests serialization of a shipment.
*
* @dataProvider formatProvider
*/
public function testSerialization($format) {
$user = $this->createUser(['mail' => $this->randomString() . '@example.com']);
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = Order::create([
'type' => 'default',
'state' => 'draft',
'mail' => $user->getEmail(),
'uid' => $user->id(),
'store_id' => $this->store->id(),
]);
$order->setRefreshState(Order::REFRESH_SKIP);
$order->save();
$order = $this->reloadEntity($order);
/** @var \Drupal\commerce_shipping\Entity\ShippingMethodInterface $shipping_method */
$shipping_method = ShippingMethod::create([
'name' => $this->randomString(),
'status' => 1,
'plugin' => [
'target_plugin_id' => 'flat_rate',
'target_plugin_configuration' => [
'rate_label' => 'Flat rate',
'rate_amount' => new Price('1', 'USD'),
],
],
'weight' => 1,
]);
$shipping_method->save();
$shipping_method = $this->reloadEntity($shipping_method);
/** @var \Drupal\profile\Entity\ProfileInterface $profile */
$profile = Profile::create([
'type' => 'customer',
]);
$profile->save();
$profile = $this->reloadEntity($profile);
/** @var \Drupal\commerce_shipping\Entity\Shipment $shipment */
$shipment = Shipment::create([
'type' => 'default',
'state' => 'ready',
'order_id' => $order->id(),
'title' => 'Shipment',
'amount' => new Price('12.00', 'USD'),
'shipping_profile' => $profile,
'shipping_method' => $shipping_method,
]);
$items[] = new ShipmentItem([
'order_item_id' => 10,
'title' => 'T-shirt (red, large)',
'quantity' => 2,
'weight' => new Weight('40', 'kg'),
'declared_value' => new Price('30', 'USD'),
]);
$items[] = new ShipmentItem([
'order_item_id' => 9,
'title' => 'T-shirt (blue, large)',
'quantity' => 2,
'weight' => new Weight('30', 'kg'),
'declared_value' => new Price('30', 'USD'),
]);
$shipment->setItems($items);
$shipment->save();
if ($format === 'json') {
$serializer = $this->container->get('serializer');
}
elseif ($format === 'api_json') {
$serializer = $this->container->get('jsonapi.serializer');
}
else {
throw new \InvalidArgumentException('$format must be json or api_json');
}
$normalized = $serializer->normalize($shipment, $format);
$this->assertAllPrimitives($normalized);
$json = $serializer->serialize($shipment, $format);
$decoded = Json::decode($json);
foreach ($decoded['items'] as $shipment_item_values) {
// An invalid definition will throw an exception.
new ShipmentItem($shipment_item_values['value']);
}
}
/**
* Test data provider.
*
* @return \Generator
* The test data.
*/
public function formatProvider(): \Generator {
yield ['json'];
yield ['api_json'];
}
/**
* Asserts all items are a primitive value.
*
* @param iterable $traversable
* The data.
*/
private function assertAllPrimitives($traversable) {
assert(is_array($traversable) || $traversable instanceof \Traversable);
foreach ($traversable as $value) {
if (is_array($value) || $value instanceof \Traversable) {
$this->assertAllPrimitives($value);
}
else {
$this->assertIsNotObject($value);
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment