From eef1ffb0f43783e28d0b931f270cab6d10ece2fb Mon Sep 17 00:00:00 2001 From: Victor Häggqvist Date: Thu, 5 Mar 2015 02:31:03 +0100 Subject: init --- test/Foo.php | 10 ++++ test/SortByFieldExtensionTest.php | 109 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 test/Foo.php create mode 100644 test/SortByFieldExtensionTest.php (limited to 'test') diff --git a/test/Foo.php b/test/Foo.php new file mode 100644 index 0000000..e6e688c --- /dev/null +++ b/test/Foo.php @@ -0,0 +1,10 @@ +'')); + $twig = new Twig_Environment($loader); + $twig->addExtension(new SortByFieldExtension()); + $this->addToAssertionCount(1); + $twig->render('foo'); + } + + public function testSortArray(){ + $base = array( + array( + "name" => "Redmine", + "desc" => "Issues Tracker", + "url" => "http://www.redmine.org/", + "oss" => "GPL", + "cost" => 0 + ), + array( + "name" => "GitLab", + "desc" => "Version Control", + "url" => "https://about.gitlab.com/", + "oss" => "GPL", + "cost" => 1, + ), + array( + "name" => "Jenkins", + "desc" => "Continous Integration", + "url" => "http://jenkins-ci.org/", + "oss" => "MIT", + "cost" => 0, + ), + array( + "name" => "Piwik", + "desc" => "Web Analytics", + "url" => "http://piwik.org/", + "oss" => "GPL", + "cost" => 1 + ) + ); + + $fact = array('GitLab','Jenkins','Piwik','Redmine'); + + $filter = new SortByFieldExtension(); + $sorted = $filter->sortByFieldFilter($base,'name'); + + for ($i = 0; $i < count($fact); $i++){ + $this->assertEquals($fact[$i], $sorted[$i]['name']); + } + } + + public function testSortObjects() { + $base = array(); + $ob1 = new Foo(); + $ob1->name = "Redmine"; + $base[]=$ob1; + + $ob2 = new Foo(); + $ob2->name = "GitLab"; + $base[]=$ob2; + + $ob3 = new Foo(); + $ob3->name = "Jenkins"; + $base[]=$ob3; + + $ob4 = new Foo(); + $ob4->name = "Jenkins"; + $base[]=$ob4; + + $fact = array('GitLab','Jenkins','Jenkins','Redmine'); + + $filter = new SortByFieldExtension(); + $sorted = $filter->sortByFieldFilter($base,'name'); + + for ($i = 0; $i < count($fact); $i++){ + $this->assertEquals($fact[$i], $sorted[$i]->name); + } + } + + public function testNonArrayBase() { + $filter = new SortByFieldExtension(); + $this->setExpectedException('InvalidArgumentException'); + $filter->sortByFieldFilter(1, ''); + } + + public function testInvalidField() { + $filter = new SortByFieldExtension(); + $this->setExpectedException('Exception'); + $filter->sortByFieldFilter(array(), null); + } + + public function testUnknownField() { + $filter = new SortByFieldExtension(); + $this->setExpectedException('Exception'); + $filter->sortByFieldFilter(array(new Foo()), 'bar'); + } +} -- cgit v1.2.3