Doubt using Repository in Laravel

Asked

Viewed 57 times

2

Personal talk!

Following the idea of Boilerplate http://laravel-boilerplate.com I have a Repository like this:

class EmpresaRepository extends BaseRepository 
{

    public function __construct(Empresa $model)
    {
        $this->model = $model;
    }
}

and Serviceprovider in this way:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Models\Empresa;
use App\Repositories\Backend\EmpresaRepository;

class RepositoryServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind('App\Repositories\Contracts\EmpresaRepositoryContract', function ($app) {
            return new EmpresaRepository(new Empresa());
        });
    }

}

I want to run the following test:

public function empresa_repository_pode_recuperar_empresas()
    {
        $repo = resolve('App\Repositories\Backend\EmpresaRepository');
        $empresas = factory(Empresa::class, 5)->create();
        $query_empresas = $repo->all();

        $this->assertEquals(count($empresas), count($query_empresas));
    }

The problem that occurs is: whenever I instate a Restpository, it creates a new instance of Enterprise, and Count($query_empresas) always returns one more company than those already created with Factory.

Doubts: This approach of Repository always receiving an entity is even correct?

How can I pass this test?

  • Young man, your Preview service has been registered in config/app.php ?

1 answer

-2

Look how far I know,

  new Empresa()

does not save anything in the bank, only instantiates an object in memory. Then your test results should be correct.

But if this test is performed after some other test you have inserted, it may be picking residue from the previous test.

Maybe you’re not wearing the trait

Illuminate\Foundation\Testing\RefreshDatabase

that resets the bench at each test.

I hope I’ve helped someone, something says to me :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.