PHP code example of ez-php / testing

1. Go to this page and download the library: Download ez-php/testing library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ez-php / testing example snippets


use EzPhp\Testing\EntityFactory;

$factory = new EntityFactory(User::class, $userRepo, [
    'name'  => 'Alice',
    'email' => fn () => uniqid('user_') . '@example.com',
]);

$user  = $factory->make();             // not persisted
$user  = $factory->create();           // persisted via $userRepo->save()
$users = $factory->makeMany(3);
$users = $factory->createMany(5, ['role' => 'admin']);

use EzPhp\Testing\Fake\{FakeQueue, FakeMailer, FakeChannel, EventSpy, FakeStorage};

$queue = new FakeQueue();                       // QueueInterface (ez-php/contracts)
(new Signup($queue))->register('[email protected]');
$queue->assertPushed(SendWelcomeMail::class, fn ($job) => $job->to === '[email protected]');
$queue->assertPushedTimes(SendWelcomeMail::class, 1);
$queue->assertNothingPushed();                  // or assertNotPushed(Class::class)

$mailer = new FakeMailer();                     // MailerInterface (ez-php/mail)
$mailer->assertSent(WelcomeMail::class, fn ($m) => $m->getToAddress() === '[email protected]');
$mailer->assertSentCount(1);  $mailer->assertNothingSent();

$channel = new FakeChannel();                   // notification channel (ez-php/notification)
$channel->assertSentTo($user, InvoicePaid::class);

$spy = EventSpy::attachTo($dispatcher);         // ez-php/events — real listeners still run
$spy->assertDispatched(UserRegistered::class);  // assertDispatchedTimes / assertNotDispatched / assertNothingDispatched

$storage = new FakeStorage();                   // StorageInterface over storage's InMemoryDriver
$storage->assertExists('avatars/1.png');        // assertMissing / assertContents / assertEmpty
bash
composer