From 9590f61229a6e23c4f4ed91d34e3224804f69d40 Mon Sep 17 00:00:00 2001 From: Victor Häggqvist Date: Wed, 27 Jan 2016 00:58:22 +0100 Subject: add doctrine collection support --- test/SortByFieldExtensionTest.php | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/SortByFieldExtensionTest.php b/test/SortByFieldExtensionTest.php index e97455d..7419f60 100644 --- a/test/SortByFieldExtensionTest.php +++ b/test/SortByFieldExtensionTest.php @@ -7,7 +7,7 @@ use Snilius\Twig\SortByFieldExtension; -require_once 'Foo.php'; +require_once __DIR__.'/../vendor/autoload.php'; class SortByFieldExtensionTest extends PHPUnit_Framework_TestCase { @@ -159,6 +159,33 @@ class SortByFieldExtensionTest extends PHPUnit_Framework_TestCase { } } + public function testSortDoctrineCollection() { + $collection = new \Doctrine\Common\Collections\ArrayCollection(); + $ob1 = new Foo(); + $ob1->name = "Redmine"; + $collection->add($ob1); + + $ob2 = new Foo(); + $ob2->name = "GitLab"; + $collection->add($ob2); + + $ob3 = new Foo(); + $ob3->name = "Jenkins"; + $collection->add($ob3); + + $ob4 = new Foo(); + $ob4->name = "Piwik"; + $collection->add($ob4); + $fact = array('GitLab', 'Jenkins', 'Piwik', 'Redmine'); + + $filter = new SortByFieldExtension(); + $sorted = $filter->sortByFieldFilter($collection, 'name'); + + for ($i = 0; $i < count($fact); $i++) { + $this->assertEquals($fact[$i], $sorted[$i]->name); + } + } + public function testNonArrayBase() { $filter = new SortByFieldExtension(); $this->setExpectedException('InvalidArgumentException'); @@ -168,7 +195,13 @@ class SortByFieldExtensionTest extends PHPUnit_Framework_TestCase { public function testInvalidField() { $filter = new SortByFieldExtension(); $this->setExpectedException('Exception'); - $filter->sortByFieldFilter(array(), null); + $filter->sortByFieldFilter(array(1,2,3), null); + } + + public function testEmptyArray() { + $filter = new SortByFieldExtension(); + $unTouchedArray = $filter->sortByFieldFilter(array()); + $this->assertEquals(array(), $unTouchedArray); } public function testUnknownField() { @@ -176,4 +209,5 @@ class SortByFieldExtensionTest extends PHPUnit_Framework_TestCase { $this->setExpectedException('Exception'); $filter->sortByFieldFilter(array(new Foo()), 'bar'); } + } -- cgit v1.2.3