I’m trying to write a feature test in RESTful API that I have been created in laravel 8. I have ChatController that gets user chats by getUserChats method: that called by this route: this is my feature test on this route: the test runs successfully by this green output OK (1 test, 3 assertions) but I do…
Tag: phpunit
a PHPpunit test fail randomly
I have a Quiz class. This class load 10 questions from a database depending on the level and the type of the quiz Object: level 0 load the ten first, level 1 load the next ten and so on. So in my test i create in a test database 30 questions. Then i create quiz object with different level and
Laravel how to mock SoapClient response for custom validation rule
Having a custom validation rule that uses the SoapClient I now need to mock it in tests. Used to use laminas-soap package but it doesn’t support PHP8. With laminas-soap it was possible to do But that doesn’t work with SoapClient::class. Then tried from Phpunit, mocking SoapClient is problematic (m…
laravel feature tests with locales
I am new to Laravel and I am having an issue to write a test function that asserts that the HTTP response of my welcome page is 200 knowing that I have EN and FR locales defined, meaning that the test should test both localhost:8000/en and localhost:8000/fr. This is my exampletest.php: I don’t like writ…
Mock class with fluent interface in Phpunit
I am mocking this sample class with fluent methods that doesn’t have return type declaration: Using PhpUnit: By default, this will not work on fluent methods without return type declaration so I need to add the ff.: This works but cumbersome to write if there are a lot fluent methods. My question is, is…
What should I test in an application layer service
I wrote the following application service for a project I’m working on, but struggled to write a good unit test for it. As you can see, the service itself doesn’t perform any meaningful logic other than orchestrating the domain layer (as it should if I understand correctly, according to the blue b…
How can I test laravel job’s second try logic which is using attempts function
I have a queued job class which backs off exponentially thanks to the backoff() method. I want to test that backoff() method works right. It should create 2, 4, 8, 16 and so on after each retry. Since attempts() function belongs to the InteractsWithQueue trait and reads the attempt count from the deeper Redis…
PHPUnit always output “No tests executed!” on MacOS Big Sur
When trying to run any PHPUnit tests, I always get a No tests executed! message on my MacOS machine. A simple way to reproduce, on this specific machine, is to install a fresh instance of Laravel and running the default tests : The expected output would be OK (2 tests, 2 assertions). As far as I know, this is…
Symfony 3.4 Functional tests
I have been stuck for some time on an error to do my functional tests with symfony 3.4. My app run a custom GuardAuthenticator for authenticate my users within CAS authentication. This is just for explain the context. In my tests, I don’t want to use it, I want to use a specific authentication system. I…
Error when running multiple PHPUnit Tests in Laravel 8
Unit testing in a Laravel 8 project is throwing an error when more than one test is called. The issue is causing my tests to fail when I run more than one at once. When running a batch of tests the …