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

Skip to content

Commit f5c12d0

Browse files
oallainpamil
authored andcommitted
make taxon comparable
1 parent 832c382 commit f5c12d0

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

src/Sylius/Component/Core/Model/Taxon.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\Resource\Model\TimestampableTrait;
1920
use Sylius\Component\Taxonomy\Model\Taxon as BaseTaxon;
2021
use Sylius\Component\Taxonomy\Model\TaxonTranslation;
2122

22-
class Taxon extends BaseTaxon implements TaxonInterface
23+
class Taxon extends BaseTaxon implements TaxonInterface, Comparable
2324
{
2425
use TimestampableTrait;
2526

@@ -101,4 +102,12 @@ public static function getTranslationClass(): string
101102
{
102103
return TaxonTranslation::class;
103104
}
105+
106+
/**
107+
* @inheritDoc
108+
*/
109+
public function compareTo($other): int
110+
{
111+
return $this->code === $other->getCode() ? 0 : 1;
112+
}
104113
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
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\ImageInterface;
2021
use Sylius\Component\Core\Model\ImagesAwareInterface;
22+
use Sylius\Component\Core\Model\Taxon;
2123
use Sylius\Component\Core\Model\TaxonInterface;
2224

2325
final class TaxonSpec extends ObjectBehavior
@@ -32,6 +34,11 @@ function it_implements_an_image_aware_interface(): void
3234
$this->shouldImplement(ImagesAwareInterface::class);
3335
}
3436

37+
function it_implements_doctrine_comparable(): void
38+
{
39+
$this->shouldImplement(Comparable::class);
40+
}
41+
3542
function it_initializes_an_image_collection_by_default(): void
3643
{
3744
$this->getImages()->shouldHaveType(Collection::class);
@@ -60,4 +67,16 @@ function it_returns_images_by_type(ImageInterface $image): void
6067

6168
$this->getImagesByType('thumbnail')->shouldBeLike(new ArrayCollection([$image->getWrappedObject()]));
6269
}
70+
71+
function it_is_comparable(): void
72+
{
73+
$this->setCode('test');
74+
75+
$otherTaxon = new Taxon();
76+
$otherTaxon->setCode('test');
77+
$this->compareTo($otherTaxon)->shouldReturn(0);
78+
79+
$otherTaxon->setCode('other');
80+
$this->compareTo($otherTaxon)->shouldReturn(1);
81+
}
6382
}

0 commit comments

Comments
 (0)