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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
<?php
namespace Illuminate\Tests\Integration\Database\EloquentRelationshipsTest;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Orchestra\Testbench\TestCase;
/**
* @group integration
*/
class EloquentRelationshipsTest extends TestCase
{
public function testStandardRelationships()
{
$post = new Post;
$this->assertInstanceOf(HasOne::class, $post->attachment());
$this->assertInstanceOf(BelongsTo::class, $post->author());
$this->assertInstanceOf(HasMany::class, $post->comments());
$this->assertInstanceOf(MorphOne::class, $post->owner());
$this->assertInstanceOf(MorphMany::class, $post->likes());
$this->assertInstanceOf(BelongsToMany::class, $post->viewers());
$this->assertInstanceOf(HasManyThrough::class, $post->lovers());
$this->assertInstanceOf(HasOneThrough::class, $post->contract());
$this->assertInstanceOf(MorphToMany::class, $post->tags());
$this->assertInstanceOf(MorphTo::class, $post->postable());
}
public function testOverriddenRelationships()
{
$post = new CustomPost;
$this->assertInstanceOf(CustomHasOne::class, $post->attachment());
$this->assertInstanceOf(CustomBelongsTo::class, $post->author());
$this->assertInstanceOf(CustomHasMany::class, $post->comments());
$this->assertInstanceOf(CustomMorphOne::class, $post->owner());
$this->assertInstanceOf(CustomMorphMany::class, $post->likes());
$this->assertInstanceOf(CustomBelongsToMany::class, $post->viewers());
$this->assertInstanceOf(CustomHasManyThrough::class, $post->lovers());
$this->assertInstanceOf(CustomHasOneThrough::class, $post->contract());
$this->assertInstanceOf(CustomMorphToMany::class, $post->tags());
$this->assertInstanceOf(CustomMorphTo::class, $post->postable());
}
}
class FakeRelationship extends Model
{
//
}
class Post extends Model
{
public function attachment()
{
return $this->hasOne(FakeRelationship::class);
}
public function author()
{
return $this->belongsTo(FakeRelationship::class);
}
public function comments()
{
return $this->hasMany(FakeRelationship::class);
}
public function likes()
{
return $this->morphMany(FakeRelationship::class, 'actionable');
}
public function owner()
{
return $this->morphOne(FakeRelationship::class, 'property');
}
public function viewers()
{
return $this->belongsToMany(FakeRelationship::class);
}
public function lovers()
{
return $this->hasManyThrough(FakeRelationship::class, FakeRelationship::class);
}
public function contract()
{
return $this->hasOneThrough(FakeRelationship::class, FakeRelationship::class);
}
public function tags()
{
return $this->morphToMany(FakeRelationship::class, 'taggable');
}
public function postable()
{
return $this->morphTo();
}
}
class CustomPost extends Post
{
protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation)
{
return new CustomBelongsTo($query, $child, $foreignKey, $ownerKey, $relation);
}
protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey)
{
return new CustomHasMany($query, $parent, $foreignKey, $localKey);
}
protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey)
{
return new CustomHasOne($query, $parent, $foreignKey, $localKey);
}
protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey)
{
return new CustomMorphOne($query, $parent, $type, $id, $localKey);
}
protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey)
{
return new CustomMorphMany($query, $parent, $type, $id, $localKey);
}
protected function newBelongsToMany(Builder $query, Model $parent, $table, $foreignPivotKey, $relatedPivotKey,
$parentKey, $relatedKey, $relationName = null
) {
return new CustomBelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName);
}
protected function newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey,
$secondKey, $localKey, $secondLocalKey
) {
return new CustomHasManyThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
}
protected function newHasOneThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey,
$secondKey, $localKey, $secondLocalKey
) {
return new CustomHasOneThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
}
protected function newMorphToMany(Builder $query, Model $parent, $name, $table, $foreignPivotKey,
$relatedPivotKey, $parentKey, $relatedKey, $relationName = null, $inverse = false)
{
return new CustomMorphToMany($query, $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey,
$relationName, $inverse);
}
protected function newMorphTo(Builder $query, Model $parent, $foreignKey, $ownerKey, $type, $relation)
{
return new CustomMorphTo($query, $parent, $foreignKey, $ownerKey, $type, $relation);
}
}
class CustomHasOne extends HasOne
{
//
}
class CustomBelongsTo extends BelongsTo
{
//
}
class CustomHasMany extends HasMany
{
//
}
class CustomMorphOne extends MorphOne
{
//
}
class CustomMorphMany extends MorphMany
{
//
}
class CustomBelongsToMany extends BelongsToMany
{
//
}
class CustomHasManyThrough extends HasManyThrough
{
//
}
class CustomHasOneThrough extends HasOneThrough
{
//
}
class CustomMorphToMany extends MorphToMany
{
//
}
class CustomMorphTo extends MorphTo
{
//
}
|