Skip to content

Commit

Permalink
Simplify code, add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz committed May 2, 2024
1 parent da485d4 commit 6e27246
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 18 deletions.
7 changes: 1 addition & 6 deletions src/Http/FakeHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,15 @@ class FakeHttp
private ?object $actor = null;
private ?SessionInterface $session = null;
private BinderInterface $binder;
private ContainerInterface $container;

public function __construct(
ContainerInterface $container,
#[Proxy] private readonly ContainerInterface $container,
private readonly FileFactory $fileFactory,
private readonly \Closure $scope,
) {
$this->binder = $container
->get(InvokerInterface::class)
->invoke(static fn (#[Proxy] BinderInterface $binder): BinderInterface => $binder);

$this->container = $container
->get(InvokerInterface::class)
->invoke(static fn (#[Proxy] ContainerInterface $container): ContainerInterface => $container);
}

public function withActor(object $actor): self
Expand Down
8 changes: 1 addition & 7 deletions src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,11 @@ protected function runTest(): mixed
return parent::runTest();
}

$previousApp = $this->getApp();

$scopes = \is_array($scope->scope) ? $scope->scope : [$scope->scope];
$result = $this->runScopes($scopes, function (Container $container): mixed {
$this->initApp([...static::ENV, ...$this->getEnvVariablesFromConfig()], $container);

$result = $this->runScopes($scopes, function (): mixed {
return parent::runTest();
}, $this->getContainer(), $scope->bindings);

$this->app = $previousApp;

return $result;
}

Expand Down
10 changes: 5 additions & 5 deletions src/Traits/InteractsWithHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Spiral\Testing\Traits;

use Spiral\Core\FactoryInterface;
use Spiral\Testing\Http\FakeHttp;
use Spiral\Testing\Http\FileFactory;

Expand All @@ -16,12 +17,11 @@ final public function getFileFactory(): FileFactory

final public function fakeHttp(): FakeHttp
{
return new FakeHttp(
$this->getContainer(),
$this->getFileFactory(),
function (\Closure $closure, array $bindings = []) {
return $this->getContainer()->get(FactoryInterface::class)->make(FakeHttp::class, [
'fileFactory' => $this->getFileFactory(),
'scope' => function (\Closure $closure, array $bindings = []) {
return $this->runScoped($closure, $bindings);
}
);
]);
}
}
10 changes: 10 additions & 0 deletions src/Traits/TestableKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@

use Spiral\Boot\DispatcherInterface;
use Spiral\Core\Container;
use Spiral\Core\ContainerScope;
use Spiral\Core\Internal\Introspector;

trait TestableKernel
{
/** @inheritDoc */
public function getContainer(): Container
{
$scopedContainer = ContainerScope::getContainer();
if (
$scopedContainer instanceof Container &&
Introspector::scopeName($scopedContainer) !== Container::DEFAULT_ROOT_SCOPE_NAME
) {
return $scopedContainer;
}

return $this->container;
}

Expand Down
20 changes: 20 additions & 0 deletions tests/src/Attribute/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Spiral\Testing\Tests\Attribute;

use Spiral\Core\Internal\Introspector;
use Spiral\Storage\Config\StorageConfig;
use Spiral\Testing\Attribute\Config;
use Spiral\Testing\Attribute\TestScope;
use Spiral\Testing\Tests\TestCase;

final class ConfigTest extends TestCase
Expand All @@ -31,4 +33,22 @@ public function testMultipleAttributes(): void
$this->assertSame('replaced', $config['default']);
$this->assertSame('test', $config['servers']['static']['directory']);
}

#[TestScope('foo')]
#[Config('storage.default', 'replaced')]
public function testReplaceUsingAttributeInScope(): void
{
$config = $this->getConfig(StorageConfig::CONFIG);
$this->assertSame('replaced', $config['default']);
$this->assertSame(['foo', 'root'], Introspector::scopeNames($this->getContainer()));
}

#[TestScope(['foo', 'bar'])]
#[Config('storage.default', 'replaced')]
public function testReplaceUsingAttributeInNestedScope(): void
{
$config = $this->getConfig(StorageConfig::CONFIG);
$this->assertSame('replaced', $config['default']);
$this->assertSame(['bar', 'foo', 'root'], Introspector::scopeNames($this->getContainer()));
}
}
20 changes: 20 additions & 0 deletions tests/src/Attribute/EnvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Spiral\Testing\Tests\Attribute;

use Spiral\Core\Internal\Introspector;
use Spiral\Testing\Attribute\Env;
use Spiral\Testing\Attribute\TestScope;
use Spiral\Testing\Tests\TestCase;

final class EnvTest extends TestCase
Expand Down Expand Up @@ -34,4 +36,22 @@ public function testMultipleAttributes(): void
$this->assertEnvironmentValueSame('FOO', 'BAZ');
$this->assertEnvironmentValueSame('BAZ', 'BAZ');
}

#[TestScope('foo')]
#[Env('FOO', 'BAZ')]
public function testEnvFromAttributeInScope(): void
{
$this->assertEnvironmentValueSame('FOO', 'BAZ');
$this->assertEnvironmentValueSame('BAZ', 'QUX');
$this->assertSame(['foo', 'root'], Introspector::scopeNames($this->getContainer()));
}

#[TestScope(['foo', 'bar'])]
#[Env('FOO', 'BAZ')]
public function testEnvFromAttributeInNestedScope(): void
{
$this->assertEnvironmentValueSame('FOO', 'BAZ');
$this->assertEnvironmentValueSame('BAZ', 'QUX');
$this->assertSame(['bar', 'foo', 'root'], Introspector::scopeNames($this->getContainer()));
}
}
36 changes: 36 additions & 0 deletions tests/src/Attribute/TestScopeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\Attribute;

use Spiral\Core\Internal\Introspector;
use Spiral\Testing\Attribute\TestScope;
use Spiral\Testing\Tests\TestCase;

final class TestScopeTest extends TestCase
{
public function testDefaultScope(): void
{
$this->assertSame(['root'], Introspector::scopeNames($this->getContainer()));
}

#[TestScope('foo')]
public function testScopeFromAttribute(): void
{
$this->assertSame(['foo', 'root'], Introspector::scopeNames($this->getContainer()));
}

#[TestScope(['foo', 'bar'])]
public function testNestedScopes(): void
{
$this->assertSame(['bar', 'foo', 'root'], Introspector::scopeNames($this->getContainer()));
}

#[TestScope('foo', ['test' => \stdClass::class])]
public function testScopeWithBindings(): void
{
$this->assertSame(['foo', 'root'], Introspector::scopeNames($this->getContainer()));
$this->assertInstanceOf(\stdClass::class, $this->getContainer()->get('test'));
}
}
18 changes: 18 additions & 0 deletions tests/src/Http/FakeHttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Spiral\Testing\Tests\Http;

use PHPUnit\Framework\ExpectationFailedException;
use Spiral\Core\Internal\Introspector;
use Spiral\Testing\Attribute\TestScope;
use Spiral\Testing\Tests\TestCase;

final class FakeHttpTest extends TestCase
Expand Down Expand Up @@ -59,4 +61,20 @@ public function testGetJsonParsedBody(): void
$response->getJsonParsedBody()
);
}

#[TestScope('foo')]
public function testGetWithQueryParamsInScope(): void
{
$response = $this->fakeHttp()->get('/get/query-params', ['foo' => 'bar', 'baz' => ['foo1' => 'bar1']]);
$response->assertBodySame('{"foo":"bar","baz":{"foo1":"bar1"}}');
$this->assertSame(['foo', 'root'], Introspector::scopeNames($this->getContainer()));
}

#[TestScope(['foo', 'bar'])]
public function testGetWithQueryParamsInNestedScope(): void
{
$response = $this->fakeHttp()->get('/get/query-params', ['foo' => 'bar', 'baz' => ['foo1' => 'bar1']]);
$response->assertBodySame('{"foo":"bar","baz":{"foo1":"bar1"}}');
$this->assertSame(['bar', 'foo', 'root'], Introspector::scopeNames($this->getContainer()));
}
}

0 comments on commit 6e27246

Please sign in to comment.