-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Utilize PropertyAccess component for sorting arrays #159
Merged
akerouanton
merged 1 commit into
KnpLabs:master
from
ostrolucky:feature-property_access
Jun 13, 2017
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
tests/Test/Pager/Subscriber/Sortable/ArraySubscriberTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Test\Pager\Subscriber\Sortable; | ||
|
||
use Knp\Component\Pager\Event\ItemsEvent; | ||
use Knp\Component\Pager\Event\Subscriber\Sortable\ArraySubscriber; | ||
use Test\Tool\BaseTestCase; | ||
|
||
class ArraySubscriberTest extends BaseTestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function shouldSort() | ||
{ | ||
$array = array( | ||
array('entry' => array('sortProperty' => 2)), | ||
array('entry' => array('sortProperty' => 3)), | ||
array('entry' => array('sortProperty' => 1)), | ||
); | ||
|
||
$itemsEvent = new ItemsEvent(0, 10); | ||
$itemsEvent->target = &$array; | ||
$itemsEvent->options = array('sortFieldParameterName' => 'sort', 'sortDirectionParameterName' => 'ord'); | ||
$_GET = array('sort' => '[entry][sortProperty]', 'ord' => 'asc'); | ||
|
||
$this->assertEquals(2, $array[0]['entry']['sortProperty']); | ||
$arraySubscriber = new ArraySubscriber(); | ||
$arraySubscriber->items($itemsEvent); | ||
$this->assertEquals(1, $array[0]['entry']['sortProperty']); | ||
$_GET ['ord'] = 'desc'; | ||
$arraySubscriber->items($itemsEvent); | ||
$this->assertEquals(3, $array[0]['entry']['sortProperty']); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldSortWithCustomCallback() | ||
{ | ||
$array = array( | ||
array('name' => 'hot'), | ||
array('name' => 'cold'), | ||
array('name' => 'hot'), | ||
); | ||
|
||
$itemsEvent = new ItemsEvent(0, 10); | ||
$itemsEvent->target = &$array; | ||
$itemsEvent->options = array( | ||
'sortFieldParameterName' => 'sort', | ||
'sortDirectionParameterName' => 'ord', | ||
'sortFunction' => function (&$target, $sortField, $sortDirection) { | ||
usort($target, function($object1, $object2) use ($sortField, $sortDirection) { | ||
if ($object1[$sortField] == $object2[$sortField]) { | ||
return 0; | ||
} | ||
|
||
return ($object1[$sortField] == 'hot' ? 1 : -1) * ($sortDirection == 'asc' ? 1 : -1); | ||
}); | ||
}, | ||
); | ||
$_GET = array('sort' => '.name', 'ord' => 'asc'); | ||
|
||
$this->assertEquals('hot', $array[0]['name']); | ||
$arraySubscriber = new ArraySubscriber(); | ||
$arraySubscriber->items($itemsEvent); | ||
$this->assertEquals('cold', $array[0]['name']); | ||
$_GET['ord'] = 'desc'; | ||
$arraySubscriber->items($itemsEvent); | ||
$this->assertEquals('hot', $array[0]['name']); | ||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactoring and enhancing in the same PR makes the diff roughly hard to read. Please next time create two separate PRs, it will make our life's easier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean? Like first PR purely to refactor and second PR with actual feature pointed into first branch/PR? Sure can do so if you prefer that, even though I prefer having it in all in single PR to prevent conflicts (if there are some requested changes in first PR, it's likely second PR will have to be changed as well). In this case reading the full source is easier than diff though https://github.com/ostrolucky/knp-components/blob/db2daf3016612890b7a296dd5d44ae2bfe88856c/src/Knp/Component/Pager/Event/Subscriber/Sortable/ArraySubscriber.php
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep that's it. From my point of view it's better to not restructure the code and in the mean time change the contact(s). But don't worry, I'm not asking you to recreate two separate PRs :)