Skip to content

Commit

Permalink
fix #13527 (#13537)
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid authored and taylorotwell committed May 12, 2016
1 parent eedb619 commit 07177e9
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public function firstOrFail($columns = ['*'])
return $model;
}

throw new ModelNotFoundException;
throw (new ModelNotFoundException)->setModel(get_class($this->parent));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public function firstOrFail($columns = ['*'])
return $model;
}

throw new ModelNotFoundException;
throw (new ModelNotFoundException)->setModel(get_class($this->parent));
}

/**
Expand Down
32 changes: 32 additions & 0 deletions tests/Database/DatabaseEloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
32 changes: 32 additions & 0 deletions tests/Database/DatabaseEloquentHasManyThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 07177e9

Please sign in to comment.