Skip to content

Commit

Permalink
Fixes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
butschster committed Sep 7, 2022
1 parent 0bd49db commit 41bffcf
Show file tree
Hide file tree
Showing 17 changed files with 56 additions and 76 deletions.
2 changes: 1 addition & 1 deletion src/Broadcasting/src/Config/BroadcastConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getAliases(): array
*/
public function getDefaultConnection(): string
{
if (!isset($this->config['default']) || empty($this->config['default'])) {
if (empty($this->config['default'])) {
throw new InvalidArgumentException('Default broadcast connection is not defined.');
}

Expand Down
10 changes: 0 additions & 10 deletions src/Broadcasting/tests/Config/BroadcastConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,6 @@ public function testGetsDefaultConnection(): void
);
}

public function testNotDefinedDefaultKeyShouldThrowAnException(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectErrorMessage('Default broadcast connection is not defined.');

$config = new BroadcastConfig();

$config->getDefaultConnection();
}

public function testGetsConnectionConfigByName(): void
{
$this->assertSame(
Expand Down
6 changes: 1 addition & 5 deletions src/Cache/src/Config/CacheConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,14 @@ final class CacheConfig extends InjectableConfig
*/
public function getAliases(): array
{
return (array)($this->config['aliases'] ?? []);
return $this->config['aliases'];
}

/**
* Get default cache storage
*/
public function getDefaultStorage(): string
{
if (!isset($this->config['default']) || empty($this->config['default'])) {
throw new InvalidArgumentException('Default cache storage is not defined.');
}

if (!\is_string($this->config['default'])) {
throw new InvalidArgumentException('Default cache storage config value must be a string');
}
Expand Down
10 changes: 0 additions & 10 deletions src/Cache/tests/Config/CacheConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,6 @@ public function testGetdDefaultDriver(): void
);
}

public function testNotDefinedDefaultKeyShouldThrowAnException(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectErrorMessage('Default cache storage is not defined.');

$config = new CacheConfig();

$config->getDefaultStorage();
}

public function testGetsStorageConfigByStorageName(): void
{
$this->assertSame(
Expand Down
13 changes: 11 additions & 2 deletions src/Console/src/StaticLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace Spiral\Console;

use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Spiral\Console\Config\ConsoleConfig;
use Spiral\Console\Traits\LazyTrait;
use Spiral\Core\Container;
use Spiral\Events\EventDispatcherAwareInterface;
use Symfony\Component\Console\Command\Command as SymfonyCommand;

final class StaticLocator implements LocatorInterface
Expand All @@ -20,8 +22,11 @@ final class StaticLocator implements LocatorInterface
public function __construct(
private readonly array $commands,
private ConsoleConfig $config,
private ContainerInterface $container = new Container()
ContainerInterface $container = new Container(),
?EventDispatcherInterface $eventDispatcher = null
) {
$this->dispatcher = $eventDispatcher;
$this->container = $container;
}

/**
Expand All @@ -36,9 +41,13 @@ public function locateCommands(): array
continue;
}

$commands[] = $this->supportsLazyLoading($command)
$commands[] = $command = $this->supportsLazyLoading($command)
? $this->createLazyCommand($command)
: $this->container->get($command);

if ($this->dispatcher !== null && $command instanceof EventDispatcherAwareInterface) {
$command->setEventDispatcher($this->dispatcher);
}
}

return $commands;
Expand Down
7 changes: 7 additions & 0 deletions src/Console/src/Traits/LazyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
namespace Spiral\Console\Traits;

use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Spiral\Console\Command as SpiralCommand;
use Spiral\Console\Config\ConsoleConfig;
use Spiral\Events\EventDispatcherAwareInterface;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Command\LazyCommand;

trait LazyTrait
{
private ContainerInterface $container;
private ConsoleConfig $config;
private ?EventDispatcherInterface $dispatcher = null;

/**
* Check if command can be lazy-loaded.
Expand Down Expand Up @@ -45,6 +48,10 @@ function () use ($class): SymfonyCommand {
$command->setContainer($this->container);
$command->setInterceptors($this->config->getInterceptors());

if ($this->dispatcher !== null && $command instanceof EventDispatcherAwareInterface) {
$command->setEventDispatcher($this->dispatcher);
}

return $command;
}
);
Expand Down
10 changes: 6 additions & 4 deletions src/Console/tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Spiral\Tests\Console;

use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
use Spiral\Console\CommandLocator;
use Spiral\Console\Config\ConsoleConfig;
use Spiral\Console\Console;
Expand Down Expand Up @@ -41,14 +42,15 @@ public function setUp(): void
);
}

protected function getCore(LocatorInterface $locator = null): Console
protected function getCore(LocatorInterface $locator = null, ?EventDispatcherInterface $eventDispatcher = null): Console
{
$config = $this->container->get(ConsoleConfig::class);

return new Console(
$config,
$locator ?? $this->getStaticLocator([]),
$this->container
config: $config,
locator: $locator ?? $this->getStaticLocator([]),
container: $this->container,
dispatcher: $eventDispatcher
);
}

Expand Down
13 changes: 7 additions & 6 deletions src/Console/tests/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ public function testEventsShouldBeDispatched(): void
->method('dispatch')
->with(
$this->callback(
static fn (CommandStarting|CommandFinished $event): bool => $event->command instanceof TestCommand
static fn(CommandStarting|CommandFinished $event): bool => $event->command instanceof TestCommand
),
);

$this->container->bind(EventDispatcherInterface::class, $dispatcher);

$core = $this->getCore($this->getStaticLocator([
TestCommand::class
]));
$core = $this->getCore(
locator: $this->getStaticLocator([
new TestCommand(),
]),
eventDispatcher: $dispatcher
);

$core->run('test');
}
Expand Down
13 changes: 7 additions & 6 deletions src/Csrf/src/Config/CsrfConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ final class CsrfConfig extends InjectableConfig
public const CONFIG = 'csrf';

protected array $config = [
'cookie' => 'csrf-token',
'length' => 16,
'lifetime' => 86400,
'cookie' => 'csrf-token',
'length' => 16,
'lifetime' => null,
'sameSite' => null,
];

public function getTokenLength(): int
Expand All @@ -29,16 +30,16 @@ public function getCookie(): string

public function getCookieLifetime(): ?int
{
return $this->config['lifetime'] ?? null;
return $this->config['lifetime'];
}

public function isCookieSecure(): bool
{
return !empty($this->config['secure']);
return ! empty($this->config['secure']);
}

public function getSameSite(): ?string
{
return $this->config['sameSite'] ?? null;
return $this->config['sameSite'];
}
}
12 changes: 10 additions & 2 deletions src/Framework/Console/CommandLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Spiral\Console;

use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Spiral\Console\Config\ConsoleConfig;
use Spiral\Console\Traits\LazyTrait;
use Spiral\Events\EventDispatcherAwareInterface;
use Spiral\Tokenizer\ScopedClassesInterface;
use Symfony\Component\Console\Command\Command as SymfonyCommand;

Expand All @@ -17,9 +19,11 @@ final class CommandLocator implements LocatorInterface
public function __construct(
private readonly ScopedClassesInterface $classes,
private ConsoleConfig $config,
ContainerInterface $container
ContainerInterface $container,
?EventDispatcherInterface $eventDispatcher = null
) {
$this->container = $container;
$this->dispatcher = $eventDispatcher;
}

public function locateCommands(): array
Expand All @@ -30,9 +34,13 @@ public function locateCommands(): array
continue;
}

$commands[] = $this->supportsLazyLoading($class->getName())
$commands[] = $command = $this->supportsLazyLoading($class->getName())
? $this->createLazyCommand($class->getName())
: $this->container->get($class->getName());

if ($this->dispatcher !== null && $command instanceof EventDispatcherAwareInterface) {
$command->setEventDispatcher($this->dispatcher);
}
}

return $commands;
Expand Down
2 changes: 1 addition & 1 deletion src/Http/src/Config/HttpConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getBaseHeaders(): array
*/
public function getMiddleware(): array
{
return $this->config['middleware'] ?? $this->config['middlewares'];
return $this->config['middleware'];
}

public function getInputBags(): array
Expand Down
9 changes: 0 additions & 9 deletions src/Http/tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,4 @@ public function testBaseMiddleware(): void

$this->assertSame([TestMiddleware::class], $c->getMiddleware());
}

public function testBaseMiddlewareFallback(): void
{
$c = new HttpConfig([
'middlewares' => [TestMiddleware::class]
]);

$this->assertSame([TestMiddleware::class], $c->getMiddleware());
}
}
6 changes: 1 addition & 5 deletions src/Queue/src/Config/QueueConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class QueueConfig extends InjectableConfig
*/
public function getAliases(): array
{
return (array)($this->config['aliases'] ?? []);
return $this->config['aliases'];
}

/**
Expand Down Expand Up @@ -64,10 +64,6 @@ public function getPushInterceptors(): array
*/
public function getDefaultDriver(): string
{
if (!isset($this->config['default']) || empty($this->config['default'])) {
throw new InvalidArgumentException('Default queue connection is not defined.');
}

if (!\is_string($this->config['default'])) {
throw new InvalidArgumentException('Default queue connection config value must be a string');
}
Expand Down
10 changes: 0 additions & 10 deletions src/Queue/tests/Config/QueueConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,6 @@ public function testGetsDefaultDriver(): void
$this->assertSame('foo', $config->getDefaultDriver());
}

public function testGetsEmptyDefaultDriverShouldThrowAnException(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectErrorMessage('Default queue connection is not defined.');

$config = new QueueConfig();

$config->getDefaultDriver();
}

public function testGetsNonStringDefaultDriverShouldThrowAnException(): void
{
$this->expectException(InvalidArgumentException::class);
Expand Down
4 changes: 2 additions & 2 deletions src/SendIt/tests/App/MailInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@

class MailInterceptor implements MailerInterface
{
private $last;
private RawMessage $last;

public function send(RawMessage $message, Envelope $envelope = null): void
{
$this->last = $message;
}

public function getLast(): Email
public function getLast(): RawMessage
{
return $this->last;
}
Expand Down
2 changes: 1 addition & 1 deletion src/SendIt/tests/RenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Spiral\Mailer\Message;

/**
* @requires function \Spiral\Framework\Kernel::init
* @requires function \Spiral\Framework\Kernel::create
*/
class RenderTest extends TestCase
{
Expand Down
3 changes: 1 addition & 2 deletions src/Translator/src/Config/TranslatorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class TranslatorConfig extends InjectableConfig
/**
* @psalm-var array{
* locale: string,
* fallbackLocale: string,
* fallbackLocale?: string,
* directory: string,
* cacheLocales: bool,
* autoRegister: bool,
Expand All @@ -31,7 +31,6 @@ final class TranslatorConfig extends InjectableConfig
*/
protected array $config = [
'locale' => '',
'fallbackLocale' => '',
'directory' => '',
'cacheLocales' => true,
'autoRegister' => true,
Expand Down

0 comments on commit 41bffcf

Please sign in to comment.