From d4f251b61810b85a29d2d273d40e75c0d1353b49 Mon Sep 17 00:00:00 2001 From: Sunel Tr Date: Mon, 3 Sep 2018 22:47:57 +0530 Subject: [PATCH] Test Case for issue #10 --- tests/Feature/Issue10Test.php | 89 +++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tests/Feature/Issue10Test.php diff --git a/tests/Feature/Issue10Test.php b/tests/Feature/Issue10Test.php new file mode 100644 index 0000000..b7ca802 --- /dev/null +++ b/tests/Feature/Issue10Test.php @@ -0,0 +1,89 @@ +belongsToMany(Designs::class, 'car_design', 'car_id', 'design_id')->withTimestamps(); + } +} + +class Designs extends \Illuminate\Database\Eloquent\Model +{ + /** + * The table associated with the model. + * + * @var string + */ + protected $table = "design"; + + /** + * The attributes that are mass assignable. + * + * @var array + */ + protected $fillable = ['name']; + + public function cars() + { + return $this->belongsToMany(Tesla::class, 'car_design', 'design_id', 'car_id')->withTimestamps(); + } +} + +class Issue10Test extends TestCase +{ + /** @test */ + public function entity_can_have_relationship_attached() + { + $car = $this->product(); + + + $cars = Tesla::select(['attr.*'])->find($car->getKey()); + $designs = $cars->designs; + + $this->assertEquals($designs->count(), 2); + $this->assertEquals($designs->first()->name, 'Model3'); + } + + /** @test */ + public function entity_can_be_loaed_by_relationship() + { + $car = $this->product(); + + $designs = Designs::findOrFail(1); + $cars = $designs->cars()->select(['attr.*'])->get(); + + $this->assertEquals($cars->count(), 1); + $this->assertEquals($cars->first()->sku, 'PDO1HJK92'); + } + + private function product() + { + $car = Tesla::create([ + 'name' => 'Flamethrower', + 'sku' => 'PDO1HJK92', + 'age' => rand(50, 100), + 'search' => 1 + ]); + + $car->designs()->saveMany([ + new Designs(['name' => 'Model3']), + new Designs(['name' => 'Model4']), + ]); + + return $car; + } +}