Skip to content

Commit

Permalink
Merge pull request #4 from UseMuffin/issue-3
Browse files Browse the repository at this point in the history
Fix generation of ORDER clause based on callback.
  • Loading branch information
ADmad authored Apr 21, 2020
2 parents 969c459 + f07af9a commit b27f120
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 44 deletions.
30 changes: 17 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ language: php

php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.4

sudo: false
services:
- mysql
- postgresql

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://[email protected]/cakephp_test'
- DB=sqlite db_dsn='sqlite:///:memory:'

Expand All @@ -21,32 +21,36 @@ matrix:
fast_finish: true

include:
- php: 7.0
- php: 7.2
env: PHPCS=1 DEFAULT=0

- php: 7.0
- php: 7.2
env: PHPSTAN=1 DEFAULT=0

- php: 5.6
env: PREFER_LOWEST=1

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

- composer install --prefer-dist --no-interaction
- if [[ $PREFER_LOWEST != 1 ]]; then composer install --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 [[ $PHPCS = 1 ]]; then composer require cakephp/cakephp-codesniffer:"^3.0"; fi

script:
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.0 ]]; then vendor/bin/phpunit; fi
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then vendor/bin/phpunit --coverage-clover=clover.xml; fi
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.4 ]]; then vendor/bin/phpunit; fi
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.4 ]]; then vendor/bin/phpunit --coverage-clover=clover.xml; fi

- if [[ $PHPCS = 1 ]]; then vendor/bin/phpcs -n -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi

- if [[ $PHPSTAN = 1 ]]; then composer require --dev phpstan/phpstan:^0.9 && vendor/bin/phpstan analyse -l 5 src; fi
- if [[ $PHPSTAN = 1 ]]; then composer require --dev phpstan/phpstan:^0.12 && vendor/bin/phpstan analyse -l 5 src; fi

after_success:
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then bash <(curl -s https://codecov.io/bash); fi
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.4 ]]; then bash <(curl -s https://codecov.io/bash); fi

notifications:
email: false
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,12 @@ 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
```

or by manually adding statement shown below to your app's `config/bootstrap.php`:

```php
Plugin::load('Muffin/Orderly');
```

## What is does

Orderly allow you to set default `ORDER` clause for your table queries, similar
Expand Down Expand Up @@ -97,10 +91,10 @@ http://github.com/usemuffin/orderly/issues

## License

Copyright (c) 2015, [Use Muffin][muffin] and licensed under [The MIT License][mit].
Copyright (c) 2015-Present, [Use Muffin][muffin] and licensed under [The MIT License][mit].

[cakephp]:http://cakephp.org
[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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"require-dev": {
"cakephp/cakephp": "^3.4",
"cakephp/chronos": "^1.1",
"phpunit/phpunit": "^5.7.14|^6.0"
},
"autoload": {
Expand Down
14 changes: 7 additions & 7 deletions src/Model/Behavior/OrderlyBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ public function initialize(array $config)
*/
public function beforeFind(Event $event, Query $query, ArrayObject $options, $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')
if (
empty($config['callback'])
|| call_user_func($config['callback'], $query, $options, $primary)
) {
$query->order($config['order']);
break;
}
}
}
Expand Down
78 changes: 65 additions & 13 deletions tests/TestCase/Model/Behavior/OrderlyBehaviorTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
namespace Muffin\Orderly\Test\TestCase\Model\Behavior;

use Cake\Database\ValueBinder;
use Cake\Event\Event;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

class OrderlyBehaviorTest extends TestCase
{

public $fixtures = [
'core.Posts',
];
Expand All @@ -34,8 +34,8 @@ public function testInitialize()
$expected = [
[
'order' => $this->Table->aliasField($this->Table->getDisplayField()),
'callback' => null
]
'callback' => null,
],
];
$this->assertEquals(
$expected,
Expand All @@ -48,8 +48,8 @@ public function testInitialize()
$expected = [
[
'order' => 'published',
'callback' => null
]
'callback' => null,
],
];
$this->assertEquals(
$expected,
Expand All @@ -65,8 +65,8 @@ public function testInitialize()
$expected = [
[
'order' => 'Posts.title',
'callback' => $callback
]
'callback' => $callback,
],
];
$this->assertEquals(
$expected,
Expand All @@ -76,18 +76,18 @@ public function testInitialize()
$this->Table->removeBehavior('Orderly');
$this->Table->addBehavior('Muffin/Orderly.Orderly', [
[],
['order' => 'published', 'callback' => $callback]
['order' => 'published', 'callback' => $callback],
]);

$expected = [
[
'order' => 'Posts.title',
'callback' => null
'callback' => null,
],
[
'order' => 'published',
'callback' => $callback
]
'callback' => $callback,
],
];
$this->assertEquals(
$expected,
Expand All @@ -102,7 +102,7 @@ public function testBeforeFind()

$event = new Event('Model.beforeFind', $this);
$query = $this->Table->query();
$behavior->beforeFind($event, $query, new \ArrayObject, true);
$behavior->beforeFind($event, $query, new \ArrayObject(), true);
$this->assertEquals(1, count($query->clause('order')));
}

Expand All @@ -114,7 +114,59 @@ public function testBeforeFindQueryWithOrder()
$event = new Event('Model.beforeFind', $this);
$query = $this->Table->find()
->order('author_id');
$behavior->beforeFind($event, $query, new \ArrayObject, true);
$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));
}
}
2 changes: 0 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,3 @@
}

require $root . '/vendor/cakephp/cakephp/tests/bootstrap.php';

\Cake\Core\Plugin::load('Muffin/Orderly', ['path' => dirname(dirname(__FILE__)) . DS]);

0 comments on commit b27f120

Please sign in to comment.