/** * Check models have proper relations with each other. */ public function testModelRelations() { /** @var Site $site */ /** @noinspection PhpUndefinedMethodInspection */ $this->assertNotNull($site = Site::firstOrFail()); $this->assertNotEmpty($site->posts); /** @var Post $post */ $post = null; foreach ($site->posts as $curPost) { /** @var Post $curPost */ $this->assertNotNull($curPost->site); if ($curPost->site_id === $site->id) { $post = $curPost; break; } } $this->assertNotNull($post); $this->assertNotNull($post->site); $this->assertEquals($site->id, $post->site_id); /** @var Author $author */ $this->assertNotNull($author = $post->author); $this->assertNotEmpty($author->posts); foreach ($author->posts as $curPost) { /** @var Post $curPost */ $this->assertNotNull($curPost->author); } $this->assertNotEmpty($post->comments); foreach ($post->comments as $curComment) { /** @var Comment $curComment */ $this->assertNotNull($curComment); $this->assertEquals($post->id, $curComment->post_id); } }
/** * Seeds the table. * * @return void */ public function run() { $post = new Post(); $post->title = 'JSON API paints my bikeshed!'; $post->body = 'If you\'ve ever argued with your team about the way your JSON responses should be ' . 'formatted, JSON API is your anti-bikeshedding weapon.'; /** @var Site $site */ $site = Site::firstOrFail(); /** @var Author $author */ $author = Author::firstOrFail(); $post->site_id = $site->id; $post->author_id = $author->id; $post->save(); }