Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test to attach and detach observer with anonymous classes #3

Open
wants to merge 1 commit into
base: observer4
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions observer/tests/AttachObserverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Styde\Tests;

use SplObserver;
use Styde\Registration;
use Styde\Observers\SendWelcomeEmail;
use Styde\Observers\LogUserRegistration;

class AttachObserverTest extends TestCase
{
/** @test */
function it_can_detach_observer()
{
$logger = $this->loggerFake('test-log-registration.txt');

$mailer = $this->mailerFake();

$registration = new Registration;
$mail = new class ($logger) extends LogUserRegistration implements SplObserver{};
$log = new class ($mailer) extends SendWelcomeEmail implements SplObserver{};
$registration->attach($log);
$registration->attach($mail);

$registration->detach($mail);

$result = $registration->create([
'name' => 'Duilio',
'email' => '[email protected]',
'password' => 'laravel',
]);

$this->assertTrue($result);

// $this->assertDatabaseHas('users', [...])

$logger->assertFileEquals('User Duilio <[email protected]> was created');

$mailer->assertSentTimes(0);
}
}