Skip to content

Commit

Permalink
Merge pull request #833 from PennyDreadfulMTG/trophies
Browse files Browse the repository at this point in the history
Better handling of season on eventreport.php
  • Loading branch information
bakert authored Nov 10, 2024
2 parents 9125579 + 8188cbd commit 675091b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 22 deletions.
17 changes: 15 additions & 2 deletions gatherling/Helpers/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function int(string $key, int|false $default = false): int

public function optionalInt(string $key): ?int
{
return marshal($this->vars[$key] ?? null)->optionalInt();
return marshal($this->coalesceNumeric($key))->optionalInt();
}

public function string(string $key, string|false $default = false): string
Expand All @@ -39,7 +39,7 @@ public function float(string $key, float|false $default = false): float
/** @psalm-suppress PossiblyUnusedMethod */
public function optionalFloat(string $key): ?float
{
return marshal($this->vars[$key] ?? null)->optionalFloat();
return marshal($this->coalesceNumeric($key))->optionalFloat();
}

/** @return list<int> */
Expand Down Expand Up @@ -71,4 +71,17 @@ public function dictString(string $key): array
{
return marshal($this->vars[$key] ?? null)->dictString();
}

// Coalesce like ?? does, but additionally if the value is an empty string, return null.
// This is how we want to treat something like 'season=' in a querystring.
private function coalesceNumeric(string $key): mixed
{
if (!isset($this->vars[$key])) {
return null;
}
if ($this->vars[$key] === '') {
return null;
}
return $this->vars[$key];
}
}
8 changes: 2 additions & 6 deletions gatherling/Views/Components/NumDropMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@

class NumDropMenu extends DropMenu
{
public function __construct(string $field, ?string $title, int $max, int|string|null $def, int $min = 0, ?string $special = null)
public function __construct(string $field, ?string $title, int $max, ?int $def, int $min = 0, ?string $special = null)
{
if ($def === '') {
$def = -1;
}

$options = [];
if ($special) {
$options[] = [
Expand All @@ -26,7 +22,7 @@ public function __construct(string $field, ?string $title, int $max, int|string|
$options[] = [
'text' => (string) $n,
'value' => (string) $n,
'isSelected' => $n == $def,
'isSelected' => $n === $def,
];
}

Expand Down
20 changes: 10 additions & 10 deletions gatherling/Views/Pages/EventForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ public function __construct(Event $event, bool $edit)
if ($event->start != null) {
$date = $event->start;
preg_match('/([0-9]+)-([0-9]+)-([0-9]+) ([0-9]+):([0-9]+):.*/', $date, $datearr);
$year = $datearr[1];
$month = $datearr[2];
$day = $datearr[3];
$hour = $datearr[4];
$minutes = $datearr[5];
$year = (int) $datearr[1];
$month = (int) $datearr[2];
$day = (int) $datearr[3];
$hour = (int) $datearr[4];
$minutes = (int) $datearr[5];
} else {
$year = date('Y', time());
$month = date('n', time());
$day = date('j', time());
$hour = date('H', time());
$minutes = date('i', time());
$year = (int) date('Y', time());
$month = (int) date('n', time());
$day = (int) date('j', time());
$hour = (int) date('H', time());
$minutes = (int) date('i', time());
}

$navLinks = [];
Expand Down
2 changes: 1 addition & 1 deletion gatherling/eventreport.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function eventList(string $format, string $series, ?int $season): array
$params['series'] = $series;
}

if (!empty($season)) {
if ($season !== null) {
$sql .= ' AND e.season = :season';
$params['season'] = $season;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/Helpers/MarshallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public function testOptionalInt(): void
marshal(123.45)->optionalInt();
}

public function testOptionalIntThrowsOnEmptyString(): void
{
$this->expectException(MarshalException::class);
marshal('')->optionalInt();
}

public function testString(): void
{
$this->assertEquals('hello', marshal('hello')->string('key'));
Expand Down
13 changes: 13 additions & 0 deletions tests/Helpers/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public function testIntMissing(): void
$request->int('baz');
}

public function testIntThrowsOnEmptyString(): void
{
$request = new Request(['foo' => '']);
$this->expectException(MarshalException::class);
$request->int('foo');
}

public function testOptionalInt(): void
{
$request = new Request(['foo' => '123', 'bar' => '123.4']);
Expand All @@ -34,6 +41,12 @@ public function testOptionalInt(): void
$request->optionalInt('bar');
}

public function testOptionalIntReturnsNullOnEmptyString(): void
{
$request = new Request(['foo' => '']);
$this->assertNull($request->optionalInt('foo'));
}

public function testFloat(): void
{
$request = new Request(['foo' => '123.45']);
Expand Down
7 changes: 4 additions & 3 deletions tests/Views/Components/NumDropMenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ public function testNumDropMenu(): void
$this->assertCount(12, $html->filter('select')->filter('option'));
$this->assertEquals('5', $html->filter('select')->filter('option[selected]')->attr('value'));

$numDropMenu = new NumDropMenu('test', 'Test', 10, '5');
$numDropMenu = new NumDropMenu('test', 'Test', 10, null);
$html = new Crawler($numDropMenu->render());
$this->assertEquals('test', $html->filter('select')->attr('name'));
$this->assertCount(12, $html->filter('select')->filter('option'));
$this->assertEquals('5', $html->filter('select')->filter('option[selected]')->attr('value'));
// Nothing is explicitly marked as selected, not even the default option
$this->assertEquals(0, $html->filter('select')->filter('option[selected]')->count());

$numDropMenu = new NumDropMenu('test', 'Test', 10, null);
$numDropMenu = new NumDropMenu('test', 'Test', 10, 0);
$html = new Crawler($numDropMenu->render());
$this->assertEquals('test', $html->filter('select')->attr('name'));
$this->assertCount(12, $html->filter('select')->filter('option'));
Expand Down

0 comments on commit 675091b

Please sign in to comment.