Questions tagged mocking

Explore the latest questions and answers asked by our top developers.

Laravel Mock Request function only and header

I test code with PHPUnit 9.0. I use Laravel framework 8.* and PHP 7.4 I struggle to test a function that uses request() Here is a very short version of the code I have to test: trait SomeTrait { function someFunction() { //1. retrieve only the documents $documents = request()->only(‘documents’); …. //set an array called […]

By Nele
Published
Categorised as laravel, mocking, php, phpunit, unit-testing Tagged , , , ,

Laravel Unit Test: Partial mocking is not working

I am building a Web application using Laravel. I am now writing unit and integration tests for my application. In my tests I need to mock some of the functions of a class. I only want to mock a function of a class and I want the other functions to behave as they are without […]

By Wai Yan Hein
Published
Categorised as laravel, mocking, unit-testing Tagged , ,

Testing an external api in Laravel

I am consuming an external api and I want to test it. This is my code <?php namespace AppCrmClient; use AppUser; use Exception; use IlluminateHttpClientPendingRequest; use IlluminateSupportFacadesHttp; class CrmRestClient implements CrmClient { /** * Updating user * @param User $user * @return bool * @throws Exception */ public function updateUser(User $user): bool { // If […]

By Halil Y ld r m
Published
Categorised as laravel, mocking, testing, unit-testing Tagged , , ,

Laravel – Mock function in model

In my application, I have a service that uses a function related to a model. This function has already been tested (on its Unit Test), and in my Feature test, I just need to "use" its output value. Because this function is "complicated", I would mock its value without warry about what the function does. […]

By Mistre83
Published
Categorised as laravel, mocking, php-pest, phpunit Tagged , , ,

How to test create model service?

I decided to create CreateClassroomService to separte logic in my controller method. class CreateClassroomService extends Service { public function create(string $name, User $user): ?Classroom { $this->checkName($name); $classroom = new Classroom(); $created = $classroom->setName($name) ->associateUser($user) ->save(); return $created ? $classroom : null; } private function checkName(string $name): void { if (empty($name)) { throw new InvalidArgumentException(); } […]

By longmaikel
Published
Categorised as laravel, mocking, model, php, unit-testing Tagged , , , ,

Laravel mock multiple dependency

I have a Controller that has a dependency with BillingService, and BillingService has another dependency on UserService. I need to call the Controller method getPlans and in this call I need to mock two functions: loadPlans that is inside BillingService getUsage that is in UserService This is the full example: class BillingPlanController { public function […]

By Mistre83
Published
Categorised as laravel, mocking, php-pest, phpunit Tagged , , ,

How to mock Laravel Eloquent model with phpunit?

I have an Laravel User model based on the IlluminateDatabaseEloquentModel. It’s used within a service that I want to unit test. That’s why I want to mock the User with certain properties, in this case it’s about the id which should be set to 23. I create my mock via: /** * @param int $id […]

By k0pernikus
Published
Categorised as eloquent, laravel, mocking, phpunit, unit-testing Tagged , , , ,

How to mock class method for whole application when testing?

I have ServiceApi.php – in constructor it have guzzle client with defaults: $this->client = new Client($options); else it have method: public function fetch() { return $this->client->get(‘http://……’)->getBody()->getContents(); } Another class ServiceUser.php – has method which using ServiceApi: public function fetchFromApi() { return (new ServiceApi())->fetch(); } When I run test I want (new ServiceUser())->fetchFromApi() – don’t call […]

By rst630
Published
Categorised as guzzle, laravel, mocking, phpunit Tagged , , ,

Laravel Cache helper "driver" not implemented

I have a test that is calling a Controller, which literally handles a call to some logic service I mocked. The test public function test_it_can_list_all_folders() { $mockedLogicResponse = [ "id" => 1111, "parent_id" => 2222, "name" => "Some Name", "children" => [ [ "id" => 3333, "parent_id" => 5555, "name" => "Ad-hoc" ], [ "id" […]

By jmares
Published
Categorised as laravel, mockery, mocking, php Tagged , , ,

Laravel/codeception testing: how to stubbing time()

Is it possible using Laravel mocking, stubling, (and/or CodeceptionTestUnit and/or CodeceptionStub) to make test for method z (public testZ(){…} ) for following class: <?php class X extends Y { public function z(string $c = ‘c’) : array { return [$c, time()]; } } So, I need time() to return fake value. Uopz & mimus are […]

By A T
Published
Categorised as codeception, laravel, mocking, php, stubbing Tagged , , , ,
1 2 3 4

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq