Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a5efeb5

Browse files
committed
make product variant comparable
1 parent 3dac05c commit a5efeb5

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

src/Sylius/Component/Core/Model/ProductVariant.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515

1616
use Doctrine\Common\Collections\ArrayCollection;
1717
use Doctrine\Common\Collections\Collection;
18+
use Doctrine\Common\Comparable;
1819
use Sylius\Component\Product\Model\ProductVariant as BaseVariant;
1920
use Sylius\Component\Shipping\Model\ShippingCategoryInterface;
2021
use Sylius\Component\Taxation\Model\TaxCategoryInterface;
2122

22-
class ProductVariant extends BaseVariant implements ProductVariantInterface
23+
class ProductVariant extends BaseVariant implements ProductVariantInterface, Comparable
2324
{
2425
/** @var int */
2526
protected $version = 1;
@@ -444,4 +445,12 @@ public function removeImage(ProductImageInterface $image): void
444445
$this->images->removeElement($image);
445446
}
446447
}
448+
449+
/**
450+
* @inheritDoc
451+
*/
452+
public function compareTo($other): int
453+
{
454+
return $this->code === $other->getCode() ? 0 : 1;
455+
}
447456
}

src/Sylius/Component/Core/spec/Model/ProductVariantSpec.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515

1616
use Doctrine\Common\Collections\ArrayCollection;
1717
use Doctrine\Common\Collections\Collection;
18+
use Doctrine\Common\Comparable;
1819
use PhpSpec\ObjectBehavior;
1920
use Sylius\Component\Core\Model\ChannelInterface;
2021
use Sylius\Component\Core\Model\ChannelPricingInterface;
2122
use Sylius\Component\Core\Model\Product;
2223
use Sylius\Component\Core\Model\ProductImageInterface;
2324
use Sylius\Component\Core\Model\ProductImagesAwareInterface;
25+
use Sylius\Component\Core\Model\ProductVariant;
2426
use Sylius\Component\Core\Model\ProductVariantInterface;
2527
use Sylius\Component\Product\Model\ProductVariant as BaseProductVariant;
2628
use Sylius\Component\Resource\Model\VersionedInterface;
@@ -41,6 +43,11 @@ function it_implements_a_taxable_interface(): void
4143
$this->shouldImplement(TaxableInterface::class);
4244
}
4345

46+
function it_implements_doctrine_comparable(): void
47+
{
48+
$this->shouldImplement(Comparable::class);
49+
}
50+
4451
function it_extends_a_product_variant_model(): void
4552
{
4653
$this->shouldHaveType(BaseProductVariant::class);
@@ -273,4 +280,16 @@ function it_returns_images_by_type(ProductImageInterface $image, Product $produc
273280
$this->addImage($image);
274281
$this->getImagesByType('thumbnail')->shouldBeLike(new ArrayCollection([$image->getWrappedObject()]));
275282
}
283+
284+
function it_is_comparable(): void
285+
{
286+
$this->setCode('test');
287+
288+
$otherTaxon = new ProductVariant();
289+
$otherTaxon->setCode('test');
290+
$this->compareTo($otherTaxon)->shouldReturn(0);
291+
292+
$otherTaxon->setCode('other');
293+
$this->compareTo($otherTaxon)->shouldReturn(1);
294+
}
276295
}

0 commit comments

Comments
 (0)