Skip to content

Instantly share code, notes, and snippets.

@alexander-schranz
Last active October 17, 2024 15:57
Show Gist options
  • Save alexander-schranz/d9180c39d3da19968dbfb5bdcd2ab690 to your computer and use it in GitHub Desktop.
Save alexander-schranz/d9180c39d3da19968dbfb5bdcd2ab690 to your computer and use it in GitHub Desktop.
In Memory Service Examples you don't require Mocks
<?php
declare(strict_types=1);
namespace App\Tests\Unit;
use Sulu\Bundle\ActivityBundle\Application\Collector\DomainEventCollectorInterface;
use Sulu\Bundle\ActivityBundle\Domain\Event\DomainEvent;
class InMemoryDomainEventDispatcher implements DomainEventCollectorInterface
{
/**
* @var DomainEvent[]
*/
private array $domainEvents = [];
public function collect(DomainEvent $domainEvent): void
{
$this->domainEvents[] = $domainEvent;
}
public function clear(): void
{
throw new \RuntimeException('Not implemented');
}
public function dispatch(): void
{
throw new \RuntimeException('Not implemented');
}
/**
* @return DomainEvent[]
*/
public function getDomainEvents(): array
{
return $this->domainEvents;
}
}
<?php
declare(strict_types=1);
namespace App\Tests\Unit;
use Psr\EventDispatcher\EventDispatcherInterface;
class InMemoryEventDispatcher implements EventDispatcherInterface
{
/**
* @var object[]
*/
private array $events = [];
public function dispatch(object $event): void
{
$this->events[] = $event;
}
public function clear(): void
{
$this->events = [];
}
/**
* @return object[]
*/
public function getEvents(): array
{
return $this->events;
}
}
<?php
declare(strict_types=1);
namespace App\Tests\Unit;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\HandledStamp;
/**
* Used inside tests of process manager or unit tests of controller to check if a certain message was dispatched.
*/
class InMemoryMessageBus implements MessageBusInterface
{
/** @var object[] */
private array $messages = [];
public function dispatch(object $message, array $stamps = []): Envelope
{
if ($message instanceof Envelope) {
$envelope = $message;
$message = $message->getMessage();
} else {
$envelope = new Envelope($message, $stamps);
}
$this->messages[] = $message;
$envelope = $envelope->with(new HandledStamp(new \stdClass(), \stdClass::class));
return $envelope;
}
/** @return object[] */
public function getMessages(): array
{
return $this->messages;
}
}
<?php
declare(strict_types=1);
namespace App\Tests\Unit;
use Symfony\Contracts\Translation\TranslatorInterface;
class InMemoryTranslator implements TranslatorInterface
{
/**
* @param array<string, string> $messages
*/
public function __construct(
private array $messages,
) {
}
public function trans(string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string
{
return $this->messages[$id] ?? $id;
}
public function getLocale(): string
{
return 'en';
}
}
// Flysystem Memory Adapter: https://github.com/thephpleague/flysystem-memory
// Seal Memory Adapter: https://github.com/schranz-search/seal-memory-adapter
// Mock Http Client: https://symfony.com/doc/current/http_client.html#testing
// The MockHttpClient: can be used to replace PSR clients inside third party e.g. Brevo SDK, ...
// So the SDK itself is tested and only the Http Request mocked
// If possible you should still test external APIs inside the CI and not mock them away
// Payment / Stripe:
// Create your own PaymentInterface and have tested Functional and InMemory implementation for testing
// Log
// PSR NullLogger if you don't care about the logs
// Symfony BufferingLogger if you want get logged messages
// Things I still mock:
///
// Repositories
// See https://danielrotter.at/2023/09/22/avoid-mocking-repositories-by-using-in-memory-implementations.html if you want avoid also that
// Symfony FormFactory and FormBuilders:
// Edge case should normally be part of your functional controller tests
// In my case I have own Custom FormFactory services which are Unit tested to test Object normalization
// Things you should never mock or fake
// Domain Objects / Entities
// Value Objects
// Symfony or PSR Request or Response
// Events objects
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment