Skip to content

Commit

Permalink
Merge branch 'master' into cake-4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Apr 21, 2020
2 parents 7ebda44 + b27f120 commit 971d5dc
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 17 deletions.
15 changes: 7 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ language: php

php:
- 7.2
- 7.3
- 7.4

services:
Expand All @@ -11,7 +10,7 @@ services:

env:
matrix:
- DB=mysql db_dsn='mysql://travis@0.0.0.0/cakephp_test'
- DB=mysql db_dsn='mysql://root@127.0.0.1/cakephp_test'
- DB=pgsql db_dsn='postgres://postgres@127.0.0.1/cakephp_test'
- DB=sqlite db_dsn='sqlite:///:memory:'

Expand All @@ -32,19 +31,19 @@ matrix:
env: PREFER_LOWEST=1

before_script:
- phpenv config-rm xdebug.ini
- if [[ $TRAVIS_PHP_VERSION != 7.2 ]]; then phpenv config-rm xdebug.ini; fi

- if [[ $PREFER_LOWEST != 1 ]]; then composer update --no-interaction; fi
- if [[ $PREFER_LOWEST == 1 ]]; then composer update --no-interaction --prefer-lowest --prefer-stable; fi

- if [[ $DB = 'mysql' ]]; then mysql -e 'CREATE DATABASE cakephp_test;'; fi
- if [[ $DB = 'pgsql' ]]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi
- if [[ $DB == 'mysql' ]]; then mysql -e 'CREATE DATABASE cakephp_test;'; fi
- if [[ $DB == 'pgsql' ]]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi

- if [[ $PHPCS = 1 ]]; then composer require cakephp/cakephp-codesniffer:^4.0; fi
- if [[ $PHPCS == 1 ]]; then composer require cakephp/cakephp-codesniffer:^4.0; fi

script:
- |
if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.2 ]]; then
if [[ $DEFAULT == 1 && $TRAVIS_PHP_VERSION == 7.2 ]]; then
mkdir -p build/logs
vendor/bin/phpunit --coverage-clover=build/logs/clover.xml
fi
Expand All @@ -56,7 +55,7 @@ script:

after_success:
- |
if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.2 ]]; then
if [[ $DEFAULT == 1 && $TRAVIS_PHP_VERSION == 7.2 ]]; then
wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.1.0/php-coveralls.phar
chmod +x php-coveralls.phar
./php-coveralls.phar
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Using [Composer][composer]:
composer require muffin/orderly
```

Then load the plugin using the shell command:
Then load the plugin using the console command:

```
bin/cake plugin load Muffin/Orderly
Expand Down Expand Up @@ -97,4 +97,4 @@ Copyright (c) 2015-Present, [Use Muffin][muffin] and licensed under [The MIT Lic
[composer]:http://getcomposer.org
[mit]:http://www.opensource.org/licenses/mit-license.php
[muffin]:http://usemuffin.com
[standards]:http://book.cakephp.org/3.0/en/contributing/cakephp-coding-conventions.html
[standards]:http://book.cakephp.org/3/en/contributing/cakephp-coding-conventions.html
13 changes: 6 additions & 7 deletions src/Model/Behavior/OrderlyBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,17 @@ public function initialize(array $config): void
*/
public function beforeFind(EventInterface $event, Query $query, ArrayObject $options, bool $primary)
{
$orders = $this->_config['orders'];

$args = [$query, $options, $primary];
if ($query->clause('order')) {
return;
}

$orders = $this->_config['orders'];
foreach ($orders as $config) {
if (
(!empty($config['callback'])
&& call_user_func_array($config['callback'], $args)
) || !$query->clause('order')
empty($config['callback'])
|| call_user_func($config['callback'], $query, $options, $primary)
) {
$query->order($config['order']);
break;
}
}
}
Expand Down
53 changes: 53 additions & 0 deletions tests/TestCase/Model/Behavior/OrderlyBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Muffin\Orderly\Test\TestCase\Model\Behavior;

use Cake\Database\ValueBinder;
use Cake\Event\Event;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
Expand Down Expand Up @@ -118,4 +119,56 @@ public function testBeforeFindQueryWithOrder()
$behavior->beforeFind($event, $query, new \ArrayObject(), true);
$this->assertEquals(1, count($query->clause('order')));
}

public function testCallback()
{
$this->Table->addBehavior('Muffin/Orderly.Orderly', [
[
'order' => 'first',
'callback' => function ($query, $options, $primary) {
if ($options['field'] === 'first' || $options['field'] === '_all_') {
return true;
}

return false;
},
],
[
'order' => 'second',
'callback' => function ($query, $options, $primary) {
if ($options['field'] === 'second' || $options['field'] === '_all_') {
return true;
}

return false;
},
],
]);
$behavior = $this->Table->behaviors()->Orderly;

$event = new Event('Model.beforeFind', $this);
$query = $this->Table->find();

$behavior->beforeFind($event, $query, new \ArrayObject(['field' => null]), true);
$this->assertNull($query->clause('order'));

$valueBinder = new ValueBinder();

$behavior->beforeFind($event, $query, new \ArrayObject(['field' => 'first']), true);
$orderClause = $query->clause('order');
$this->assertCount(1, $orderClause);
$this->assertEquals('ORDER BY first', $orderClause->sql($valueBinder));

$query = $this->Table->find();
$behavior->beforeFind($event, $query, new \ArrayObject(['field' => 'second']), true);
$orderClause = $query->clause('order');
$this->assertCount(1, $orderClause);
$this->assertEquals('ORDER BY second', $orderClause->sql($valueBinder));

$query = $this->Table->find();
$behavior->beforeFind($event, $query, new \ArrayObject(['field' => '_all_']), true);
$orderClause = $query->clause('order');
$this->assertCount(2, $orderClause);
$this->assertEquals('ORDER BY first, second', $orderClause->sql($valueBinder));
}
}

0 comments on commit 971d5dc

Please sign in to comment.