Skip to content

Commit

Permalink
Tests: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jan 4, 2024
1 parent 3b8cbcd commit 0b5d838
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 95 deletions.
4 changes: 0 additions & 4 deletions tests/.coveralls.yml

This file was deleted.

10 changes: 0 additions & 10 deletions tests/.gitignore

This file was deleted.

10 changes: 4 additions & 6 deletions tests/Cases/Decorator/Decorator.phpt
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?php declare(strict_types = 1);

/**
* Test: Decorator/Decorator
*/

use Contributte\DI\Decorator\Decorator;
use Contributte\DI\Helper\ExtensionDefinitionsHelper;
use Contributte\Tester\Environment;
use Contributte\Tester\Toolkit;
use Nette\DI\Compiler;
use Nette\DI\CompilerExtension;
use Nette\DI\Container;
Expand All @@ -17,8 +15,8 @@ use Tests\Fixtures\Inject\Tester;

require_once __DIR__ . '/../../bootstrap.php';

test(static function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
Toolkit::test(static function (): void {
$loader = new ContainerLoader(Environment::getTestDir(), true);
$class = $loader->load(static function (Compiler $compiler): void {
$extension = new class extends CompilerExtension
{
Expand Down
10 changes: 4 additions & 6 deletions tests/Cases/Extension/ContainerAwareExtension.phpt
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?php declare(strict_types = 1);

/**
* Test: Extension\ContainerAwareExtension
*/

use Contributte\DI\Extension\ContainerAwareExtension;
use Contributte\DI\IContainerAware;
use Contributte\Tester\Environment;
use Contributte\Tester\Toolkit;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
Expand All @@ -15,8 +13,8 @@ use Tests\Fixtures\TestContainerAware;

require_once __DIR__ . '/../../bootstrap.php';

test(static function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
Toolkit::test(static function (): void {
$loader = new ContainerLoader(Environment::getTestDir(), true);
$class = $loader->load(static function (Compiler $compiler): void {
$compiler->addExtension('aware', new ContainerAwareExtension());
$compiler->loadConfig(FileMock::create('
Expand Down
10 changes: 4 additions & 6 deletions tests/Cases/Extension/MutableExtension.phpt
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?php declare(strict_types = 1);

/**
* Test: Extension\MutableExtension
*/

use Contributte\DI\Extension\MutableExtension;
use Contributte\Tester\Environment;
use Contributte\Tester\Toolkit;
use Nette\DI\Compiler;
use Nette\DI\CompilerExtension;
use Nette\DI\Container;
Expand All @@ -14,8 +12,8 @@ use Tester\Assert;

require_once __DIR__ . '/../../bootstrap.php';

test(static function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
Toolkit::test(static function (): void {
$loader = new ContainerLoader(Environment::getTestDir(), true);
$class = $loader->load(static function (Compiler $compiler): void {
$mutable = new MutableExtension();
$mutable->onLoad[] = static function (CompilerExtension $ext, ContainerBuilder $builder): void {
Expand Down
78 changes: 39 additions & 39 deletions tests/Cases/Extension/ResourceExtension.phpt
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?php declare(strict_types = 1);

/**
* Test: Extension\ResourceExtension
*/

use Contributte\DI\Extension\ResourceExtension;
use Contributte\Tester\Environment;
use Contributte\Tester\Toolkit;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
Expand All @@ -25,12 +23,14 @@ use Tests\Fixtures\Scalar\ZScalarService;

require_once __DIR__ . '/../../bootstrap.php';

const APP_DIR = __DIR__ . '/../..';

// Autoload services
test(static function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
Toolkit::test(static function (): void {
$loader = new ContainerLoader(Environment::getTestDir(), true);
$class = $loader->load(static function (Compiler $compiler): void {
$compiler->addExtension('autoload', new ResourceExtension());
$compiler->addConfig(['parameters' => ['appDir' => TESTER_DIR]]);
$compiler->addConfig(['parameters' => ['appDir' => APP_DIR]]);
$compiler->loadConfig(FileMock::create('
autoload:
resources:
Expand All @@ -47,11 +47,11 @@ test(static function (): void {
});

// Skip interface & abstract classes
test(static function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
Toolkit::test(static function (): void {
$loader = new ContainerLoader(Environment::getTestDir(), true);
$class = $loader->load(static function (Compiler $compiler): void {
$compiler->addExtension('autoload', new ResourceExtension());
$compiler->addConfig(['parameters' => ['appDir' => TESTER_DIR]]);
$compiler->addConfig(['parameters' => ['appDir' => APP_DIR]]);
$compiler->loadConfig(FileMock::create('
autoload:
resources:
Expand All @@ -67,11 +67,11 @@ test(static function (): void {
});

// Multiple resources
test(static function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
Toolkit::test(static function (): void {
$loader = new ContainerLoader(Environment::getTestDir(), true);
$class = $loader->load(static function (Compiler $compiler): void {
$compiler->addExtension('autoload', new ResourceExtension());
$compiler->addConfig(['parameters' => ['appDir' => TESTER_DIR]]);
$compiler->addConfig(['parameters' => ['appDir' => APP_DIR]]);
$compiler->loadConfig(FileMock::create('
autoload:
resources:
Expand All @@ -92,11 +92,11 @@ test(static function (): void {
});

// Exclude namespace
test(static function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
Toolkit::test(static function (): void {
$loader = new ContainerLoader(Environment::getTestDir(), true);
$class = $loader->load(static function (Compiler $compiler): void {
$compiler->addExtension('autoload', new ResourceExtension());
$compiler->addConfig(['parameters' => ['appDir' => TESTER_DIR]]);
$compiler->addConfig(['parameters' => ['appDir' => APP_DIR]]);
$compiler->loadConfig(FileMock::create('
autoload:
resources:
Expand All @@ -116,9 +116,9 @@ test(static function (): void {
});

// Invalid resource - must end with /
test(static function (): void {
Toolkit::test(static function (): void {
Assert::throws(static function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
$loader = new ContainerLoader(Environment::getTestDir(), true);
$class = $loader->load(static function (Compiler $compiler): void {
$compiler->addExtension('autoload', new ResourceExtension());
$compiler->loadConfig(FileMock::create('
Expand All @@ -133,11 +133,11 @@ test(static function (): void {
});

// Exclude whole namespace
test(static function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
Toolkit::test(static function (): void {
$loader = new ContainerLoader(Environment::getTestDir(), true);
$class = $loader->load(static function (Compiler $compiler): void {
$compiler->addExtension('autoload', new ResourceExtension());
$compiler->addConfig(['parameters' => ['appDir' => TESTER_DIR]]);
$compiler->addConfig(['parameters' => ['appDir' => APP_DIR]]);
$compiler->loadConfig(FileMock::create('
autoload:
resources:
Expand All @@ -159,11 +159,11 @@ test(static function (): void {
});

// Decorate services - add tags
test(static function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
Toolkit::test(static function (): void {
$loader = new ContainerLoader(Environment::getTestDir(), true);
$class = $loader->load(static function (Compiler $compiler): void {
$compiler->addExtension('autoload', new ResourceExtension());
$compiler->addConfig(['parameters' => ['appDir' => TESTER_DIR]]);
$compiler->addConfig(['parameters' => ['appDir' => APP_DIR]]);
$compiler->loadConfig(FileMock::create('
autoload:
resources:
Expand All @@ -181,11 +181,11 @@ test(static function (): void {
});

// Decorate services - add setup
test(static function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
Toolkit::test(static function (): void {
$loader = new ContainerLoader(Environment::getTestDir(), true);
$class = $loader->load(static function (Compiler $compiler): void {
$compiler->addExtension('autoload', new ResourceExtension());
$compiler->addConfig(['parameters' => ['appDir' => TESTER_DIR]]);
$compiler->addConfig(['parameters' => ['appDir' => APP_DIR]]);
$compiler->loadConfig(FileMock::create('
services:
- stdClass
Expand All @@ -207,11 +207,11 @@ test(static function (): void {
});

// Decorate services - add setup
test(static function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
Toolkit::test(static function (): void {
$loader = new ContainerLoader(Environment::getTestDir(), true);
$class = $loader->load(static function (Compiler $compiler): void {
$compiler->addExtension('autoload', new ResourceExtension());
$compiler->addConfig(['parameters' => ['appDir' => TESTER_DIR]]);
$compiler->addConfig(['parameters' => ['appDir' => APP_DIR]]);
$compiler->loadConfig(FileMock::create('
autoload:
resources:
Expand All @@ -229,11 +229,11 @@ test(static function (): void {
});

// Register services manually
test(static function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
Toolkit::test(static function (): void {
$loader = new ContainerLoader(Environment::getTestDir(), true);
$class = $loader->load(static function (Compiler $compiler): void {
$compiler->addExtension('autoload', new ResourceExtension());
$compiler->addConfig(['parameters' => ['appDir' => TESTER_DIR]]);
$compiler->addConfig(['parameters' => ['appDir' => APP_DIR]]);
$compiler->loadConfig(FileMock::create('
services:
scalar: Tests\Fixtures\Scalar\ScalarService("foobar")
Expand All @@ -257,12 +257,12 @@ test(static function (): void {
});

// Register services manually (exception)
test(static function (): void {
Toolkit::test(static function (): void {
Assert::exception(function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
$loader = new ContainerLoader(Environment::getTestDir(), true);
$loader->load(static function (Compiler $compiler): void {
$compiler->addExtension('autoload', new ResourceExtension());
$compiler->addConfig(['parameters' => ['appDir' => TESTER_DIR]]);
$compiler->addConfig(['parameters' => ['appDir' => APP_DIR]]);
$compiler->loadConfig(FileMock::create('
autoload:
resources:
Expand All @@ -274,12 +274,12 @@ test(static function (): void {
});

// Register services manually
test(static function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
Toolkit::test(static function (): void {
$loader = new ContainerLoader(Environment::getTestDir(), true);
$class = $loader->load(static function (Compiler $compiler): void {
$compiler->addExtension('autoload', new ResourceExtension());
$compiler->addExtension('inject', new InjectExtension());
$compiler->addConfig(['parameters' => ['appDir' => TESTER_DIR]]);
$compiler->addConfig(['parameters' => ['appDir' => APP_DIR]]);
$compiler->loadConfig(FileMock::create('
autoload:
resources:
Expand Down
3 changes: 1 addition & 2 deletions tests/Fixtures/Bar/BarService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
final class BarService
{

/** @var stdClass */
private $logger;
private stdClass $logger;

public function setLogger(stdClass $logger): void
{
Expand Down
3 changes: 1 addition & 2 deletions tests/Fixtures/Decorator/InjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
class InjectService
{

/** @var Authenticator */
public $authenticator;
public Authenticator $authenticator;

public function injectAuthenticator(Authenticator $authenticator): void
{
Expand Down
3 changes: 1 addition & 2 deletions tests/Fixtures/Foo/FooBarService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
final class FooBarService
{

/** @var FooService */
public $foo;
public FooService $foo;

public function __construct(FooService $foo)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Inject/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ abstract class Base
{

/** @var mixed[] */
public $setup = [];
public array $setup = [];

public function setup(Tester $tester, string $bar): void
{
Expand Down
8 changes: 2 additions & 6 deletions tests/Fixtures/Scalar/Scalar2Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@
final class Scalar2Service
{

/** @var string */
public $text;
public string $text;

/**
* @param string $text
*/
public function __construct($text = 'default')
public function __construct(string $text = 'default')
{
$this->text = $text;
}
Expand Down
8 changes: 2 additions & 6 deletions tests/Fixtures/Scalar/ScalarService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@
final class ScalarService
{

/** @var string */
public $text;
public string $text;

/**
* @param string $text
*/
public function __construct($text)
public function __construct(string $text)
{
$this->text = $text;
}
Expand Down
7 changes: 2 additions & 5 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
<?php declare(strict_types = 1);

use Ninjify\Nunjuck\Environment;
use Contributte\Tester\Environment;

if (@!include __DIR__ . '/../vendor/autoload.php') {
echo 'Install Nette Tester using `composer update --dev`';
exit(1);
}

// Configure environment
Environment::setupTester();
Environment::setupTimezone();
Environment::setupVariables(__DIR__);
Environment::setup(__DIR__);

0 comments on commit 0b5d838

Please sign in to comment.