Skip to content

Instantly share code, notes, and snippets.

@rrolla
Created August 23, 2022 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rrolla/fec9fd1b1ab1399e013b5054e382d7e4 to your computer and use it in GitHub Desktop.
Save rrolla/fec9fd1b1ab1399e013b5054e382d7e4 to your computer and use it in GitHub Desktop.
Compare unit tests for simple if vs laravel when
<?php
declare(strict_types=1);
namespace Tests;
use App\Models\Postcodes\Postcode\Postcode;
use Closure;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Mockery;
class IfVsWhenTest extends \PHPUnit\Framework\TestCase
{
public function testOptionWithIf()
{
$postcodeModelMock = Mockery::mock(Postcode::class);
$eloquentBuilderMock = Mockery::mock(EloquentBuilder::class);
$postcodeId = mt_rand();
$isPublished = (bool)mt_rand(0, 1);
$expectedResult = EloquentCollection::make($postcodeId);
$postcodeModelMock->shouldReceive('query')->once()->andReturn($eloquentBuilderMock);
$eloquentBuilderMock->shouldReceive('where')
->with('is_published', '=', $isPublished)
->once()
->andReturnSelf();
$eloquentBuilderMock->shouldReceive('get')->once()->andReturn($expectedResult);
$this->assertSame($expectedResult, (new TestClass($postcodeModelMock))->optionWithIf($isPublished));
}
public function testOptionWithWhen()
{
$postcodeModelMock = Mockery::mock(Postcode::class);
$eloquentBuilderMock = Mockery::mock(EloquentBuilder::class);
$postcodeId = mt_rand();
$isPublished = (bool)mt_rand(0, 1);
$expectedResult = EloquentCollection::make($postcodeId);
$postcodeModelMock->shouldReceive('query')->once()->andReturn($eloquentBuilderMock);
$eloquentBuilderMock->shouldReceive('when')
->with($isPublished, Mockery::on(function (Closure $builderClosure) use ($isPublished) {
$eloquentBuilderMock = Mockery::mock(EloquentBuilder::class);
$eloquentBuilderMock->shouldReceive('where')
->with('is_published', '=', $isPublished)
->once()
->andReturnSelf();
$builderClosure($eloquentBuilderMock);
return is_callable($builderClosure);
}))
->andReturnSelf();
$eloquentBuilderMock->shouldReceive('get')->once()->andReturn($expectedResult);
$this->assertSame($expectedResult, (new TestClass($postcodeModelMock))->optionWithWhen($isPublished));
}
}
class TestClass
{
public function __construct(private readonly Postcode $postcodeModel)
{
}
public function optionWithIf(bool $isPublished): EloquentCollection
{
$query = $this->postcodeModel->query();
if ($isPublished) {
$query->where('is_published', '=', $isPublished);
}
return $query->get();
}
public function optionWithWhen(bool $isPublished): EloquentCollection
{
return $this->postcodeModel->query()
->when($isPublished, fn(EloquentBuilder $query) => $query->where('is_published', '=', $isPublished))
->get();
}
}
@rrolla
Copy link
Author

rrolla commented Aug 23, 2022

to run

vendor/bin/phpunit tests/IfVsWhenTest.php --process-isolation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment