Skip to content
Merged
3 changes: 1 addition & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ jobs:
php:
- '8.1'
- '8.0'
- '7.4'
laravel:
- '8.*'
testbench:
Expand Down Expand Up @@ -45,4 +44,4 @@ jobs:
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
- name: Execute tests
run: vendor/bin/phpunit tests
run: vendor/bin/pest tests
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
}
],
"require": {
"php": "^7.4|^8.0",
"php": "^8.0",
"guzzlehttp/guzzle": "^7.0.1",
"illuminate/support": "^8.0|^9.0"
},
Expand All @@ -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": {
Expand Down
21 changes: 17 additions & 4 deletions src/Endpoints/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
11 changes: 0 additions & 11 deletions src/Endpoints/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/Notion.php
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down
14 changes: 9 additions & 5 deletions src/Query/Sorting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
10 changes: 2 additions & 8 deletions tests/NotionApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -50,10 +50,4 @@ protected function assertContainsInstanceOf(string $class, $haystack): bool

return false;
}

/** @test */
public function it_asserts_true()
{
$this->assertTrue(true);
}
}
5 changes: 5 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

use FiveamCode\LaravelNotionApi\Tests\NotionApiTest;

uses(NotionApiTest::class)->in(__DIR__);
30 changes: 30 additions & 0 deletions tests/SortingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use FiveamCode\LaravelNotionApi\Query\Sorting;
use Illuminate\Support\Collection;

it('can sort by a single property', function () {
$expectedSortQuery = '[{"property":"Birth year","direction":"ascending"}]';

$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'));

$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();
});