1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
<?php
namespace Illuminate\Tests\Database;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Database\Query\Builder as BaseBuilder;
use Illuminate\Database\Query\Grammars\Grammar;
use Illuminate\Database\Query\Processors\Processor;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use stdClass;
class DatabaseSoftDeletingScopeTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
public function testApplyingScopeToABuilder()
{
$scope = m::mock(SoftDeletingScope::class.'[extend]');
$builder = m::mock(EloquentBuilder::class);
$model = m::mock(Model::class);
$model->shouldReceive('getQualifiedDeletedAtColumn')->once()->andReturn('table.deleted_at');
$builder->shouldReceive('whereNull')->once()->with('table.deleted_at');
$scope->apply($builder, $model);
}
public function testRestoreExtension()
{
$builder = new EloquentBuilder(new BaseBuilder(
m::mock(ConnectionInterface::class),
m::mock(Grammar::class),
m::mock(Processor::class)
));
$scope = new SoftDeletingScope;
$scope->extend($builder);
$callback = $builder->getMacro('restore');
$givenBuilder = m::mock(EloquentBuilder::class);
$givenBuilder->shouldReceive('withTrashed')->once();
$givenBuilder->shouldReceive('getModel')->once()->andReturn($model = m::mock(stdClass::class));
$model->shouldReceive('getDeletedAtColumn')->once()->andReturn('deleted_at');
$givenBuilder->shouldReceive('update')->once()->with(['deleted_at' => null]);
$callback($givenBuilder);
}
public function testRestoreOrCreateExtension()
{
$builder = new EloquentBuilder(new BaseBuilder(
m::mock(ConnectionInterface::class),
m::mock(Grammar::class),
m::mock(Processor::class)
));
$scope = new SoftDeletingScope;
$scope->extend($builder);
$callback = $builder->getMacro('restoreOrCreate');
$givenBuilder = m::mock(EloquentBuilder::class);
$givenBuilder->shouldReceive('withTrashed')->once();
$attributes = ['name' => 'foo'];
$values = ['email' => 'bar'];
$givenBuilder->shouldReceive('firstOrCreate')->once()->with($attributes, $values)->andReturn($model = m::mock(Model::class));
$model->shouldReceive('restore')->once()->andReturn(true);
$result = $callback($givenBuilder, $attributes, $values);
$this->assertEquals($model, $result);
}
public function testCreateOrRestoreExtension()
{
$builder = new EloquentBuilder(new BaseBuilder(
m::mock(ConnectionInterface::class),
m::mock(Grammar::class),
m::mock(Processor::class)
));
$scope = new SoftDeletingScope;
$scope->extend($builder);
$callback = $builder->getMacro('createOrRestore');
$givenBuilder = m::mock(EloquentBuilder::class);
$givenBuilder->shouldReceive('withTrashed')->once();
$attributes = ['name' => 'foo'];
$values = ['email' => 'bar'];
$givenBuilder->shouldReceive('createOrFirst')->once()->with($attributes, $values)->andReturn($model = m::mock(Model::class));
$model->shouldReceive('restore')->once()->andReturn(true);
$result = $callback($givenBuilder, $attributes, $values);
$this->assertEquals($model, $result);
}
public function testWithTrashedExtension()
{
$builder = new EloquentBuilder(new BaseBuilder(
m::mock(ConnectionInterface::class),
m::mock(Grammar::class),
m::mock(Processor::class)
));
$scope = m::mock(SoftDeletingScope::class.'[remove]');
$scope->extend($builder);
$callback = $builder->getMacro('withTrashed');
$givenBuilder = m::mock(EloquentBuilder::class);
$givenBuilder->shouldReceive('getModel')->andReturn($model = m::mock(Model::class));
$givenBuilder->shouldReceive('withoutGlobalScope')->with($scope)->andReturn($givenBuilder);
$result = $callback($givenBuilder);
$this->assertEquals($givenBuilder, $result);
}
public function testOnlyTrashedExtension()
{
$builder = new EloquentBuilder(new BaseBuilder(
m::mock(ConnectionInterface::class),
m::mock(Grammar::class),
m::mock(Processor::class)
));
$model = m::mock(Model::class);
$model->makePartial();
$scope = m::mock(SoftDeletingScope::class.'[remove]');
$scope->extend($builder);
$callback = $builder->getMacro('onlyTrashed');
$givenBuilder = m::mock(EloquentBuilder::class);
$givenBuilder->shouldReceive('getQuery')->andReturn($query = m::mock(stdClass::class));
$givenBuilder->shouldReceive('getModel')->andReturn($model);
$givenBuilder->shouldReceive('withoutGlobalScope')->with($scope)->andReturn($givenBuilder);
$model->shouldReceive('getQualifiedDeletedAtColumn')->andReturn('table.deleted_at');
$givenBuilder->shouldReceive('whereNotNull')->once()->with('table.deleted_at');
$result = $callback($givenBuilder);
$this->assertEquals($givenBuilder, $result);
}
public function testWithoutTrashedExtension()
{
$builder = new EloquentBuilder(new BaseBuilder(
m::mock(ConnectionInterface::class),
m::mock(Grammar::class),
m::mock(Processor::class)
));
$model = m::mock(Model::class);
$model->makePartial();
$scope = m::mock(SoftDeletingScope::class.'[remove]');
$scope->extend($builder);
$callback = $builder->getMacro('withoutTrashed');
$givenBuilder = m::mock(EloquentBuilder::class);
$givenBuilder->shouldReceive('getQuery')->andReturn($query = m::mock(stdClass::class));
$givenBuilder->shouldReceive('getModel')->andReturn($model);
$givenBuilder->shouldReceive('withoutGlobalScope')->with($scope)->andReturn($givenBuilder);
$model->shouldReceive('getQualifiedDeletedAtColumn')->andReturn('table.deleted_at');
$givenBuilder->shouldReceive('whereNull')->once()->with('table.deleted_at');
$result = $callback($givenBuilder);
$this->assertEquals($givenBuilder, $result);
}
}
|