Skip to content

Commit

Permalink
Use static ::run instead of returning the runner instance from boot…
Browse files Browse the repository at this point in the history
…strap file (#211)

Resolves multiple Psalm errors like:

> INFO: PossiblyNullReference - tests/UpcKeys/WiFiKeyTest.phpt:34:10 -
Cannot call method run on possibly null value (see
https://psalm.dev/083) $runner->run(WiFiKeyTest::class);
  • Loading branch information
spaze authored Aug 30, 2023
2 parents 98196f0 + 386c9ab commit 2f5444e
Show file tree
Hide file tree
Showing 73 changed files with 217 additions and 156 deletions.
13 changes: 4 additions & 9 deletions site/app/Test/TestCaseRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace MichalSpacekCz\Test;

use LogicException;
use Nette\DI\Container;
use MichalSpacekCz\Application\Bootstrap;
use Nette\Utils\Type;
use ReflectionException;
use ReflectionMethod;
Expand All @@ -13,21 +13,16 @@
class TestCaseRunner
{

public function __construct(
private readonly Container $container,
) {
}


/**
* @param class-string<TestCase> $test
* @return void
*/
public function run(string $test): void
public static function run(string $test): void
{
$params = [];
try {
$method = new ReflectionMethod($test, '__construct');
$container = Bootstrap::bootTest();
foreach ($method->getParameters() as $parameter) {
$type = Type::fromReflection($parameter);
$paramIdent = "Parameter #{$parameter->getPosition()} \${$parameter->getName()}";
Expand All @@ -46,7 +41,7 @@ public function run(string $test): void
if (!class_exists($type->getSingleName()) && !interface_exists($type->getSingleName())) {
throw new LogicException("{$paramIdent} specifies a type {$type} but the class or interface doesn't exist");
}
$params[] = $this->container->getByType($type->getSingleName());
$params[] = $container->getByType($type->getSingleName());
}
} catch (ReflectionException) {
// pass, __construct() does not exist
Expand Down
5 changes: 3 additions & 2 deletions site/tests/Application/AppRequestTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ use Error;
use Exception;
use MichalSpacekCz\Application\Exceptions\NoOriginalRequestException;
use MichalSpacekCz\ShouldNotHappenException;
use MichalSpacekCz\Test\TestCaseRunner;
use Nette\Application\Request;
use Tester\Assert;
use Tester\TestCase;

$runner = require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/../bootstrap.php';

/** @testCase */
class AppRequestTest extends TestCase
Expand Down Expand Up @@ -112,4 +113,4 @@ class AppRequestTest extends TestCase

}

$runner->run(AppRequestTest::class);
TestCaseRunner::run(AppRequestTest::class);
5 changes: 3 additions & 2 deletions site/tests/Application/BootstrapTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ declare(strict_types = 1);

namespace MichalSpacekCz\Application;

use MichalSpacekCz\Test\TestCaseRunner;
use Nette\DI\Container;
use Tester\Assert;
use Tester\TestCase;
use Tracy\Debugger;
use Tracy\ILogger;

$runner = require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/../bootstrap.php';

/** @testCase */
class BootstrapTest extends TestCase
Expand Down Expand Up @@ -76,4 +77,4 @@ class BootstrapTest extends TestCase

}

$runner->run(BootstrapTest::class);
TestCaseRunner::run(BootstrapTest::class);
5 changes: 3 additions & 2 deletions site/tests/Application/LocaleLinkGeneratorTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ declare(strict_types = 1);
namespace MichalSpacekCz\Application;

use MichalSpacekCz\Test\NoOpTranslator;
use MichalSpacekCz\Test\TestCaseRunner;
use Nette\Application\IPresenterFactory;
use Nette\Application\LinkGenerator;
use Nette\Application\UI\InvalidLinkException;
use Nette\Http\IRequest;
use Tester\Assert;
use Tester\TestCase;

$runner = require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/../bootstrap.php';

/** @testCase */
class LocaleLinkGeneratorTest extends TestCase
Expand Down Expand Up @@ -133,4 +134,4 @@ class LocaleLinkGeneratorTest extends TestCase

}

$runner->run(LocaleLinkGeneratorTest::class);
TestCaseRunner::run(LocaleLinkGeneratorTest::class);
5 changes: 3 additions & 2 deletions site/tests/Application/ServerEnvTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ namespace MichalSpacekCz\Application;
use MichalSpacekCz\Application\Exceptions\ServerEnvNotArrayException;
use MichalSpacekCz\Application\Exceptions\ServerEnvNotFoundException;
use MichalSpacekCz\Application\Exceptions\ServerEnvNotStringException;
use MichalSpacekCz\Test\TestCaseRunner;
use Tester\Assert;
use Tester\TestCase;

$runner = require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/../bootstrap.php';

/** @testCase */
class ServerEnvTest extends TestCase
Expand Down Expand Up @@ -106,4 +107,4 @@ class ServerEnvTest extends TestCase

}

$runner->run(ServerEnvTest::class);
TestCaseRunner::run(ServerEnvTest::class);
5 changes: 3 additions & 2 deletions site/tests/Application/ThemeTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ namespace MichalSpacekCz\Application;

use MichalSpacekCz\Test\Http\Request;
use MichalSpacekCz\Test\Http\Response;
use MichalSpacekCz\Test\TestCaseRunner;
use Tester\Assert;
use Tester\TestCase;

$runner = require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/../bootstrap.php';

/** @testCase */
class ThemeTest extends TestCase
Expand Down Expand Up @@ -74,4 +75,4 @@ class ThemeTest extends TestCase

}

