Skip to content

Commit

Permalink
fix(database): correct date filter JSON serialization
Browse files Browse the repository at this point in the history
Co-authored-by: Mario Simão <[email protected]>
  • Loading branch information
braumye and mariosimao authored Apr 16, 2023
1 parent 2fc1940 commit 1f21cbc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
20 changes: 11 additions & 9 deletions src/Databases/Query/DateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Notion\Databases\Query;

use stdClass;

/** @psalm-immutable */
class DateFilter implements Filter, Condition
{
Expand Down Expand Up @@ -32,7 +34,7 @@ private function __construct(
private readonly string $propertyType,
private readonly string $propertyName,
private readonly Operator $operator,
private readonly string|bool|array $value,
private readonly string|bool|array|stdClass $value,
) {
if (!in_array($operator, self::$validOperators)) {
throw new \Exception("Invalid operator");
Expand Down Expand Up @@ -84,7 +86,7 @@ public function operator(): Operator
return $this->operator;
}

public function value(): string|bool|array
public function value(): string|bool|array|stdClass
{
return $this->value;
}
Expand Down Expand Up @@ -136,36 +138,36 @@ public function onOrAfter(string $value): self

public function pastWeek(): self
{
return new self($this->propertyType, $this->propertyName, Operator::PastWeek, []);
return new self($this->propertyType, $this->propertyName, Operator::PastWeek, new stdClass());
}

public function pastMonth(): self
{
return new self($this->propertyType, $this->propertyName, Operator::PastMonth, []);
return new self($this->propertyType, $this->propertyName, Operator::PastMonth, new stdClass());
}

public function pastYear(): self
{
return new self($this->propertyType, $this->propertyName, Operator::PastYear, []);
return new self($this->propertyType, $this->propertyName, Operator::PastYear, new stdClass());
}

public function nextWeek(): self
{
return new self($this->propertyType, $this->propertyName, Operator::NextWeek, []);
return new self($this->propertyType, $this->propertyName, Operator::NextWeek, new stdClass());
}

public function nextMonth(): self
{
return new self($this->propertyType, $this->propertyName, Operator::NextMonth, []);
return new self($this->propertyType, $this->propertyName, Operator::NextMonth, new stdClass());
}

public function nextYear(): self
{
return new self($this->propertyType, $this->propertyName, Operator::NextYear, []);
return new self($this->propertyType, $this->propertyName, Operator::NextYear, new stdClass());
}

public function thisWeek(): self
{
return new self($this->propertyType, $this->propertyName, Operator::ThisWeek, []);
return new self($this->propertyType, $this->propertyName, Operator::ThisWeek, new stdClass());
}
}
9 changes: 5 additions & 4 deletions tests/Unit/Databases/Query/CompoundFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Notion\Databases\Query\SelectFilter;
use Notion\Databases\Query\TextFilter;
use PHPUnit\Framework\TestCase;
use stdClass;

class CompoundFilterTest extends TestCase
{
Expand All @@ -25,11 +26,11 @@ public function test_and(): void
],
[
"timestamp" => "created_time",
"date" => [ "past_week" => [] ],
"date" => [ "past_week" => new stdClass() ],
],
],
];
$this->assertSame($expected, $filter->toArray());
$this->assertEquals($expected, $filter->toArray());
}

public function test_or(): void
Expand All @@ -47,11 +48,11 @@ public function test_or(): void
],
[
"timestamp" => "created_time",
"date" => [ "past_week" => [] ],
"date" => [ "past_week" => new stdClass() ],
],
],
];
$this->assertSame($expected, $filter->toArray());
$this->assertEquals($expected, $filter->toArray());
}

public function test_nested(): void
Expand Down
29 changes: 15 additions & 14 deletions tests/Unit/Databases/Query/DateFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Notion\Databases\Query\DateFilter;
use Notion\Databases\Query\Operator;
use PHPUnit\Framework\TestCase;
use stdClass;

class DateFilterTest extends TestCase
{
Expand Down Expand Up @@ -129,9 +130,9 @@ public function test_past_week(): void

$expected = [
"property" => "Release date",
"date" => [ "past_week" => [] ],
"date" => [ "past_week" => new stdClass() ],
];
$this->assertSame($expected, $filter->toArray());
$this->assertEquals($expected, $filter->toArray());
}

public function test_past_month(): void
Expand All @@ -141,9 +142,9 @@ public function test_past_month(): void

$expected = [
"property" => "Release date",
"date" => [ "past_month" => [] ],
"date" => [ "past_month" => new stdClass() ],
];
$this->assertSame($expected, $filter->toArray());
$this->assertEquals($expected, $filter->toArray());
}

public function test_past_year(): void
Expand All @@ -153,9 +154,9 @@ public function test_past_year(): void

$expected = [
"property" => "Release date",
"date" => [ "past_year" => [] ],
"date" => [ "past_year" => new stdClass() ],
];
$this->assertSame($expected, $filter->toArray());
$this->assertEquals($expected, $filter->toArray());
}

public function test_next_week(): void
Expand All @@ -165,9 +166,9 @@ public function test_next_week(): void

$expected = [
"property" => "Release date",
"date" => [ "next_week" => [] ],
"date" => [ "next_week" => new stdClass() ],
];
$this->assertSame($expected, $filter->toArray());
$this->assertEquals($expected, $filter->toArray());
}

public function test_next_month(): void
Expand All @@ -177,9 +178,9 @@ public function test_next_month(): void

$expected = [
"property" => "Release date",
"date" => [ "next_month" => [] ],
"date" => [ "next_month" => new stdClass() ],
];
$this->assertSame($expected, $filter->toArray());
$this->assertEquals($expected, $filter->toArray());
}

public function test_next_year(): void
Expand All @@ -189,9 +190,9 @@ public function test_next_year(): void

$expected = [
"property" => "Release date",
"date" => [ "next_year" => [] ],
"date" => [ "next_year" => new stdClass() ],
];
$this->assertSame($expected, $filter->toArray());
$this->assertEquals($expected, $filter->toArray());
}

public function test_this_week(): void
Expand All @@ -201,8 +202,8 @@ public function test_this_week(): void

$expected = [
"property" => "Release date",
"date" => [ "this_week" => [] ],
"date" => [ "this_week" => new stdClass() ],
];
$this->assertSame($expected, $filter->toArray());
$this->assertEquals($expected, $filter->toArray());
}
}

0 comments on commit 1f21cbc

Please sign in to comment.