From 53e1c6db90c91e06cb29538865e03c7b18041cc8 Mon Sep 17 00:00:00 2001 From: Thomas Baumann Date: Mon, 27 Nov 2017 11:32:57 +0100 Subject: [PATCH] #6712 Added tests to demonstrate, that sorting by two criterias is unreliable, when first field holds objects. --- .../ORM/Functional/Ticket/Issue6712Test.php | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/Issue6712Test.php diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/Issue6712Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/Issue6712Test.php new file mode 100644 index 00000000000..b57a14a169e --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/Issue6712Test.php @@ -0,0 +1,130 @@ +foo = 10; + $a->bar = 400; + + $b = new \stdClass(); + $b->foo = 20; + $b->bar = 100; + + $c = new \stdClass(); + $c->foo = 20; + $c->bar = 300; + + $d = new \stdClass(); + $d->foo = 20; + $d->bar = 200; + + $collection = new ArrayCollection([$a, $b, $c, $d]); + + $sortedCollection = $collection->matching( + new Criteria(null, ['foo' => Criteria::ASC, 'bar' => Criteria::ASC]) + ); + + $this->assertEquals( + [0 => $a, 1 => $b, 3 => $d, 2 => $c], + $sortedCollection->toArray() + ); + } + + /** + * Sorting by date values does not work as expected. + * + * $next() will only be evaluated, if $foo1 and $foo2 are identical. + * + * @return void + */ + public function testSortingCollectionByDateTimeObjects() + { + $foo1 = new \DateTime('yesterday'); + $foo2 = new \DateTime('now'); + + $a = new \stdClass(); + $a->foo = clone $foo1; + $a->bar = 400; + + $b = new \stdClass(); + $b->foo = clone $foo2; + $b->bar = 100; + + $c = new \stdClass(); + $c->foo = clone $foo2; + $c->bar = 300; + + $d = new \stdClass(); + $d->foo = clone $foo2; + $d->bar = 200; + + $collection = new ArrayCollection([$a, $b, $c, $d]); + + $sortedCollection = $collection->matching( + new Criteria(null, ['foo' => Criteria::ASC, 'bar' => Criteria::ASC]) + ); + + $this->assertSame( + [0 => $a, 1 => $b, 3 => $d, 2 => $c], + $sortedCollection->toArray() + ); + } + + /** + * Sorting by object values does not work as expected, either. + * + * $next() will only be evaluated, if $foo1 and $foo2 are identical. + * + * @return void + */ + public function testSortingCollectionByObjects() + { + $foo1 = new \stdClass(); + $foo1->value = 1; + + $foo2 = new \stdClass(); + $foo2->value = 2; + + $a = new \stdClass(); + $a->foo = clone $foo1; + $a->bar = 400; + + $b = new \stdClass(); + $b->foo = clone $foo2; + $b->bar = 100; + + $c = new \stdClass(); + $c->foo = clone $foo2; + $c->bar = 300; + + $d = new \stdClass(); + $d->foo = clone $foo2; + $d->bar = 200; + + $collection = new ArrayCollection([$a, $b, $c, $d]); + + $sortedCollection = $collection->matching( + new Criteria(null, ['foo' => Criteria::ASC, 'bar' => Criteria::ASC]) + ); + + $this->assertSame( + [0 => $a, 1 => $b, 3 => $d, 2 => $c], + $sortedCollection->toArray() + ); + } +}