$runner->run(ThemeTest::class);
TestCaseRunner::run(ThemeTest::class);
5 changes: 3 additions & 2 deletions site/tests/Articles/ArticlesTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ use MichalSpacekCz\ShouldNotHappenException;
use MichalSpacekCz\Tags\Tags;
use MichalSpacekCz\Test\Database\Database;
use MichalSpacekCz\Test\NoOpTranslator;
use MichalSpacekCz\Test\TestCaseRunner;
use Tester\Assert;
use Tester\TestCase;

$runner = require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/../bootstrap.php';

/** @testCase */
class ArticlesTest extends TestCase
Expand Down Expand Up @@ -195,4 +196,4 @@ class ArticlesTest extends TestCase

}

$runner->run(ArticlesTest::class);
TestCaseRunner::run(ArticlesTest::class);
5 changes: 3 additions & 2 deletions site/tests/Articles/Blog/BlogPostPreviewTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ namespace MichalSpacekCz\Articles\Blog;

use DateTime;
use MichalSpacekCz\Templating\TemplateFactory;
use MichalSpacekCz\Test\TestCaseRunner;
use Nette\Application\IPresenterFactory;
use Nette\Application\UI\Presenter;
use Nette\Bridges\ApplicationLatte\DefaultTemplate;
use Tester\Assert;
use Tester\TestCase;

$runner = require __DIR__ . '/../../bootstrap.php';
require __DIR__ . '/../../bootstrap.php';

/** @testCase */
class BlogPostPreviewTest extends TestCase
Expand Down Expand Up @@ -63,4 +64,4 @@ class BlogPostPreviewTest extends TestCase

}

$runner->run(BlogPostPreviewTest::class);
TestCaseRunner::run(BlogPostPreviewTest::class);
5 changes: 3 additions & 2 deletions site/tests/Articles/Blog/BlogPostRecommendedLinkTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ declare(strict_types = 1);

namespace MichalSpacekCz\Articles\Blog;

use MichalSpacekCz\Test\TestCaseRunner;
use Nette\Utils\Json;
use Tester\Assert;
use Tester\TestCase;

