|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace MyParcelNL\Sdk\Test\Model\Consignment; |
| 6 | + |
| 7 | +use MyParcelNL\Sdk\Model\Carrier\CarrierPostNL; |
| 8 | +use MyParcelNL\Sdk\Model\Carrier\CarrierDPD; |
| 9 | +use MyParcelNL\Sdk\Model\Consignment\AbstractConsignment; |
| 10 | +use MyParcelNL\Sdk\Model\Consignment\PostNLConsignment; |
| 11 | +use MyParcelNL\Sdk\Model\Consignment\DPDConsignment; |
| 12 | +use MyParcelNL\Sdk\Test\Bootstrap\ConsignmentTestCase; |
| 13 | +use MyParcelNL\Sdk\Helper\MyParcelCollection; |
| 14 | + |
| 15 | +class MultiColloConsignmentTest extends ConsignmentTestCase |
| 16 | +{ |
| 17 | + public function provideMultiColloData(): array |
| 18 | + { |
| 19 | + return [ |
| 20 | + 'two parcels with different weight and options' => [ |
| 21 | + [ |
| 22 | + (new PostNLConsignment()) |
| 23 | + ->setApiKey('irrelevant') |
| 24 | + ->setPackageType(AbstractConsignment::PACKAGE_TYPE_PACKAGE) |
| 25 | + ->setTotalWeight(1000) |
| 26 | + ->setSignature(true), |
| 27 | + (new PostNLConsignment()) |
| 28 | + ->setApiKey('irrelevant') |
| 29 | + ->setPackageType(AbstractConsignment::PACKAGE_TYPE_PACKAGE) |
| 30 | + ->setTotalWeight(2000) |
| 31 | + ->setAgeCheck(true), |
| 32 | + ], |
| 33 | + true, // should support multi collo |
| 34 | + ], |
| 35 | + 'one parcel' => [ |
| 36 | + [ |
| 37 | + (new PostNLConsignment()) |
| 38 | + ->setApiKey('irrelevant') |
| 39 | + ->setPackageType(AbstractConsignment::PACKAGE_TYPE_PACKAGE) |
| 40 | + ->setTotalWeight(1500), |
| 41 | + ], |
| 42 | + true, |
| 43 | + ], |
| 44 | + 'three parcels' => [ |
| 45 | + [ |
| 46 | + (new PostNLConsignment()) |
| 47 | + ->setApiKey('irrelevant') |
| 48 | + ->setPackageType(AbstractConsignment::PACKAGE_TYPE_PACKAGE) |
| 49 | + ->setTotalWeight(500), |
| 50 | + (new PostNLConsignment()) |
| 51 | + ->setApiKey('irrelevant') |
| 52 | + ->setPackageType(AbstractConsignment::PACKAGE_TYPE_PACKAGE) |
| 53 | + ->setTotalWeight(750), |
| 54 | + (new PostNLConsignment()) |
| 55 | + ->setApiKey('irrelevant') |
| 56 | + ->setPackageType(AbstractConsignment::PACKAGE_TYPE_PACKAGE) |
| 57 | + ->setTotalWeight(1250), |
| 58 | + ], |
| 59 | + true, |
| 60 | + ], |
| 61 | + 'different carriers (should fail)' => [ |
| 62 | + [ |
| 63 | + (new PostNLConsignment()) |
| 64 | + ->setApiKey('irrelevant') |
| 65 | + ->setPackageType(AbstractConsignment::PACKAGE_TYPE_PACKAGE) |
| 66 | + ->setTotalWeight(1000), |
| 67 | + (new DPDConsignment()) |
| 68 | + ->setApiKey('irrelevant') |
| 69 | + ->setPackageType(AbstractConsignment::PACKAGE_TYPE_PACKAGE) |
| 70 | + ->setTotalWeight(2000), |
| 71 | + ], |
| 72 | + false, |
| 73 | + ], |
| 74 | + ]; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @dataProvider provideMultiColloData |
| 79 | + */ |
| 80 | + public function testMultiColloConsignments(array $consignments, bool $shouldSupportMultiCollo): void |
| 81 | + { |
| 82 | + $collection = new MyParcelCollection(); |
| 83 | + $exceptionThrown = false; |
| 84 | + try { |
| 85 | + $collection->addMultiColloConsignments($consignments); |
| 86 | + } catch (\Exception $e) { |
| 87 | + $exceptionThrown = true; |
| 88 | + } |
| 89 | + |
| 90 | + if (! $shouldSupportMultiCollo) { |
| 91 | + self::assertTrue($exceptionThrown, 'Expected exception for different carriers.'); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + self::assertCount(count($consignments), $collection, 'Number of consignments in collection does not match.'); |
| 96 | + |
| 97 | + // Check multi collo properties if more than one consignment |
| 98 | + if (count($consignments) > 1) { |
| 99 | + $referenceId = $collection->getConsignments()[0]->getReferenceIdentifier(); |
| 100 | + foreach ($collection as $index => $consignment) { |
| 101 | + self::assertTrue( |
| 102 | + $consignment->isPartOfMultiCollo(), |
| 103 | + sprintf('Consignment %d is not marked as multi collo.', $index + 1) |
| 104 | + ); |
| 105 | + self::assertEquals( |
| 106 | + $referenceId, |
| 107 | + $consignment->getReferenceIdentifier(), |
| 108 | + sprintf('Consignment %d does not have the correct reference identifier.', $index + 1) |
| 109 | + ); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Check that shipment options and weight are unique per consignment |
| 114 | + foreach ($collection as $index => $consignment) { |
| 115 | + self::assertEquals( |
| 116 | + $consignments[$index]->getTotalWeight(), |
| 117 | + $consignment->getTotalWeight(), |
| 118 | + sprintf('Consignment %d does not have the correct weight.', $index + 1) |
| 119 | + ); |
| 120 | + if (method_exists($consignments[$index], 'isSignature')) { |
| 121 | + self::assertEquals( |
| 122 | + $consignments[$index]->isSignature(), |
| 123 | + $consignment->isSignature(), |
| 124 | + sprintf('Consignment %d does not have the correct signature option.', $index + 1) |
| 125 | + ); |
| 126 | + } |
| 127 | + if (method_exists($consignments[$index], 'hasAgeCheck')) { |
| 128 | + self::assertEquals( |
| 129 | + $consignments[$index]->hasAgeCheck(), |
| 130 | + $consignment->hasAgeCheck(), |
| 131 | + sprintf('Consignment %d does not have the correct age check option.', $index + 1) |
| 132 | + ); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments