Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #11 from patrickomeara/master
Browse files Browse the repository at this point in the history
Support Laravel 6.*
  • Loading branch information
juampi92 authored Oct 15, 2019
2 parents 3d1cf7c + c9605bd commit c06e725
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 42 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
language: php

php:
- 7.0
- 7.1
- 7.2

before_script:
- travis_retry composer self-update
- travis_retry composer install --prefer-source --no-interaction --dev

script: vendor/bin/phpunit
script: vendor/bin/phpunit
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
}
],
"require": {
"php": ">=7.0.0",
"illuminate/support": "5.5.*||5.6.*||5.7.*||5.8.*",
"illuminate/http": "5.5.*||5.6.*||5.7.*||5.8.*",
"illuminate/database": "5.5.*||5.6.*||5.7.*||5.8.*"
"php": ">=7.1.0",
"illuminate/support": "5.5.*|5.6.*|5.7.*|5.8.*|6.*",
"illuminate/http": "5.5.*|5.6.*|5.7.*|5.8.*|6.*",
"illuminate/database": "5.5.*|5.6.*|5.7.*|5.8.*|6.*"
},
"require-dev": {
"orchestra/testbench": "~3.5",
"phpunit/phpunit": "~6.0"
"orchestra/testbench": "~3.5|~4.2",
"phpunit/phpunit": "~6.0|~7.0|~8.0"
},
"autoload": {
"psr-4": {
Expand Down
3 changes: 1 addition & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
stopOnFailure="false">
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix="Test.php">./tests/</directory>
Expand Down
30 changes: 18 additions & 12 deletions src/CursorPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Pagination\AbstractPaginator;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use IteratorAggregate;
use JsonSerializable;

Expand Down Expand Up @@ -54,7 +55,7 @@ class CursorPaginator extends AbstractPaginator implements Arrayable, ArrayAcces
* Create a new paginator instance.
*
* @param mixed $items
* @param int $perPage
* @param int $perPage
* @param array $options
*/
public function __construct($items, $perPage, array $options = [])
Expand Down Expand Up @@ -134,7 +135,9 @@ protected static function formatNames($name)
$transform_name = config('cursor_pagination.transform_name', null);

if (!is_null($transform_name)) {
return call_user_func($transform_name, $name);
$str_method = Str::replaceLast('_case', '', $transform_name);

return Str::$str_method($name);
}

return $name;
Expand Down Expand Up @@ -231,9 +234,9 @@ public function url($cursor = [])
$query = array_merge($this->query, $cursor);

return $this->path
. (str_contains($this->path, '?') ? '&' : '?')
. http_build_query($query, '', '&')
. $this->buildFragment();
.(Str::contains($this->path, '?') ? '&' : '?')
.http_build_query($query, '', '&')
.$this->buildFragment();
}

/**
Expand Down Expand Up @@ -303,6 +306,7 @@ protected function getIdentifierName(): string

/**
* @param $id
*
* @return int
*/
protected function parseDateIdentifier($id): int
Expand All @@ -312,7 +316,9 @@ protected function parseDateIdentifier($id): int

/**
* Will check if the identifier is type date.
*
* @param $id
*
* @return bool
*/
protected function isDateIdentifier($id): bool
Expand All @@ -324,7 +330,7 @@ protected function isDateIdentifier($id): bool
* Render the paginator using a given view.
*
* @param string|null $view
* @param array $data
* @param array $data
*
* @return string
*/
Expand All @@ -344,11 +350,11 @@ public function toArray()
list($prev, $next) = $this->getCursorQueryNames();

return [
'data' => $this->items->toArray(),
'path' => $this->url(),
$prev => self::castCursor($this->prevCursor()),
$next => self::castCursor($this->nextCursor()),
'per_page' => (int)$this->perPage(),
'data' => $this->items->toArray(),
'path' => $this->url(),
$prev => self::castCursor($this->prevCursor()),
$next => self::castCursor($this->nextCursor()),
'per_page' => (int) $this->perPage(),
'next_page_url' => $this->nextPageUrl(),
'prev_page_url' => $this->previousPageUrl(),
];
Expand Down Expand Up @@ -387,6 +393,6 @@ protected static function castCursor($val = null)
return $val;
}

return (string)$val;
return (string) $val;
}
}
12 changes: 6 additions & 6 deletions tests/CursorNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ public function test_camel_case()
config(['cursor_pagination.transform_name' => 'snake_case']);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();

$this->assertContains('_', $prev_name);
$this->assertContains('_', $next_name);
$this->assertStringContainsString('_', $prev_name);
$this->assertStringContainsString('_', $next_name);

config(['cursor_pagination.transform_name' => 'camel_case']);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();

$this->assertNotContains('_', $prev_name);
$this->assertNotContains('_', $next_name);
$this->assertStringNotContainsString('_', $prev_name);
$this->assertStringNotContainsString('_', $next_name);

config(['cursor_pagination.transform_name' => 'kebab_case']);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();

