Skip to content

Commit

Permalink
Add getSortedAttributes method
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor Åsbrink committed Jun 10, 2022
1 parent 4e0057b commit d4b53c4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/extensions/AttributeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public function getAttributes()
return $attributes;
}

public function getSortedAttributes()
{
$attributes = $this->owner->getAttributes()
->sort("Sort", "ASC");
return $attributes;
}

public function getSortedAttributeValues()
{
$attributeSet = $this->owner->AttributeSet();
Expand Down
35 changes: 34 additions & 1 deletion src/tests/AttributeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,40 @@ public function testGetAttributes()
$attribute2->delete();
}

public function testGetSortedAttributes()
public function testGetSortedAttributesReturnsSorted()
{
$attributeSet = AttributeSet::create();
$attribute1 = Attribute::create();
$attribute2 = Attribute::create();
$attributeSet->Attributes()->add($attribute1);
$attributeSet->Attributes()->add($attribute2);
$attributeSet->write();

$object = AttributeHolder::create();
$object->AttributeSetID = $attributeSet->ID;
$object->write();
$link1 = AttributeLink::get()->filter(['AttributeID' => $attribute1->ID])->first();
$link2 = AttributeLink::get()->filter(['AttributeID' => $attribute2->ID])->first();

$link1->Sort = 1;
$link2->Sort = 2;

$link1->write();
$link2->write();

$this->assertEquals([$attribute1->ID, $attribute2->ID], $object->getSortedAttributes()->column('ID'));


$link1->Sort = 2;
$link2->Sort = 1;

$link1->write();
$link2->write();

$this->assertEquals([$attribute2->ID, $attribute1->ID], $object->getSortedAttributes()->column('ID'));
}

public function testGetSortedAttributeValues()
{
$attributeSet = AttributeSet::create();
$attribute1 = Attribute::create();
Expand Down

0 comments on commit d4b53c4

Please sign in to comment.