$runner = require __DIR__ . '/../../bootstrap.php';
require __DIR__ . '/../../bootstrap.php';

/** @testCase */
class BlogPostRecommendedLinkTest extends TestCase
Expand Down Expand Up @@ -37,4 +38,4 @@ class BlogPostRecommendedLinkTest extends TestCase

}

$runner->run(BlogPostRecommendedLinkTest::class);
TestCaseRunner::run(BlogPostRecommendedLinkTest::class);
5 changes: 3 additions & 2 deletions site/tests/Articles/Blog/BlogPostRecommendedLinksTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ declare(strict_types = 1);

namespace MichalSpacekCz\Articles\Blog;

use MichalSpacekCz\Test\TestCaseRunner;
use Nette\Schema\ValidationException;
use Nette\Utils\Json;
use Tester\Assert;
use Tester\TestCase;

$runner = require __DIR__ . '/../../bootstrap.php';
require __DIR__ . '/../../bootstrap.php';

/** @testCase */
class BlogPostRecommendedLinksTest extends TestCase
Expand Down Expand Up @@ -64,4 +65,4 @@ class BlogPostRecommendedLinksTest extends TestCase

}

$runner->run(BlogPostRecommendedLinksTest::class);
TestCaseRunner::run(BlogPostRecommendedLinksTest::class);
5 changes: 3 additions & 2 deletions site/tests/DateTime/DateTimeFactoryTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ namespace MichalSpacekCz\DateTime;
use DateTimeImmutable;
use MichalSpacekCz\DateTime\Exceptions\CannotParseDateTimeException;
use MichalSpacekCz\DateTime\Exceptions\InvalidTimezoneException;
use MichalSpacekCz\Test\TestCaseRunner;
use Tester\Assert;
use Tester\TestCase;

$runner = require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/../bootstrap.php';

/** @testCase */
class DateTimeFactoryTest extends TestCase
Expand Down Expand Up @@ -51,4 +52,4 @@ class DateTimeFactoryTest extends TestCase

}

$runner->run(DateTimeFactoryTest::class);
TestCaseRunner::run(DateTimeFactoryTest::class);
5 changes: 3 additions & 2 deletions site/tests/DateTime/DateTimeZoneFactoryTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ declare(strict_types = 1);
namespace MichalSpacekCz\DateTime;

use MichalSpacekCz\DateTime\Exceptions\InvalidTimezoneException;
use MichalSpacekCz\Test\TestCaseRunner;
use Tester\Assert;
use Tester\TestCase;

$runner = require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/../bootstrap.php';

/** @testCase */
class DateTimeZoneFactoryTest extends TestCase
Expand All @@ -34,4 +35,4 @@ class DateTimeZoneFactoryTest extends TestCase

}

$runner->run(DateTimeZoneFactoryTest::class);
TestCaseRunner::run(DateTimeZoneFactoryTest::class);
5 changes: 3 additions & 2 deletions site/tests/EasterEgg/FourOhFourButFoundTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ namespace MichalSpacekCz\EasterEgg;
use MichalSpacekCz\Test\Application\ApplicationPresenter;
use MichalSpacekCz\Test\Application\UiPresenterMock;
use MichalSpacekCz\Test\Http\Request;
use MichalSpacekCz\Test\TestCaseRunner;
use Nette\Application\Responses\TextResponse;
use Nette\Http\UrlScript;
use Tester\Assert;
use Tester\TestCase;

$runner = require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/../bootstrap.php';

/** @testCase */
class FourOhFourButFoundTest extends TestCase
Expand Down Expand Up @@ -71,4 +72,4 @@ class FourOhFourButFoundTest extends TestCase

}

$runner->run(FourOhFourButFoundTest::class);
TestCaseRunner::run(FourOhFourButFoundTest::class);
5 changes: 3 additions & 2 deletions site/tests/EasterEgg/NetteCve202015227Test.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ declare(strict_types = 1);

