Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add find or fail methods #108

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"illuminate/console": "^5.2"
},
"require-dev": {
"mockery/mockery": "dev-master@dev"
"mockery/mockery": "dev-master@dev",
"phpunit/phpunit": "6.*"
},
"autoload": {
"psr-4": {
Expand Down
14 changes: 14 additions & 0 deletions src/Bosnadev/Repositories/Contracts/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,18 @@ public function findAllBy($field, $value, $columns = array('*'));
*/
public function findWhere($where, $columns = array('*'));

/**
* @param $id
* @param array $columns
* @return mixed
*/
public function findOrFail($id, $columns = array('*'));

/**
* @param $attribute
* @param $value
* @param array $columns
* @return mixed
*/
public function findByOrFail($attribute, $value, $columns = array('*'));
}
29 changes: 29 additions & 0 deletions src/Bosnadev/Repositories/Eloquent/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,35 @@ public function findWhere($where, $columns = ['*'], $or = false)
return $model->get($columns);
}

/**
* @param $id
* @param array $columns
* @return mixed
*/
public function findOrFail($id, $columns = array('*'))
{
$model = $this->find($id, $columns);
if (!$model) {
throw new \Illuminate\Database\Eloquent\ModelNotFoundException(sprintf('Nothing found for %d', $id));
}
return $model;
}

/**
* @param $attribute
* @param $value
* @param array $columns
* @return mixed
*/
public function findByOrFail($attribute, $value, $columns = array('*'))
{
$model = $this->findBy($attribute, $value, $columns);
if (!$model) {
throw new \Illuminate\Database\Eloquent\ModelNotFoundException(sprintf('Nothing found for %s==%s', $attribute, $value));
}
return $model;
}

/**
* @return \Illuminate\Database\Eloquent\Builder
* @throws RepositoryException
Expand Down
124 changes: 124 additions & 0 deletions tests/FindOrFailTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace Bosnadev\Tests\Repositories;

use Bosnadev\Repositories\Contracts\RepositoryInterface;
use Bosnadev\Repositories\Eloquent\Repository;
use Illuminate\Container\Container;
use Mockery as m;

/**
* Class FindOrFailTest
* @todo test columns
* @package Bosnadev\Tests\Repositories
*/
class FindOrFailTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Mockery
*/
protected $modelMock;

/**
* @var Container
*/
protected $containerMock;

public function setUp()
{
}

public function tearDown()
{
m::close();
}

public function testFindOrFail()
{
$items = [['id' => 10, 'data' => 'abc']];
$this->reinitMocks(collect($items));

/** @var RepositoryInterface $sut */
$sut = $this->getMockForAbstractClass(
Repository::class,
[$this->containerMock, collect($items)]
);

$result = $sut->findOrFail(10);

$this->assertEquals($result->count(), count($items));
$this->assertEquals($result->all(), $items);
}

protected function reinitMocks($collection = null)
{
$this->modelMock = m::mock('Illuminate\Database\Eloquent\Model')
->shouldReceive([
'find' => $collection,
'first' => $collection,
])
->getMock();

$this->modelMock = $this->modelMock
->shouldReceive('where')
->andReturn($this->modelMock)
->getMock();

$this->containerMock = m::mock(Container::class)
->expects('make')
->andReturn($this->modelMock)
->getMock();
}

/**
* @expectedException \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function testFindOrFailNothingFound()
{
$this->reinitMocks();

/** @var RepositoryInterface $sut */
$sut = $this->getMockForAbstractClass(
Repository::class,
[$this->containerMock, collect([])]
);

$sut->findOrFail(10);
}

public function testFindByOrFail()
{
$items = [['id' => 10, 'hash' => 'abc']];
$this->reinitMocks(collect($items));

/** @var RepositoryInterface $sut */
$sut = $this->getMockForAbstractClass(
Repository::class,
[$this->containerMock, collect($items)]
);

$result = $sut->findByOrFail('id', 10);
$this->assertEquals($result->count(), count($items));
$this->assertEquals($result->all(), $items);

$result = $sut->findByOrFail('hash', 'abc');
$this->assertEquals($result->count(), count($items));
$this->assertEquals($result->all(), $items);
}

/**
* @expectedException \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function testFindByOrFailNothingFound()
{
$this->reinitMocks();

/** @var RepositoryInterface $sut */
$sut = $this->getMockForAbstractClass(
Repository::class,
[$this->containerMock, collect([])]
);

$sut->findByOrFail('id', 10);
}
}
2 changes: 1 addition & 1 deletion tests/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Bosnadev\Tests\Repositories;

use \Mockery as m;
use \PHPUnit_Framework_TestCase as TestCase;
use PHPUnit\Framework\TestCase;

class RepositoryTest extends TestCase {

Expand Down