1. Go to this page and download the library: Download marko/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/ */
marko / testing example snippets
title="tests/Pest.php"
use Marko\Testing\TestCase;
uses(TestCase::class)->in(__DIR__);
use Marko\Testing\Fake\FakeEventDispatcher;
$dispatcher = new FakeEventDispatcher();
$dispatcher->dispatch(new OrderPlaced($order));
$dispatcher->assertDispatched(OrderPlaced::class);
$dispatcher->assertDispatchedCount(OrderPlaced::class, 1);
$dispatcher->assertNotDispatched(OrderShipped::class);
use Marko\Testing\Fake\FakeMailer;
$mailer = new FakeMailer();
$mailer->send($message);
$mailer->assertSent(WelcomeEmail::class);
$mailer->assertSentCount(WelcomeEmail::class, 1);
$mailer->assertNothingSent();
use Marko\Testing\Fake\FakeQueue;
$queue = new FakeQueue();
$queue->push(new ProcessOrder($order));
$queue->assertPushed(ProcessOrder::class);
$queue->assertPushedCount(ProcessOrder::class, 1);
$queue->assertNotPushed(SendInvoice::class);
$queue->assertNothingPushed();
use Marko\Testing\Fake\FakeSession;
$session = new FakeSession();
$session->put('user_id', 42);
$value = $session->get('user_id'); // 42
$session->forget('user_id');
use Marko\Testing\Fake\FakeCookieJar;
$cookies = new FakeCookieJar();
$cookies->set('token', 'abc123');
$value = $cookies->get('token'); // 'abc123'
use Marko\Testing\Fake\FakeLogger;
$logger = new FakeLogger();
$logger->info('User logged in');
$logger->error('Something failed');
$logger->assertLogged('User logged in');
$logger->assertNothingLogged();
use Marko\Testing\Fake\FakeConfigRepository;
$config = new FakeConfigRepository([
'auth.defaults.guard' => 'web',
'app.name' => 'Marko',
]);
$value = $config->get('auth.defaults.guard'); // 'web'
use Marko\Testing\Fake\FakeAuthenticatable;
$user = new FakeAuthenticatable(id: 1, password: 'hashed-secret');
$user->getAuthIdentifier(); // 1
$user->getAuthPassword(); // 'hashed-secret'
use Marko\Testing\Fake\FakeUserProvider;
use Marko\Testing\Fake\FakeAuthenticatable;
$user = new FakeAuthenticatable(id: 1);
$provider = new FakeUserProvider($user);
$found = $provider->retrieveById(1); // returns $user
use Marko\Testing\Fake\FakeGuard;
use Marko\Testing\Fake\FakeAuthenticatable;
$guard = new FakeGuard(name: 'web', attemptResult: true);
// Set a user directly
$user = new FakeAuthenticatable(id: 1);
$guard->setUser($user);
$guard->assertAuthenticated();
// Test login attempt
$guard->attempt(['email' => '[email protected]', 'password' => 'secret']);
$guard->assertAttempted();
// Assert no user logged in
$guard->logout();
$guard->assertGuest();
$guard->assertLoggedOut();