diff --git a/modules/order/src/Comparator/AdjustmentComparator.php b/modules/order/src/Comparator/AdjustmentComparator.php new file mode 100644 index 00000000..0b3c5329 --- /dev/null +++ b/modules/order/src/Comparator/AdjustmentComparator.php @@ -0,0 +1,46 @@ +toArray(); + $actual_array = $actual->toArray(); + unset($expected_array['amount'], $actual_array['amount']); + if (!$actual->getAmount()->equals($expected->getAmount()) || $expected_array !== $actual_array) { + throw new ComparisonFailure( + $expected, + $actual, + $expected->getLabel(), + $actual->getLabel(), + FALSE, + \sprintf( + 'Failed asserting that Adjustment %s matches expected %s.', + $actual->getLabel(), + $expected->getLabel() + ) + ); + } + } + +} diff --git a/modules/order/tests/src/Kernel/AdjustmentTest.php b/modules/order/tests/src/Kernel/AdjustmentTest.php index 0004dfbd..56cc63fc 100644 --- a/modules/order/tests/src/Kernel/AdjustmentTest.php +++ b/modules/order/tests/src/Kernel/AdjustmentTest.php @@ -3,8 +3,10 @@ namespace Drupal\Tests\commerce_order\Kernel; use Drupal\commerce_order\Adjustment; +use Drupal\commerce_order\Comparator\AdjustmentComparator; use Drupal\commerce_price\Price; use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase; +use SebastianBergmann\Comparator\Factory as PhpUnitComparatorFactory; /** * @coversDefaultClass Drupal\commerce_order\Adjustment @@ -23,6 +25,15 @@ class AdjustmentTest extends CommerceKernelTestBase { 'commerce_order_test', ]; + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + PhpUnitComparatorFactory::getInstance()->register(new AdjustmentComparator()); + } + /** * Tests the constructor and definition checks. * diff --git a/modules/order/tests/src/Kernel/PriceSplitterTest.php b/modules/order/tests/src/Kernel/PriceSplitterTest.php index 5d1aa9d2..01267ad2 100644 --- a/modules/order/tests/src/Kernel/PriceSplitterTest.php +++ b/modules/order/tests/src/Kernel/PriceSplitterTest.php @@ -103,13 +103,16 @@ class PriceSplitterTest extends CommerceKernelTestBase { // Each order item should be discounted by half (9 USD). $amounts = $this->splitter->split($this->order, new Price('18', 'USD')); $expected_amount = new Price('9', 'USD'); - $this->assertEquals([$expected_amount, $expected_amount], array_values($amounts)); + foreach ($amounts as $amount) { + $this->assertEquals($expected_amount, $amount); + } // Same result with an explicit percentage. $amounts = $this->splitter->split($this->order, new Price('18', 'USD'), '0.5'); $expected_amount = new Price('9', 'USD'); - $this->assertEquals([$expected_amount, $expected_amount], array_values($amounts)); - + foreach ($amounts as $amount) { + $this->assertEquals($expected_amount, $amount); + } // 9.99 x 3 + 1.01 x 3 = 33. $first_unit_price = new Price('9.99', 'USD'); $second_unit_price = new Price('1.01', 'USD'); @@ -121,8 +124,10 @@ class PriceSplitterTest extends CommerceKernelTestBase { $amounts = $this->splitter->split($this->order, $amount); $first_expected_amount = new Price('4.54', 'USD'); $second_expected_amount = new Price('0.46', 'USD'); - $this->assertEquals($amount, $first_expected_amount->add($second_expected_amount)); - $this->assertEquals([$first_expected_amount, $second_expected_amount], array_values($amounts)); + $this->assertEquals($first_expected_amount->add($second_expected_amount), $amount); + $amounts = array_values($amounts); + $this->assertEquals($first_expected_amount, $amounts[0]); + $this->assertEquals($second_expected_amount, $amounts[1]); // Split an amount that has a reminder. $unit_price = new Price('69.99', 'USD'); @@ -134,8 +139,10 @@ class PriceSplitterTest extends CommerceKernelTestBase { $amounts = $this->splitter->split($this->order, $amount, '0.3'); $first_expected_amount = new Price('21.00', 'USD'); $second_expected_amount = new Price('20.99', 'USD'); - $this->assertEquals($amount, $first_expected_amount->add($second_expected_amount)); - $this->assertEquals([$first_expected_amount, $second_expected_amount], array_values($amounts)); + $this->assertEquals($first_expected_amount->add($second_expected_amount), $amount); + $amounts = array_values($amounts); + $this->assertEquals($first_expected_amount, $amounts[0]); + $this->assertEquals($second_expected_amount, $amounts[1]); } /** diff --git a/modules/payment/tests/src/Functional/DefaultPaymentAdminTest.php b/modules/payment/tests/src/Functional/DefaultPaymentAdminTest.php index 5ac8e05a..f80f13c7 100644 --- a/modules/payment/tests/src/Functional/DefaultPaymentAdminTest.php +++ b/modules/payment/tests/src/Functional/DefaultPaymentAdminTest.php @@ -174,8 +174,8 @@ class DefaultPaymentAdminTest extends CommerceBrowserTestBase { \Drupal::entityTypeManager()->getStorage('commerce_payment')->resetCache([1]); /** @var \Drupal\commerce_payment\Entity\PaymentInterface $payment */ $payment = Payment::load(1); - $this->assertEquals($payment->getOrderId(), $this->order->id()); - $this->assertEquals($payment->getAmount()->getNumber(), '100'); + $this->assertEquals($this->order->id(), $payment->getOrderId()); + $this->assertEquals('100.00', $payment->getAmount()->getNumber()); $this->assertNotEmpty($payment->getCompletedTime()); } diff --git a/modules/payment/tests/src/Functional/ManualPaymentAdminTest.php b/modules/payment/tests/src/Functional/ManualPaymentAdminTest.php index 35b1e523..41daca79 100644 --- a/modules/payment/tests/src/Functional/ManualPaymentAdminTest.php +++ b/modules/payment/tests/src/Functional/ManualPaymentAdminTest.php @@ -114,9 +114,9 @@ class ManualPaymentAdminTest extends CommerceBrowserTestBase { \Drupal::entityTypeManager()->getStorage('commerce_payment')->resetCache([1]); /** @var \Drupal\commerce_payment\Entity\PaymentInterface $payment */ $payment = Payment::load(1); - $this->assertEquals($payment->getOrderId(), $this->order->id()); - $this->assertEquals($payment->getAmount()->getNumber(), '100'); - $this->assertEquals($payment->getState()->getLabel(), 'Pending'); + $this->assertEquals($this->order->id(), $payment->getOrderId()); + $this->assertEquals('100.00', $payment->getAmount()->getNumber()); + $this->assertEquals('Pending', $payment->getState()->getLabel()); $this->drupalGet($this->paymentUri . '/add'); $this->assertSession()->pageTextContains('Manual example'); @@ -128,9 +128,9 @@ class ManualPaymentAdminTest extends CommerceBrowserTestBase { \Drupal::entityTypeManager()->getStorage('commerce_payment')->resetCache([2]); /** @var \Drupal\commerce_payment\Entity\PaymentInterface $payment */ $payment = Payment::load(2); - $this->assertEquals($payment->getOrderId(), $this->order->id()); - $this->assertEquals($payment->getAmount()->getNumber(), '100'); - $this->assertEquals($payment->getState()->getLabel(), 'Completed'); + $this->assertEquals($this->order->id(), $payment->getOrderId()); + $this->assertEquals('100.00', $payment->getAmount()->getNumber()); + $this->assertEquals('Completed', $payment->getState()->getLabel()); $this->assertNotEmpty($payment->getCompletedTime()); } diff --git a/modules/price/src/Comparator/PriceComparator.php b/modules/price/src/Comparator/PriceComparator.php new file mode 100644 index 00000000..ae26a099 --- /dev/null +++ b/modules/price/src/Comparator/PriceComparator.php @@ -0,0 +1,43 @@ +equals($expected)) { + throw new ComparisonFailure( + $expected, + $actual, + (string) $expected, + (string) $actual, + FALSE, + \sprintf( + 'Failed asserting that Price %s matches expected %s.', + $actual, + $expected + ) + ); + } + } + +} diff --git a/modules/product/tests/src/Functional/ProductAdminTest.php b/modules/product/tests/src/Functional/ProductAdminTest.php index 6923de84..3e464b10 100644 --- a/modules/product/tests/src/Functional/ProductAdminTest.php +++ b/modules/product/tests/src/Functional/ProductAdminTest.php @@ -351,8 +351,8 @@ class ProductAdminTest extends ProductBrowserTestBase { $this->container->get('entity_type.manager')->getStorage('commerce_product_variation')->resetCache([$variation->id()]); $variation = ProductVariation::load($variation->id()); - $this->assertEquals($variation->getSku(), $new_sku); - $this->assertEquals($variation->getPrice()->getNumber(), $new_price_amount); + $this->assertEquals($new_sku, $variation->getSku()); + $this->assertEquals($new_price_amount, $variation->getPrice()->getNumber()); } /** @@ -395,8 +395,8 @@ class ProductAdminTest extends ProductBrowserTestBase { $expected_variation_id = $variation->id() + 1; $variation = ProductVariation::load($expected_variation_id); - $this->assertEquals($variation->getSku(), $new_sku); - $this->assertEquals($variation->getPrice()->getNumber(), '12.00'); + $this->assertEquals($new_sku, $variation->getSku()); + $this->assertEquals('12.00', $variation->getPrice()->getNumber()); $this->assertTrue($variation->isPublished()); } diff --git a/tests/src/Functional/CommerceBrowserTestBase.php b/tests/src/Functional/CommerceBrowserTestBase.php index 56bf4551..0b59478b 100644 --- a/tests/src/Functional/CommerceBrowserTestBase.php +++ b/tests/src/Functional/CommerceBrowserTestBase.php @@ -2,11 +2,13 @@ namespace Drupal\Tests\commerce\Functional; +use Drupal\commerce_price\Comparator\PriceComparator; use Drupal\commerce_store\StoreCreationTrait; use Drupal\Tests\block\Traits\BlockCreationTrait; use Drupal\Tests\BrowserTestBase; use Drupal\Tests\commerce\Traits\CommerceBrowserTestTrait; use Drupal\Tests\commerce\Traits\DeprecationSuppressionTrait; +use SebastianBergmann\Comparator\Factory as PhpUnitComparatorFactory; /** * Provides a base class for Commerce functional tests. @@ -56,6 +58,8 @@ abstract class CommerceBrowserTestBase extends BrowserTestBase { $this->setErrorHandler(); parent::setUp(); + PhpUnitComparatorFactory::getInstance()->register(new PriceComparator()); + $this->store = $this->createStore(); $this->placeBlock('local_tasks_block'); $this->placeBlock('local_actions_block'); diff --git a/tests/src/FunctionalJavascript/CommerceWebDriverTestBase.php b/tests/src/FunctionalJavascript/CommerceWebDriverTestBase.php index 10c055ea..afe963ba 100644 --- a/tests/src/FunctionalJavascript/CommerceWebDriverTestBase.php +++ b/tests/src/FunctionalJavascript/CommerceWebDriverTestBase.php @@ -2,11 +2,13 @@ namespace Drupal\Tests\commerce\FunctionalJavascript; +use Drupal\commerce_price\Comparator\PriceComparator; use Drupal\commerce_store\StoreCreationTrait; use Drupal\FunctionalJavascriptTests\JSWebAssert; use Drupal\FunctionalJavascriptTests\WebDriverTestBase; use Drupal\Tests\block\Traits\BlockCreationTrait; use Drupal\Tests\commerce\Traits\CommerceBrowserTestTrait; +use SebastianBergmann\Comparator\Factory as PhpUnitComparatorFactory; /** * Provides a base class for Commerce functional tests. @@ -61,6 +63,8 @@ abstract class CommerceWebDriverTestBase extends WebDriverTestBase { protected function setUp() { parent::setUp(); + PhpUnitComparatorFactory::getInstance()->register(new PriceComparator()); + $this->store = $this->createStore(); $this->placeBlock('local_tasks_block'); $this->placeBlock('local_actions_block'); diff --git a/tests/src/Kernel/CommerceKernelTestBase.php b/tests/src/Kernel/CommerceKernelTestBase.php index 4cdcb7d3..efda5e8c 100644 --- a/tests/src/Kernel/CommerceKernelTestBase.php +++ b/tests/src/Kernel/CommerceKernelTestBase.php @@ -2,9 +2,11 @@ namespace Drupal\Tests\commerce\Kernel; +use Drupal\commerce_price\Comparator\PriceComparator; use Drupal\commerce_store\StoreCreationTrait; use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; use Drupal\Tests\commerce\Traits\DeprecationSuppressionTrait; +use SebastianBergmann\Comparator\Factory as PhpUnitComparatorFactory; /** * Provides a base class for Commerce kernel tests. @@ -47,7 +49,9 @@ abstract class CommerceKernelTestBase extends EntityKernelTestBase { */ protected function setUp() { parent::setUp(); + $this->setErrorHandler(); + PhpUnitComparatorFactory::getInstance()->register(new PriceComparator()); if (\Drupal::entityTypeManager()->hasDefinition('path_alias')) { $this->installEntitySchema('path_alias');