Created
August 23, 2022 16:32
-
-
Save rrolla/fec9fd1b1ab1399e013b5054e382d7e4 to your computer and use it in GitHub Desktop.
Compare unit tests for simple if vs laravel when
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
to run
vendor/bin/phpunit tests/IfVsWhenTest.php --process-isolation