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
|
<?php
namespace Illuminate\Tests\Database;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Mockery as m;
use PHPUnit\Framework\TestCase;
class DatabaseEloquentBelongsToManyWithCastedAttributesTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
public function testModelsAreProperlyMatchedToParents()
{
$relation = $this->getRelation();
$model1 = m::mock(Model::class);
$model1->shouldReceive('getAttribute')->with('parent_key')->andReturn(1);
$model1->shouldReceive('getAttribute')->with('foo')->passthru();
$model1->shouldReceive('hasGetMutator')->andReturn(false);
$model1->shouldReceive('hasAttributeMutator')->andReturn(false);
$model1->shouldReceive('getCasts')->andReturn([]);
$model1->shouldReceive('getRelationValue', 'relationLoaded', 'relationResolver', 'setRelation', 'isRelation')->passthru();
$model2 = m::mock(Model::class);
$model2->shouldReceive('getAttribute')->with('parent_key')->andReturn(2);
$model2->shouldReceive('getAttribute')->with('foo')->passthru();
$model2->shouldReceive('hasGetMutator')->andReturn(false);
$model2->shouldReceive('hasAttributeMutator')->andReturn(false);
$model2->shouldReceive('getCasts')->andReturn([]);
$model2->shouldReceive('getRelationValue', 'relationLoaded', 'relationResolver', 'setRelation', 'isRelation')->passthru();
$result1 = (object) [
'pivot' => (object) [
'foreign_key' => new class
{
public function __toString()
{
return '1';
}
},
],
];
$models = $relation->match([$model1, $model2], Collection::wrap($result1), 'foo');
$this->assertNull($models[1]->foo);
$this->assertSame(1, $models[0]->foo->count());
$this->assertContains($result1, $models[0]->foo);
}
protected function getRelation()
{
$builder = m::mock(Builder::class);
$related = m::mock(Model::class);
$related->shouldReceive('newCollection')->passthru();
$builder->shouldReceive('getModel')->andReturn($related);
$related->shouldReceive('qualifyColumn');
$builder->shouldReceive('join', 'where');
return new BelongsToMany(
$builder,
new EloquentBelongsToManyModelStub,
'relation',
'foreign_key',
'id',
'parent_key',
'related_key'
);
}
}
class EloquentBelongsToManyModelStub extends Model
{
public $foreign_key = 'foreign.value';
}
|