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

[8.x] Add when() and unless() methods to MailMessage #34814

Merged
merged 1 commit into from
Oct 13, 2020
Merged
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
38 changes: 38 additions & 0 deletions src/Illuminate/Notifications/Messages/MailMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,42 @@ public function withSwiftMessage($callback)

return $this;
}

/**
* Apply the callback's message changes if the given "value" is true.
*
* @param mixed $value
* @param callable $callback
* @param callable|null $default
* @return mixed|$this
*/
public function when($value, $callback, $default = null)
{
if ($value) {
return $callback($this, $value) ?: $this;
} elseif ($default) {
return $default($this, $value) ?: $this;
}

return $this;
}

/**
* Apply the callback's message changes if the given "value" is false.
*
* @param mixed $value
* @param callable $callback
* @param callable|null $default
* @return mixed|$this
*/
public function unless($value, $callback, $default = null)
{
if (! $value) {
return $callback($this, $value) ?: $this;
} elseif ($default) {
return $default($this, $value) ?: $this;
}

return $this;
}
}
118 changes: 118 additions & 0 deletions tests/Notifications/NotificationMailMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,122 @@ public function testCallbackIsSetCorrectly()

$this->assertSame([$callback], $message->callbacks);
}

public function testWhenCallback()
{
$callback = function (MailMessage $mailMessage, $condition) {
$this->assertTrue($condition);

$mailMessage->cc('cc@example.com');
};

$message = new MailMessage();
$message->when(true, $callback);
$this->assertSame([['cc@example.com', null]], $message->cc);

$message = new MailMessage();
$message->when(false, $callback);
$this->assertSame([], $message->cc);
}

public function testWhenCallbackWithReturn()
{
$callback = function (MailMessage $mailMessage, $condition) {
$this->assertTrue($condition);

return $mailMessage->cc('cc@example.com');
};

$message = new MailMessage();
$message->when(true, $callback)->bcc('bcc@example.com');
$this->assertSame([['cc@example.com', null]], $message->cc);
$this->assertSame([['bcc@example.com', null]], $message->bcc);

$message = new MailMessage();
$message->when(false, $callback)->bcc('bcc@example.com');
$this->assertSame([], $message->cc);
$this->assertSame([['bcc@example.com', null]], $message->bcc);
}

public function testWhenCallbackWithDefault()
{
$callback = function (MailMessage $mailMessage, $condition) {
$this->assertEquals('truthy', $condition);

$mailMessage->cc('truthy@example.com');
};

$default = function (MailMessage $mailMessage, $condition) {
$this->assertEquals(0, $condition);

$mailMessage->cc('zero@example.com');
};

$message = new MailMessage();
$message->when('truthy', $callback, $default);
$this->assertSame([['truthy@example.com', null]], $message->cc);

$message = new MailMessage();
$message->when(0, $callback, $default);
$this->assertSame([['zero@example.com', null]], $message->cc);
}

public function testUnlessCallback()
{
$callback = function (MailMessage $mailMessage, $condition) {
$this->assertFalse($condition);

$mailMessage->cc('test@example.com');
};

$message = new MailMessage();
$message->unless(false, $callback);
$this->assertSame([['test@example.com', null]], $message->cc);

$message = new MailMessage();
$message->unless(true, $callback);
$this->assertSame([], $message->cc);
}

public function testUnlessCallbackWithReturn()
{
$callback = function (MailMessage $mailMessage, $condition) {
$this->assertFalse($condition);

return $mailMessage->cc('cc@example.com');
};

$message = new MailMessage();
$message->unless(false, $callback)->bcc('bcc@example.com');
$this->assertSame([['cc@example.com', null]], $message->cc);
$this->assertSame([['bcc@example.com', null]], $message->bcc);

$message = new MailMessage();
$message->unless(true, $callback)->bcc('bcc@example.com');
$this->assertSame([], $message->cc);
$this->assertSame([['bcc@example.com', null]], $message->bcc);
}

public function testUnlessCallbackWithDefault()
{
$callback = function (MailMessage $mailMessage, $condition) {
$this->assertEquals(0, $condition);

$mailMessage->cc('zero@example.com');
};

$default = function (MailMessage $mailMessage, $condition) {
$this->assertEquals('truthy', $condition);

$mailMessage->cc('truthy@example.com');
};

$message = new MailMessage();
$message->unless(0, $callback, $default);
$this->assertSame([['zero@example.com', null]], $message->cc);

$message = new MailMessage();
$message->unless('truthy', $callback, $default);
$this->assertSame([['truthy@example.com', null]], $message->cc);
}
}