Skip to content

[9.x] Simplify Conditionable tests #37657

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

Merged
merged 1 commit into from
Jun 11, 2021
Merged
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
84 changes: 32 additions & 52 deletions tests/Support/SupportConditionableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,107 +2,87 @@

namespace Illuminate\Tests\Support;

use Exception;
use Illuminate\Support\Traits\Conditionable;
use PHPUnit\Framework\TestCase;

class SupportConditionableTest extends TestCase
{
public function testWhenConditionCallback()
{
$object = (new CustomConditionableObject())
->when(2, function ($object, $condition) {
$object->on();
$this->assertEquals(2, $condition);
$logger = (new ConditionableLogger())
->when(2, function ($logger, $condition) {
$logger->log('when', $condition);
}, function () {
throw new Exception('when() should not trigger default callback on a truthy value');
$logger->log('default', $condition);
});

$this->assertTrue($object->enabled);
$this->assertSame(['when', 2], $logger->values);
}

public function testWhenDefaultCallback()
{
$object = (new CustomConditionableObject())
$logger = (new ConditionableLogger())
->when(null, function () {
throw new Exception('when() should not trigger on a falsy value');
}, function ($object, $condition) {
$object->on();
$this->assertNull($condition);
$logger->log('when', $condition);
}, function ($logger, $condition) {
$logger->log('default', $condition);
});

$this->assertTrue($object->enabled);
$this->assertSame(['default', null], $logger->values);
}

public function testUnlessConditionCallback()
{
$object = (new CustomConditionableObject())
->unless(null, function ($object, $condition) {
$object->on();
$this->assertNull($condition);
$logger = (new ConditionableLogger())
->unless(null, function ($logger, $condition) {
$logger->log('unless', $condition);
}, function () {
throw new Exception('unless() should not trigger default callback on a falsy value');
$logger->log('default', $condition);
});

$this->assertTrue($object->enabled);
$this->assertSame(['unless', null], $logger->values);
}

public function testUnlessDefaultCallback()
{
$object = (new CustomConditionableObject())
$logger = (new ConditionableLogger())
->unless(2, function () {
throw new Exception('unless() should not trigger on a truthy value');
}, function ($object, $condition) {
$object->on();
$this->assertEquals(2, $condition);
$logger->log('unless', $condition);
}, function ($logger, $condition) {
$logger->log('default', $condition);
});

$this->assertTrue($object->enabled);
$this->assertSame(['default', 2], $logger->values);
}

public function testWhenProxy()
{
$object = (new CustomConditionableObject())->when(true)->on();
$logger = (new ConditionableLogger())
->when(true)->log('one')
->when(false)->log('two');

$this->assertInstanceOf(CustomConditionableObject::class, $object);
$this->assertTrue($object->enabled);

$object = (new CustomConditionableObject())->when(false)->on();

$this->assertInstanceOf(CustomConditionableObject::class, $object);
$this->assertFalse($object->enabled);
$this->assertSame(['one'], $logger->values);
}

public function testUnlessProxy()
{
$object = (new CustomConditionableObject())->unless(false)->on();

$this->assertInstanceOf(CustomConditionableObject::class, $object);
$this->assertTrue($object->enabled);
$logger = (new ConditionableLogger())
->unless(true)->log('one')
->unless(false)->log('two');

$object = (new CustomConditionableObject())->unless(true)->on();

$this->assertInstanceOf(CustomConditionableObject::class, $object);
$this->assertFalse($object->enabled);
$this->assertSame(['two'], $logger->values);
}
}

class CustomConditionableObject
class ConditionableLogger
{
use Conditionable;

public $enabled = false;

public function on()
{
$this->enabled = true;

return $this;
}
public $values = [];

public function off()
public function log(...$values)
{
$this->enabled = false;
array_push($this->values, ...$values);

return $this;
}
Expand Down