adventofcode/tests/SolutionsTest.php

92 lines
2.6 KiB
PHP
Raw Normal View History

<?php
namespace Tests;
use PHPUnit\Framework\TestCase;
use trizz\AdventOfCode\Solution;
2022-11-29 19:52:02 +01:00
/**
* @internal
*/
final class SolutionsTest extends TestCase
{
/**
* @dataProvider loadSolutions
*/
2022-11-29 19:52:02 +01:00
public function testSolutionPart1Example(Solution $solution): void
{
2022-11-29 19:52:02 +01:00
$this->runPart($solution, part: 1, testExample: true);
}
/**
* @dataProvider loadSolutions
2022-11-29 19:52:02 +01:00
*
* @depends testSolutionPart1Example
*/
2022-11-29 19:52:02 +01:00
public function testSolutionPart1(Solution $solution): void
{
2022-11-29 19:52:02 +01:00
$this->runPart($solution, part: 1, testExample: false);
}
/**
* @dataProvider loadSolutions
2022-11-29 19:52:02 +01:00
*
* @depends testSolutionPart1
*/
2022-11-29 19:52:02 +01:00
public function testSolutionPart2Example(Solution $solution): void
{
2022-11-29 19:52:02 +01:00
$this->runPart($solution, part: 2, testExample: true);
}
/**
* @dataProvider loadSolutions
2022-11-29 19:52:02 +01:00
*
* @depends testSolutionPart2Example
*/
2022-11-29 19:52:02 +01:00
public function testSolutionPart2(Solution $solution): void
{
2022-11-29 19:52:02 +01:00
$this->runPart($solution, part: 2, testExample: false);
}
2022-11-29 19:52:02 +01:00
/**
* @return array<string, array<int, Solution>>
*/
2023-12-01 11:04:30 +01:00
public static function loadSolutions(): array
{
$classes = [];
2022-11-29 19:52:02 +01:00
for ($year = 15; $year <= date('y'); ++$year) {
if (is_dir(__DIR__.'/../src/Y'.$year)) {
2022-11-29 19:52:02 +01:00
for ($day = 1; $day < 26; ++$day) {
$className = sprintf('trizz\\AdventOfCode\\Y%d\\Day%d', $year, $day);
if (class_exists($className)) {
2022-11-29 19:52:02 +01:00
/** @var Solution $class */
$class = new $className();
$class->loadData();
2022-11-29 19:52:02 +01:00
$classes["Year '".$year.' / Day '.$day] = [$class];
}
}
}
}
return $classes;
}
2022-11-29 19:52:02 +01:00
private function runPart(Solution $solution, int $part, bool $testExample): void
{
if (
($testExample && $solution->hasExampleData())
|| (!$testExample && $solution->hasData())
) {
$expectedResult = $solution::${'part'.$part.($testExample ? 'Example' : null).'Result'};
if ($expectedResult) {
$result = $solution->{'part'.$part.'Data'}(useExampleData: $testExample);
self::assertSame($expectedResult, $result);
} else {
$this->markTestSkipped('No '.($testExample ? 'example' : 'expected').' data for part '.$part.'.');
}
} else {
$this->markTestSkipped('No example and expected data for part '.$part.'.');
}
}
}