Skip to content

Commit

Permalink
Merge 42f29f9 into 3c29b9d
Browse files Browse the repository at this point in the history
  • Loading branch information
wimski committed Feb 7, 2023
2 parents 3c29b9d + 42f29f9 commit 9083bb2
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ This changelog is initialized in release 1.0.0

## [Unreleased]

## [v2.6.0] - 2023-02-07

### Added
* Builder method to repository

## [v2.5.0] - 2022-08-18

### Added
Expand Down Expand Up @@ -52,7 +57,8 @@ This changelog is initialized in release 1.0.0
* Columns argument to ModelRepositoryInterface::all to be compatible with the underlying Eloquent model
* FQN options for contract and repository to ModelRepositoryMakeCommand

[Unreleased]: https://github.com/wimski/laravel-model-repositories/compare/v2.5.0...master
[Unreleased]: https://github.com/wimski/laravel-model-repositories/compare/v2.6.0...master
[v2.6.0]: https://github.com/wimski/laravel-model-repositories/compare/v2.5.0...v2.6.0
[v2.5.0]: https://github.com/wimski/laravel-model-repositories/compare/v2.4.0...v2.5.0
[v2.4.0]: https://github.com/wimski/laravel-model-repositories/compare/v2.3.0...v2.4.0
[v2.3.0]: https://github.com/wimski/laravel-model-repositories/compare/v2.2.0...v2.3.0
Expand Down
6 changes: 6 additions & 0 deletions src/Contracts/Repositories/ModelRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Closure;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Query\Expression;
Expand All @@ -15,6 +16,11 @@
*/
interface ModelRepositoryInterface
{
/**
* @return Builder<T>
*/
public function builder(): Builder;

/**
* @param int|string $key
* @param string ...$column
Expand Down
9 changes: 9 additions & 0 deletions src/Repositories/AbstractModelRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Wimski\ModelRepositories\Repositories;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Wimski\ModelRepositories\Contracts\Repositories\ModelRepositoryInterface;
Expand All @@ -19,6 +20,14 @@ abstract class AbstractModelRepository implements ModelRepositoryInterface
*/
protected $model;

public function builder(): Builder
{
/** @var Builder<T> $builder */
$builder = $this->model->newQuery();

return $builder;
}

public function find($key, string ...$column)
{
/** @var T|null $model */
Expand Down
14 changes: 13 additions & 1 deletion tests/Integration/AbstractIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

namespace Wimski\ModelRepositories\Tests\Integration;

use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Foundation\Application as BaseApplication;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Orchestra\Testbench\Foundation\PackageManifest;
use Orchestra\Testbench\TestCase;
use RuntimeException;
use Wimski\ModelRepositories\Tests\Laravel\App\Providers\ModelRepositoryServiceProvider;
use Wimski\ModelRepositories\Tests\Laravel\Application;

Expand All @@ -18,7 +21,7 @@ protected function setUp(): void
{
parent::setUp();

$this->app['config']->set('model-repositories.namespaces', [
$this->getApplication()->make(Config::class)->set('model-repositories.namespaces', [
[
'models' => 'Wimski\\ModelRepositories\\Tests\\Laravel\\App\\Models',
'contracts' => 'Wimski\\ModelRepositories\\Tests\\Laravel\\App\\Contracts\\Repositories',
Expand All @@ -27,6 +30,15 @@ protected function setUp(): void
]);
}

protected function getApplication(): BaseApplication
{
if (! $this->app) {
throw new RuntimeException('Application should be setup');
}

return $this->app;
}

protected function getPackageProviders($app): array
{
return [
Expand Down
12 changes: 12 additions & 0 deletions tests/Integration/Repositories/ModelRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Wimski\ModelRepositories\Tests\Integration\Repositories;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
Expand Down Expand Up @@ -49,6 +50,17 @@ protected function setUp(): void
$this->repository = new ModelWithRepositoryRepository(new ModelWithRepository());
}

/**
* @test
*/
public function it_returns_an_eloquent_builder(): void
{
$builder = $this->repository->builder();

self::assertInstanceOf(Builder::class, $builder);
self::assertSame(ModelWithRepository::class, get_class($builder->getModel()));
}

/**
* @test
*/
Expand Down

0 comments on commit 9083bb2

Please sign in to comment.