Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/packages/messenger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ framework:
default_bus: messenger.bus.default
buses:
messenger.bus.default: ~
transports:
async: 'in-memory://'
routing:
App\Message\SendWelcomeMessage: async
15 changes: 15 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
use App\Controller\DashboardController;
use App\Controller\DomCrawlerController;
use App\Controller\ExternalApiController;
use App\Controller\FlashController;
use App\Controller\FormController;
use App\Controller\GreetingController;
use App\Controller\HomeController;
use App\Controller\HttpClientController;
use App\Controller\RegistrationController;
use App\Controller\RunQueriesController;
use App\Controller\SecurityController;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

Expand Down Expand Up @@ -109,4 +112,16 @@
$routes->add('dispatch_message', '/dispatch-message')
->controller(App\Controller\DispatchMessageController::class)
->methods(['GET']);

$routes->add('app_flash', '/flash')
->controller(FlashController::class)
->methods(['GET']);

$routes->add('app_greeting', '/greeting')
->controller(GreetingController::class)
->methods(['GET']);

$routes->add('app_run_queries', '/run-queries')
->controller(RunQueriesController::class)
->methods(['GET']);
};
5 changes: 5 additions & 0 deletions config/services_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use App\Service\Greeting;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

Expand All @@ -10,4 +11,8 @@

$services->alias(Security::class, 'security.helper')
->public();

$services->set(Greeting::class)
->autowire()
->public();
};
45 changes: 45 additions & 0 deletions src/Command/ResultCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace App\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(ResultCommand::COMMAND_NAME, 'A command exercising the execution result paths.')]
final class ResultCommand extends Command
{
public const COMMAND_NAME = 'app:result-command';

protected function configure(): void
{
$this->addOption('fail', null, InputOption::VALUE_NONE);
$this->addOption('invalid', null, InputOption::VALUE_NONE);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
if ($input->getOption('invalid')) {
$output->writeln('Invalid input.');

return Command::INVALID;
}

if ($input->getOption('fail')) {
if ($output instanceof ConsoleOutputInterface) {
$output->getErrorOutput()->writeln('Something failed.');
}

return Command::FAILURE;
}

$output->writeln('All good.');

return Command::SUCCESS;
}
}
18 changes: 18 additions & 0 deletions src/Controller/FlashController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;

final class FlashController extends AbstractController
{
public function __invoke(): RedirectResponse
{
$this->addFlash('success', 'Welcome back!');

return $this->redirectToRoute('index');
}
}
22 changes: 22 additions & 0 deletions src/Controller/GreetingController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace App\Controller;

use App\Service\Greeting;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

final class GreetingController extends AbstractController
{
public function __construct(
private readonly Greeting $greeting,
) {
}

public function __invoke(): Response
{
return new Response($this->greeting->greet());
}
}
25 changes: 25 additions & 0 deletions src/Controller/RunQueriesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\Controller;

use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

final class RunQueriesController extends AbstractController
{
public function __construct(
private readonly UserRepository $users,
) {
}

public function __invoke(): Response
{
$this->users->findAll();
$this->users->getByEmail('john_doe@gmail.com');

return new Response('Queries executed.');
}
}
13 changes: 13 additions & 0 deletions src/Service/Greeting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Service;

class Greeting
{
public function greet(): string
{
return 'Hello world!';
}
}
12 changes: 12 additions & 0 deletions tests/Functional/ConsoleCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Tests\Functional;

use App\Command\ExampleCommand;
use App\Command\ResultCommand;
use App\Tests\Support\FunctionalTester;

final class ConsoleCest
Expand All @@ -29,4 +30,15 @@ public function runSymfonyConsoleCommand(FunctionalTester $I)
);
$I->assertStringContainsString('Bye world!', $output);
}

public function consoleExecutionResult(FunctionalTester $I): void
{
$I->assertCommandIsSuccessful($I->runCommand(ResultCommand::COMMAND_NAME));
$I->assertCommandFailed($I->runCommand(ResultCommand::COMMAND_NAME, ['--fail' => true]));
$I->assertCommandIsInvalid($I->runCommand(ResultCommand::COMMAND_NAME, ['--invalid' => true]));

$result = $I->runCommand(ResultCommand::COMMAND_NAME, ['--fail' => true]);
$I->assertStringContainsString('Something failed.', $result->getErrorOutput());
$I->assertCommandResultEquals($result, expectedStatusCode: 1);
}
}
7 changes: 7 additions & 0 deletions tests/Functional/DoctrineCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,11 @@ public function seeNumRecords(FunctionalTester $I)
{
$I->seeNumRecords(1, User::class);
}

public function queryCountAssertions(FunctionalTester $I): void
{
$I->amOnPage('/run-queries');
$I->seeNumQueriesIsLessThan(10);
$I->dontSeeDuplicateQueries();
}
}
13 changes: 13 additions & 0 deletions tests/Functional/MessengerCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,17 @@ public function noMessagesDispatched(FunctionalTester $I): void
$I->seeDispatchedMessageCount(0);
$I->assertSame([], $I->grabDispatchedMessageClasses());
}

public function messengerTransport(FunctionalTester $I): void
{
$I->amOnPage('/dispatch-message');
$I->seeMessengerQueueCount(1, 'async');
$I->seeMessengerTransportContains(SendWelcomeMessage::class, 'async');

$envelope = $I->grabMessengerTransport('async')->getSent()[0];
$I->assertInstanceOf(SendWelcomeMessage::class, $envelope->getMessage());

$I->consumeMessengerMessages('async');
$I->assertCount(1, $I->grabMessengerTransport('async')->getAcknowledged());
}
}
14 changes: 14 additions & 0 deletions tests/Functional/ServicesCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Tests\Functional;

use App\Tests\Support\FunctionalTester;
use App\Service\Greeting;
use Symfony\Bundle\SecurityBundle\Security;

final class ServicesCest
Expand All @@ -14,4 +15,17 @@ public function grabService(FunctionalTester $I)
$security = $I->grabService('security.helper');
$I->assertInstanceOf(Security::class, $security);
}

public function mockService(FunctionalTester $I): void
{
$I->mockService(Greeting::class, new class extends Greeting {
public function greet(): string
{
return 'Mocked greeting!';
}
});

$I->amOnPage('/greeting');
$I->see('Mocked greeting!');
}
}
8 changes: 8 additions & 0 deletions tests/Functional/SessionCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,12 @@ public function seeSessionHasValues(FunctionalTester $I)

$I->seeSessionHasValues(['_security_main', '_security_main']);
}

public function assertSessionHasFlashMessage(FunctionalTester $I): void
{
$I->stopFollowingRedirects();
$I->amOnPage('/flash');
$I->assertSessionHasFlashMessage('success');
$I->assertSessionHasFlashMessage('success', 'Welcome back!');
}
}
Loading