From cdb4a25907e309e7c3efcaf516dd4457c70320f2 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Thu, 12 May 2016 22:31:09 +0200 Subject: [PATCH] fix #13527 --- .../Eloquent/Relations/BelongsToMany.php | 2 +- .../Eloquent/Relations/HasManyThrough.php | 2 +- .../DatabaseEloquentBelongsToManyTest.php | 32 +++++++++++++++++++ .../DatabaseEloquentHasManyThroughTest.php | 32 +++++++++++++++++++ 4 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index 5cbab4a51b12..2651ad47bca6 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -182,7 +182,7 @@ public function firstOrFail($columns = ['*']) return $model; } - throw new ModelNotFoundException; + throw (new ModelNotFoundException)->setModel(get_class($this->parent)); } /** diff --git a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php index 52f72ac45c9a..b133e000eb56 100644 --- a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php @@ -244,7 +244,7 @@ public function firstOrFail($columns = ['*']) return $model; } - throw new ModelNotFoundException; + throw (new ModelNotFoundException)->setModel(get_class($this->parent)); } /** diff --git a/tests/Database/DatabaseEloquentBelongsToManyTest.php b/tests/Database/DatabaseEloquentBelongsToManyTest.php index 505160cdd91a..36e57e1803e3 100755 --- a/tests/Database/DatabaseEloquentBelongsToManyTest.php +++ b/tests/Database/DatabaseEloquentBelongsToManyTest.php @@ -335,6 +335,38 @@ public function testCreateMethodCreatesNewModelAndInsertsAttachmentRecord() $this->assertEquals($model, $relation->create(['attributes'], ['joining'])); } + public function testFindOrFailThrowsException() + { + $relation = $this->getMock('Illuminate\Database\Eloquent\Relations\BelongsToMany', ['find'], $this->getRelationArguments()); + $relation->expects($this->once())->method('find')->with('foo')->will($this->returnValue(null)); + + $this->setExpectedException(\Illuminate\Database\Eloquent\ModelNotFoundException::class); + + try { + $relation->findOrFail('foo'); + } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { + $this->assertNotEmpty($e->getModel()); + + throw $e; + } + } + + public function testFirstOrFailThrowsException() + { + $relation = $this->getMock('Illuminate\Database\Eloquent\Relations\BelongsToMany', ['first'], $this->getRelationArguments()); + $relation->expects($this->once())->method('first')->with(['id' => 'foo'])->will($this->returnValue(null)); + + $this->setExpectedException(\Illuminate\Database\Eloquent\ModelNotFoundException::class); + + try { + $relation->firstOrFail(['id' => 'foo']); + } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { + $this->assertNotEmpty($e->getModel()); + + throw $e; + } + } + public function testFindOrNewMethodFindsModel() { $relation = $this->getMock('Illuminate\Database\Eloquent\Relations\BelongsToMany', ['find'], $this->getRelationArguments()); diff --git a/tests/Database/DatabaseEloquentHasManyThroughTest.php b/tests/Database/DatabaseEloquentHasManyThroughTest.php index 9af3b673ac18..198a6bd41538 100644 --- a/tests/Database/DatabaseEloquentHasManyThroughTest.php +++ b/tests/Database/DatabaseEloquentHasManyThroughTest.php @@ -138,6 +138,38 @@ public function testFirstMethod() $this->assertEquals('first', $relation->first()); } + public function testFindOrFailThrowsException() + { + $relation = $this->getMock('Illuminate\Database\Eloquent\Relations\HasManyThrough', ['find'], $this->getRelationArguments()); + $relation->expects($this->once())->method('find')->with('foo')->will($this->returnValue(null)); + + $this->setExpectedException(\Illuminate\Database\Eloquent\ModelNotFoundException::class); + + try { + $relation->findOrFail('foo'); + } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { + $this->assertNotEmpty($e->getModel()); + + throw $e; + } + } + + public function testFirstOrFailThrowsException() + { + $relation = $this->getMock('Illuminate\Database\Eloquent\Relations\HasManyThrough', ['first'], $this->getRelationArguments()); + $relation->expects($this->once())->method('first')->with(['id' => 'foo'])->will($this->returnValue(null)); + + $this->setExpectedException(\Illuminate\Database\Eloquent\ModelNotFoundException::class); + + try { + $relation->firstOrFail(['id' => 'foo']); + } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { + $this->assertNotEmpty($e->getModel()); + + throw $e; + } + } + public function testFindMethod() { $relation = m::mock('Illuminate\Database\Eloquent\Relations\HasManyThrough[first]', $this->getRelationArguments());