From 11639f54055a562e11fe742ca4658cd4b0168194 Mon Sep 17 00:00:00 2001 From: Diana Scharf Date: Wed, 7 Dec 2022 10:34:02 +0100 Subject: [PATCH 1/8] Pest tests work now properly, thanks to @mpociot --- composer.json | 4 ++-- src/Endpoints/Database.php | 20 ++++++++++++++++---- src/Notion.php | 2 +- tests/NotionApiTest.php | 11 ++--------- tests/Pest.php | 5 +++++ tests/SortingTest.php | 30 ++++++++++++++++++++++++++++++ 6 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 tests/Pest.php create mode 100644 tests/SortingTest.php diff --git a/composer.json b/composer.json index 5623aeb..f3b9f76 100644 --- a/composer.json +++ b/composer.json @@ -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..5519d65 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,23 @@ public function filterBy(Collection $filter): Database } /** - * @param Collection $sorts - * @return $this + * @param Collection|Sorting $sorts + * @return Database $this + * + * @throws HandlingException + * + * @todo As soon as this package drops PHP 7.4 support, we can use union types here (Sorting and Collection) */ - public function sortBy(Collection $sorts): Database + public function sortBy($sorts): Database { - $this->sorts = $sorts; + if($sorts instanceof Sorting) { + $this->sorts->push($sorts); + } elseif($sorts instanceof Collection) { + $this->sorts = $sorts; + } + else { + 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/Notion.php b/src/Notion.php index 8ef9eca..29e543b 100644 --- a/src/Notion.php +++ b/src/Notion.php @@ -240,7 +240,7 @@ private function mapVersionToHeaderVersion(): string { switch ($this->version) { case 'v1': - return '2021-05-13'; + return '2022-06-28'; default: throw new HandlingException('Invalid version.'); } diff --git a/tests/NotionApiTest.php b/tests/NotionApiTest.php index 7e0b738..32b9468 100644 --- a/tests/NotionApiTest.php +++ b/tests/NotionApiTest.php @@ -5,8 +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 +13,7 @@ * * @see https://developers.notion.com/reference/get-page */ -class NotionApiTest extends TestCase +class NotionApiTest extends Orchestra { /** * @param \Illuminate\Foundation\Application $app @@ -50,10 +49,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..29ba777 --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,5 @@ +in(__DIR__); \ No newline at end of file diff --git a/tests/SortingTest.php b/tests/SortingTest.php new file mode 100644 index 0000000..1f1920a --- /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(HandlingException::class); + + Notion::database('8284f3ff77e24d4a939d19459e4d6bdc') + ->sortBy(new stdClass()) + ->query(); +}); \ No newline at end of file From e48aef4d50b89d476aa23eb812287d08be9e45be Mon Sep 17 00:00:00 2001 From: Diana Scharf Date: Wed, 7 Dec 2022 10:49:49 +0100 Subject: [PATCH 2/8] use union types for sortBy method; removed PHP 7.4 support --- composer.json | 2 +- src/Endpoints/Database.php | 20 +++++++++++--------- src/Notion.php | 4 ++-- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/composer.json b/composer.json index f3b9f76..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" }, diff --git a/src/Endpoints/Database.php b/src/Endpoints/Database.php index 5519d65..23b6f22 100644 --- a/src/Endpoints/Database.php +++ b/src/Endpoints/Database.php @@ -102,17 +102,19 @@ public function filterBy(Collection $filter): Database * * @throws HandlingException * - * @todo As soon as this package drops PHP 7.4 support, we can use union types here (Sorting and Collection) */ - public function sortBy($sorts): Database + public function sortBy(Sorting|Collection $sorts): Database { - if($sorts instanceof Sorting) { - $this->sorts->push($sorts); - } elseif($sorts instanceof Collection) { - $this->sorts = $sorts; - } - else { - throw new HandlingException("The parameter 'sorts' must be either a instance of the class Sorting or a Collection of Sortings."); + $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/Notion.php b/src/Notion.php index 29e543b..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"). @@ -240,7 +240,7 @@ private function mapVersionToHeaderVersion(): string { switch ($this->version) { case 'v1': - return '2022-06-28'; + return '2021-05-13'; default: throw new HandlingException('Invalid version.'); } From cc624f3f2e5563d4ce5db475c597fdc22ac6684d Mon Sep 17 00:00:00 2001 From: Diana Scharf Date: Wed, 7 Dec 2022 10:53:37 +0100 Subject: [PATCH 3/8] updated tests for sortings; allowed union type in query helper --- src/Query/Sorting.php | 14 +++++++++----- tests/SortingTest.php | 4 ++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Query/Sorting.php b/src/Query/Sorting.php index 919f095..817b55f 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/SortingTest.php b/tests/SortingTest.php index 1f1920a..c99741a 100644 --- a/tests/SortingTest.php +++ b/tests/SortingTest.php @@ -5,7 +5,7 @@ use Illuminate\Support\Collection; it('can sort by a single property', function () { - $expectedSortQuery = '[{"property":"Birth year","direction":"ascending"}'; + $expectedSortQuery = '[{"property":"Birth year","direction":"ascending"}]'; $sortBy = Sorting::propertySort("Birth year", "ascending"); $this->assertEquals($expectedSortQuery, json_encode(Sorting::sortQuery($sortBy)));}); @@ -22,7 +22,7 @@ }); it('refuses other classes than sorting or collection in the sortBy() method', function () { - $this->expectException(HandlingException::class); + $this->expectException(TypeError::class); Notion::database('8284f3ff77e24d4a939d19459e4d6bdc') ->sortBy(new stdClass()) From c561b8c075d7cebd949a4f87e36411bf3e157d86 Mon Sep 17 00:00:00 2001 From: Di Date: Wed, 7 Dec 2022 10:54:02 +0100 Subject: [PATCH 4/8] Apply fixes from StyleCI (#91) --- src/Endpoints/Database.php | 5 ++--- src/Query/Sorting.php | 2 +- tests/NotionApiTest.php | 1 + tests/Pest.php | 2 +- tests/SortingTest.php | 12 ++++++------ 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Endpoints/Database.php b/src/Endpoints/Database.php index 23b6f22..eeb7e96 100644 --- a/src/Endpoints/Database.php +++ b/src/Endpoints/Database.php @@ -97,16 +97,15 @@ public function filterBy(Collection $filter): Database } /** - * @param Collection|Sorting $sorts + * @param Collection|Sorting $sorts * @return Database $this * * @throws HandlingException - * */ public function sortBy(Sorting|Collection $sorts): Database { $sortInstance = get_class($sorts); - switch($sortInstance) { + switch ($sortInstance) { case Sorting::class: $this->sorts->push($sorts); break; diff --git a/src/Query/Sorting.php b/src/Query/Sorting.php index 817b55f..113724d 100644 --- a/src/Query/Sorting.php +++ b/src/Query/Sorting.php @@ -100,7 +100,7 @@ public static function sortQuery(Sorting|Collection $sortings): array { $querySortings = new Collection(); - if($sortings instanceof Collection) { + if ($sortings instanceof Collection) { $sortings->each(function (Sorting $sorting) use ($querySortings) { $querySortings->push($sorting->toArray()); }); diff --git a/tests/NotionApiTest.php b/tests/NotionApiTest.php index 32b9468..6dea0e9 100644 --- a/tests/NotionApiTest.php +++ b/tests/NotionApiTest.php @@ -6,6 +6,7 @@ use FiveamCode\LaravelNotionApi\NotionFacade; use Illuminate\Support\Collection; use Orchestra\Testbench\TestCase as Orchestra; + /** * Class EndpointPageTest. * diff --git a/tests/Pest.php b/tests/Pest.php index 29ba777..047ff43 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -2,4 +2,4 @@ use FiveamCode\LaravelNotionApi\Tests\NotionApiTest; -uses(NotionApiTest::class)->in(__DIR__); \ No newline at end of file +uses(NotionApiTest::class)->in(__DIR__); diff --git a/tests/SortingTest.php b/tests/SortingTest.php index c99741a..410c181 100644 --- a/tests/SortingTest.php +++ b/tests/SortingTest.php @@ -1,22 +1,22 @@ assertEquals($expectedSortQuery, json_encode(Sorting::sortQuery($sortBy)));}); + $sortBy = Sorting::propertySort('Birth year', 'ascending'); + $this->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")); + $sortings->add(Sorting::timestampSort('created_time', 'ascending')); + $sortings->add(Sorting::propertySort('Birth year', 'ascending')); $this->assertEquals($expectedSortQuery, json_encode(Sorting::sortQuery($sortings))); }); @@ -27,4 +27,4 @@ Notion::database('8284f3ff77e24d4a939d19459e4d6bdc') ->sortBy(new stdClass()) ->query(); -}); \ No newline at end of file +}); From a03a01e7e9b3e1c3fe5c2251cbdcd9937e2d99e9 Mon Sep 17 00:00:00 2001 From: Diana Scharf Date: Wed, 7 Dec 2022 11:03:36 +0100 Subject: [PATCH 5/8] removed sortBy method from Search endpoint because it's not allowed anyway --- src/Endpoints/Search.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/Endpoints/Search.php b/src/Endpoints/Search.php index a11ae08..581552a 100644 --- a/src/Endpoints/Search.php +++ b/src/Endpoints/Search.php @@ -160,14 +160,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; - } } From f2d3c46c3f5003705e838d54bca6a84c3af9979e Mon Sep 17 00:00:00 2001 From: Di Date: Wed, 7 Dec 2022 11:04:02 +0100 Subject: [PATCH 6/8] Apply fixes from StyleCI (#92) --- src/Endpoints/Search.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Endpoints/Search.php b/src/Endpoints/Search.php index 581552a..c65848f 100644 --- a/src/Endpoints/Search.php +++ b/src/Endpoints/Search.php @@ -159,5 +159,4 @@ public function filterBy(string $filter): Search return $this; } - } From 0be3d152b2ef84a9c23317bf27fd913b0f16628c Mon Sep 17 00:00:00 2001 From: Diana Scharf Date: Wed, 7 Dec 2022 11:36:02 +0100 Subject: [PATCH 7/8] change workflow to pest --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b914550..caf7b39 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -45,4 +45,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 From ed303382b8b1d7611380ca0fbba1265f3084af0f Mon Sep 17 00:00:00 2001 From: Diana Scharf Date: Wed, 7 Dec 2022 11:37:20 +0100 Subject: [PATCH 8/8] Remove PHP 7.4 from workflow test matrix --- .github/workflows/main.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index caf7b39..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: