diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b914550..7c56835 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,7 +12,6 @@ jobs: php: - '8.1' - '8.0' - - '7.4' laravel: - '8.*' testbench: @@ -45,4 +44,4 @@ jobs: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest - name: Execute tests - run: vendor/bin/phpunit tests \ No newline at end of file + run: vendor/bin/pest tests \ No newline at end of file diff --git a/composer.json b/composer.json index 5623aeb..3e718c1 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ } ], "require": { - "php": "^7.4|^8.0", + "php": "^8.0", "guzzlehttp/guzzle": "^7.0.1", "illuminate/support": "^8.0|^9.0" }, @@ -47,8 +47,8 @@ } }, "scripts": { - "test": "vendor/bin/phpunit", - "test-coverage": "vendor/bin/phpunit --coverage-html coverage" + "test": "vendor/bin/pest", + "test-coverage": "vendor/bin/pest --coverage-html coverage" }, "config": { diff --git a/src/Endpoints/Database.php b/src/Endpoints/Database.php index 90a1c90..eeb7e96 100644 --- a/src/Endpoints/Database.php +++ b/src/Endpoints/Database.php @@ -4,6 +4,7 @@ use FiveamCode\LaravelNotionApi\Entities\Collections\EntityCollection; use FiveamCode\LaravelNotionApi\Entities\Collections\PageCollection; +use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use FiveamCode\LaravelNotionApi\Notion; use FiveamCode\LaravelNotionApi\Query\Filters\Filter; use FiveamCode\LaravelNotionApi\Query\Sorting; @@ -96,12 +97,24 @@ public function filterBy(Collection $filter): Database } /** - * @param Collection $sorts - * @return $this + * @param Collection|Sorting $sorts + * @return Database $this + * + * @throws HandlingException */ - public function sortBy(Collection $sorts): Database + public function sortBy(Sorting|Collection $sorts): Database { - $this->sorts = $sorts; + $sortInstance = get_class($sorts); + switch ($sortInstance) { + case Sorting::class: + $this->sorts->push($sorts); + break; + case Collection::class: + $this->sorts = $sorts; + break; + default: + throw new HandlingException("The parameter 'sorts' must be either a instance of the class Sorting or a Collection of Sortings."); + } return $this; } diff --git a/src/Endpoints/Search.php b/src/Endpoints/Search.php index a11ae08..c65848f 100644 --- a/src/Endpoints/Search.php +++ b/src/Endpoints/Search.php @@ -159,15 +159,4 @@ public function filterBy(string $filter): Search return $this; } - - /** - * @param Sorting $sort - * @return $this - */ - public function sortBy(Sorting $sort): Search - { - $this->sort = $sort; - - return $this; - } } diff --git a/src/Notion.php b/src/Notion.php index 8ef9eca..3778040 100644 --- a/src/Notion.php +++ b/src/Notion.php @@ -227,7 +227,7 @@ private function buildRequestHeader(): array } /** - * Due to the inconsistency of the Notion API requiring a endpoint url + * Due to the inconsistency of the Notion API requiring an endpoint url * with v* as well as a dated version in the request header, this method * maps the given version (e.g. v1) to the version date Notion requires * in the header (e.g. "2021-05-13"). diff --git a/src/Query/Sorting.php b/src/Query/Sorting.php index 919f095..113724d 100644 --- a/src/Query/Sorting.php +++ b/src/Query/Sorting.php @@ -93,16 +93,20 @@ public function toArray(): array } /** - * @param Collection $sortings + * @param Sorting|Collection $sortings * @return array */ - public static function sortQuery(Collection $sortings): array + public static function sortQuery(Sorting|Collection $sortings): array { $querySortings = new Collection(); - $sortings->each(function (Sorting $sorting) use ($querySortings) { - $querySortings->add($sorting->toArray()); - }); + if ($sortings instanceof Collection) { + $sortings->each(function (Sorting $sorting) use ($querySortings) { + $querySortings->push($sorting->toArray()); + }); + } else { + $querySortings->push($sortings->toArray()); + } return $querySortings->toArray(); } diff --git a/tests/NotionApiTest.php b/tests/NotionApiTest.php index 7e0b738..6dea0e9 100644 --- a/tests/NotionApiTest.php +++ b/tests/NotionApiTest.php @@ -5,7 +5,7 @@ use FiveamCode\LaravelNotionApi\Notion; use FiveamCode\LaravelNotionApi\NotionFacade; use Illuminate\Support\Collection; -use Orchestra\Testbench\TestCase; +use Orchestra\Testbench\TestCase as Orchestra; /** * Class EndpointPageTest. @@ -14,7 +14,7 @@ * * @see https://developers.notion.com/reference/get-page */ -class NotionApiTest extends TestCase +class NotionApiTest extends Orchestra { /** * @param \Illuminate\Foundation\Application $app @@ -50,10 +50,4 @@ protected function assertContainsInstanceOf(string $class, $haystack): bool return false; } - - /** @test */ - public function it_asserts_true() - { - $this->assertTrue(true); - } } diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..047ff43 --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,5 @@ +in(__DIR__); diff --git a/tests/SortingTest.php b/tests/SortingTest.php new file mode 100644 index 0000000..410c181 --- /dev/null +++ b/tests/SortingTest.php @@ -0,0 +1,30 @@ +assertEquals($expectedSortQuery, json_encode(Sorting::sortQuery($sortBy))); +}); + +it('can sort by multiple properties', function () { + $expectedSortQuery = '[{"timestamp":"created_time","direction":"ascending"},{"property":"Birth year","direction":"ascending"}]'; + + $sortings = new Collection(); + + $sortings->add(Sorting::timestampSort('created_time', 'ascending')); + $sortings->add(Sorting::propertySort('Birth year', 'ascending')); + + $this->assertEquals($expectedSortQuery, json_encode(Sorting::sortQuery($sortings))); +}); + +it('refuses other classes than sorting or collection in the sortBy() method', function () { + $this->expectException(TypeError::class); + + Notion::database('8284f3ff77e24d4a939d19459e4d6bdc') + ->sortBy(new stdClass()) + ->query(); +});