$this->assertContains('-', $prev_name);
$this->assertContains('-', $next_name);
$this->assertStringContainsString('-', $prev_name);
$this->assertStringContainsString('-', $next_name);

config(['cursor_pagination.transform_name' => null]);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/CursorPaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function test_init()
['id' => 1], ['id' => 2], ['id' => 3], ['id' => 4], ['id' => 5],
], $perPage = 2);

$this->assertAttributeEquals($perPage, 'perPage', $p);
$this->assertEquals($perPage, $p->perPage());
}

public function test_overflow_pagination()
Expand Down
8 changes: 4 additions & 4 deletions tests/CursorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function test_resolves_prev_cursor()

$cursor = CursorPaginator::resolveCurrentCursor($req);

$this->assertAttributeEquals($val, 'prev', $cursor, 'Cursor\'s nav should be prev');
$this->assertEquals($val, $cursor->getPrevCursor(), 'Cursor\'s nav should be prev');
$this->assertFalse($cursor->isNext(), 'is not next');
$this->assertTrue($cursor->isPrev(), 'is prev');
$this->assertTrue($cursor->isPresent(), 'is present');
Expand All @@ -58,7 +58,7 @@ public function test_resolves_next_cursor()

$cursor = CursorPaginator::resolveCurrentCursor($req);

$this->assertAttributeEquals($val, 'next', $cursor, "Cursor's value should be $val");
$this->assertEquals($val, $cursor->getNextCursor(), "Cursor's value should be $val");
$this->assertTrue($cursor->isNext(), 'is next');
$this->assertTrue(!$cursor->isPrev(), 'is not prev');
$this->assertTrue($cursor->isPresent(), 'is present');
Expand All @@ -78,8 +78,8 @@ public function test_both_cursor()

$cursor = CursorPaginator::resolveCurrentCursor($req);

$this->assertAttributeEquals($prev_val, 'prev', $cursor);
$this->assertAttributeEquals($next_val, 'next', $cursor);
$this->assertEquals($prev_val, $cursor->getPrevCursor());
$this->assertEquals($next_val, $cursor->getNextCursor());
$this->assertTrue($cursor->isNext(), 'is next');
$this->assertTrue($cursor->isPrev(), 'is prev');
}
Expand Down
11 changes: 3 additions & 8 deletions tests/MacroTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function test_perPage_dynamic_prev()
'path' => '/',
]);

$this->assertAttributeEquals($prev_count, 'perPage', $p);
$this->assertEquals($prev_count, $p->perPage());
$this->assertEquals(count($p->toArray()['data']), $prev_count);
}

Expand All @@ -71,7 +71,7 @@ public function test_perPage_dynamic_empty_cursor_defaults_next()
'path' => '/',
]);

$this->assertAttributeEquals($next_count, 'perPage', $p);
$this->assertEquals($next_count, $p->perPage());
$this->assertEquals(count($p->toArray()['data']), $next_count);
}

Expand All @@ -86,7 +86,7 @@ public function test_perPage_dynamic_next()
'path' => '/',
]);

$this->assertAttributeEquals($next_count, 'perPage', $p);
$this->assertEquals($next_count, $p->perPage());
$this->assertEquals(count($p->toArray()['data']), $next_count);
}

Expand All @@ -112,8 +112,6 @@ public function test_sorted_by_date()
'path' => '/',
]);

$this->assertAttributeEquals(true, 'date_identifier', $p);

$this->assertGreaterThanOrEqual(strtotime('last month'), $p->prevCursor());
$this->assertLessThanOrEqual(strtotime('now'), $p->prevCursor());
$this->assertGreaterThanOrEqual(strtotime('last month'), $p->nextCursor());
Expand All @@ -130,7 +128,6 @@ public function test_sorted_by_date_on_query()
'path' => '/',
]);

$this->assertAttributeEquals(true, 'date_identifier', $p);
$this->assertGreaterThanOrEqual(strtotime('last month'), $p->prevCursor());
$this->assertLessThanOrEqual(strtotime('now'), $p->prevCursor());
$this->assertGreaterThanOrEqual(strtotime('last month'), $p->nextCursor());
Expand All @@ -141,8 +138,6 @@ public function test_sorted_by_date_auto_identifier()
{
$p = User::orderBy('datetime', 'asc')->cursorPaginate(10, ['*'], ['path' => '/']);

$this->assertAttributeEquals('datetime', 'identifier', $p);
$this->assertAttributeEquals(true, 'date_identifier', $p);
$this->assertGreaterThanOrEqual(strtotime('last month'), $p->prevCursor());
$this->assertLessThanOrEqual(strtotime('now'), $p->prevCursor());
$this->assertGreaterThanOrEqual(strtotime('last month'), $p->nextCursor());
Expand Down
2 changes: 1 addition & 1 deletion tests/ModelsTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class ModelsTestCase extends TestCase
{
public function setUp()
public function setUp(): void
{
parent::setUp();
// Reset config on each request
Expand Down

0 comments on commit c06e725

Please sign in to comment.