namespace MichalSpacekCz\EasterEgg;

use MichalSpacekCz\Test\TestCaseRunner;
use Nette\Application\BadRequestException;
use Tester\Assert;
use Tester\TestCase;

$runner = require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/../bootstrap.php';

/** @testCase */
class NetteCve202015227Test extends TestCase
Expand Down Expand Up @@ -121,4 +122,4 @@ class NetteCve202015227Test extends TestCase

}

$runner->run(NetteCve202015227Test::class);
TestCaseRunner::run(NetteCve202015227Test::class);
5 changes: 3 additions & 2 deletions site/tests/EasterEgg/WinterIsComingTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ namespace MichalSpacekCz\EasterEgg;

use MichalSpacekCz\Test\Application\ApplicationPresenter;
use MichalSpacekCz\Test\Application\UiPresenterMock;
use MichalSpacekCz\Test\TestCaseRunner;
use Nette\Application\Responses\TextResponse;
use Nette\Application\UI\Form;
use Nette\Forms\Controls\TextInput;
use Nette\InvalidStateException;
use Tester\Assert;
use Tester\TestCase;

$runner = require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/../bootstrap.php';

/** @testCase */
class WinterIsComingTest extends TestCase
Expand Down Expand Up @@ -150,4 +151,4 @@ class WinterIsComingTest extends TestCase

}

$runner->run(WinterIsComingTest::class);
TestCaseRunner::run(WinterIsComingTest::class);
5 changes: 3 additions & 2 deletions site/tests/Feed/ExportsTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ use MichalSpacekCz\Articles\ArticleEdit;
use MichalSpacekCz\Formatter\TexyFormatter;
use MichalSpacekCz\Test\Articles\ArticlesMock;
use MichalSpacekCz\Test\NoOpTranslator;
use MichalSpacekCz\Test\TestCaseRunner;
use Nette\Application\BadRequestException;
use Nette\Caching\Storage;
use Nette\Utils\Html;
use SimpleXMLElement;
use Tester\Assert;
use Tester\TestCase;

$runner = require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/../bootstrap.php';

/** @testCase */
class ExportsTest extends TestCase
Expand Down Expand Up @@ -155,4 +156,4 @@ class ExportsTest extends TestCase

}

$runner->run(ExportsTest::class);
TestCaseRunner::run(ExportsTest::class);
5 changes: 3 additions & 2 deletions site/tests/Form/FormValuesTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ declare(strict_types = 1);
namespace Form;

use MichalSpacekCz\Form\FormValues;
use MichalSpacekCz\Test\TestCaseRunner;
use Nette\Forms\Controls\SubmitButton;
use Nette\Forms\Form;
use Nette\InvalidStateException;
use Tester\Assert;
use Tester\TestCase;

$runner = require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/../bootstrap.php';

/** @testCase */
class FormValuesTest extends TestCase
Expand Down Expand Up @@ -51,4 +52,4 @@ class FormValuesTest extends TestCase

}

$runner->run(FormValuesTest::class);
TestCaseRunner::run(FormValuesTest::class);
5 changes: 3 additions & 2 deletions site/tests/Formatter/TexyFormatterTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ namespace MichalSpacekCz\Formatter;
use DateTime;
use MichalSpacekCz\Test\Application\ApplicationPresenter;
use MichalSpacekCz\Test\Database\Database;
use MichalSpacekCz\Test\TestCaseRunner;
use Nette\Application\Application;
use Tester\Assert;
use Tester\TestCase;

$runner = require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/../bootstrap.php';

/** @testCase */
class TexyFormatterTest extends TestCase
Expand Down Expand Up @@ -135,4 +136,4 @@ class TexyFormatterTest extends TestCase

}

$runner->run(TexyFormatterTest::class);
TestCaseRunner::run(TexyFormatterTest::class);
Loading

0 comments on commit 2f5444e

Please sign in to comment.