Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,20 +714,6 @@ public static function findOrNew($id, $columns = array('*'))
return new static;
}

/**
* Find a model by its primary key or throw an exception.
*
* @param mixed $id
* @param array $columns
* @return \Illuminate\Support\Collection|static
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public static function findOrFail($id, $columns = array('*'))
{
return static::query()->findOrFail($id, $columns);
}

/**
* Reload a fresh model instance from the database.
*
Expand Down
33 changes: 33 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,39 @@ public function testBasicModelCollectionRetrieval()
}


public function testFindOrFail()
{
EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
EloquentTestUser::create(['id' => 2, 'email' => 'abigailotwell@gmail.com']);

$single = EloquentTestUser::findOrFail(1);
$multiple = EloquentTestUser::findOrFail([1, 2]);

$this->assertInstanceOf('EloquentTestUser', $single);
$this->assertEquals('taylorotwell@gmail.com', $single->email);
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $multiple);
$this->assertInstanceOf('EloquentTestUser', $multiple[0]);
$this->assertInstanceOf('EloquentTestUser', $multiple[1]);
}

/**
* @expectedException Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function testFindOrFailWithSingleIdThrowsModelNotFoundException()
{
EloquentTestUser::findOrFail(1);
}

/**
* @expectedException Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function testFindOrFailWithMultipleIdsThrowsModelNotFoundException()
{
EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
EloquentTestUser::findOrFail([1, 2]);
}


public function testHasOnSelfReferencingBelongsToManyRelationship()
{
$user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
Expand Down
18 changes: 0 additions & 18 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,6 @@ public function testFindMethodUseWritePdo()
}


/**
* @expectedException Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function testFindOrFailMethodThrowsModelNotFoundException()
{
$result = EloquentModelFindNotFoundStub::findOrFail(1);
}


public function testFindMethodWithArrayCallsQueryBuilderCorrectly()
{
$result = EloquentModelFindManyStub::find(array(1, 2));
Expand Down Expand Up @@ -1263,15 +1254,6 @@ public function newQuery()
}
}

class EloquentModelFindNotFoundStub extends Illuminate\Database\Eloquent\Model {
public static function query()
{
$mock = m::mock('Illuminate\Database\Eloquent\Builder');
$mock->shouldReceive('findOrFail')->once()->with(1, array('*'))->andThrow(new ModelNotFoundException);
return $mock;
}
}

class EloquentModelDestroyStub extends Illuminate\Database\Eloquent\Model {
public function newQuery()
{
Expand Down