Summer Sale! All accounts are 50% off this week.

madhur2791's avatar

Lumen: Error while mocking Request facade

I am trying to test paginated results by mocking Request facade, below is how i am mocking it.

Request::shouldReceive('has')
                    ->with('limit')
                    ->once()
                    ->andReturn(false)
                    ->mock()
           ->makePartial();

But i get the below error when i run the test.


$ vendor/bin/phpunit tests/integration/repositories/EloquentRepositoryTest.php 
PHPUnit 5.6.1 by Sebastian Bergmann and contributors.

.E                                                                  2 / 2 (100%)

Time: 1.21 seconds, Memory: 8.00MB

There was 1 error:

Error: Call to a member function get() on null

/vendor/illuminate/http/Request.php:612
/vendor/illuminate/http/Request.php:521
/vendor/illuminate/http/Request.php:696
/vendor/illuminate/http/Request.php:664
/vendor/illuminate/http/Request.php:335
/vendor/illuminate/pagination/PaginationServiceProvider.php:41
/vendor/illuminate/pagination/AbstractPaginator.php:353
/vendor/illuminate/database/Eloquent/Builder.php:473
/vendor/illuminate/database/Eloquent/Model.php:3538
/vendor/illuminate/database/Eloquent/Model.php:3552
/app/Repositories/EloquentRepository.php:54

Please let me know if i am doing anything wrong.

Thanks in advance.

0 likes
5 replies
madhur2791's avatar

@ifpingram

I am trying to test a repository, It tries to get the per page for pagination limit directly from the request also Laravel's paginations also gets the page number in the same way. I don't know a way in which I can pass request parameters when only testing a repository. Can you please help me if there is any way out.

madhur2791's avatar

@ifpingram Please find the complete implementation below

Test class

class UserRepositoryTest {
protected $model = User::class;
protected $repository = UserRepository::class;
function setUp()
    {
        parent::setUp();
    }       

 /** @test */
    function it_will_set_pagination_limit_to_10_if_not_set()
    {   
        Request::shouldReceive('has')
                    ->with('limit')
                    ->once()
                    ->andReturn(false)
                    ->mock()
                    ->makePartial();

        factory($this->model, 50)->create();

        $repo = new $this->repository();

        $this->assertCount(10, $repo->getAll());
    }

}

Repository class

class UserRepository
{   
    protected $paginator;
    protected $model;
    protected $limit = 10;
    protected $maxLimit = 100;

    public function __construct()
    {
        $this->model = User::class;

        if(Request::has('limit')){
            
            $queryLimit = (int) Request::input('limit');    

            $this->limit = $queryLimit <= $this->maxLimit ? $queryLimit : $this->maxLimit;
        }
        
        $this->paginator = $this->model::paginate($this->limit);

        $this->paginator->appends(Request::all());
    }

}
Aridez's avatar

@madhur2791 Any news on that? When unit testing, it would be great to be able to mock the request facade

Please or to participate in this conversation.