Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6712 Added tests to demonstrate, that sorting by two criterias is un… #6851

Open
wants to merge 1 commit into
base: 2.5
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/Issue6712Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;

/**
* @group issue-6712
*/
class Issue6712Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
/**
* Sorting by scalar values works as expected.
*
* @return void
*/
public function testSortingCollectionByScalar()
{
$a = new \stdClass();
$a->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()
);
}
}