10,094 questions
2
votes
1
answer
76
views
Why does PHPUnit require an intermediate variable when mocking/stubbing?
I am doing some unit testing with PHP. I got stuck on an error that the fluent interface of PHPUnit requires an intermediate variable. What is the reason for this behavior?
See MyTests::works vs. ...
1
vote
2
answers
148
views
PHPUnit tests get awfully slow when all tests are run at once [closed]
I have a problem which I find awfully hard to debug:
In my application, I currently have about 1000 tests, so not much. They are split across several directories (tests/someDir, tests/someOtherDir, ...
0
votes
1
answer
92
views
Testing Eloquent model events in Laravel 11
I can't seem to find a definitive answer on how to test the following boot methods using PHPUnit tests in Laravel.
I'm referring to the mthods in public static function boot() on a model.
Survey model ...
2
votes
1
answer
53
views
PhpUnit overwrite server variables from XML
I have following server variable in phpunit.xml:
<php>
<server name="APP_DEBUG" value="false"/>
</php>
Sometimes while developing, I want have APP_DEBUG ...
0
votes
0
answers
129
views
Laravel PHPUnit test: Multiple databases issue
In my Laravel application, I have set up two databases in a single server. All specific migrations and model classes have $connection property set up to use blog instance. The app works fine, no ...
1
vote
0
answers
45
views
Show only failing and expected values from PHPUnit output
I have PHPUnit tests that use data providers as inputs, and by default these are outputted in a diff-like format (when running with php artisan test):
1) Tests\Feature\MyTest::test_feature with data ...
1
vote
0
answers
191
views
How to run tests of the same phpunit testcase class in parallel with ParaTest?
I have a contrived test case showcasing how multiple test cases can slow the entire execution time down, as even though each is quick on its own, they add up if waited for before executing the next.
I ...
508
votes
16
answers
370k
views
How to run single test method with phpunit?
I am struggling to run a single test method named testSaveAndDrop in the file escalation/EscalationGroupTest.php with phpunit. I tried the following combinations:
phpunit EscalationGroupTest ...
0
votes
0
answers
98
views
Laravel testing database table is being accessed by other users
I'm currently building a multi-tenant application using stancl/tenancy (every tenant uses their own database) in my Laravel application.
Whenever I'm writing tests, tests fail a lot due to the error
...
483
votes
14
answers
375k
views
PHPUnit assert that an exception was thrown?
Does anyone know whether there is an assert or something like that which can test whether an exception was thrown in the code being tested?
40
votes
5
answers
26k
views
How to make PHPUnit explain deprecations and warnings?
I'm just learning how to use PHPUnit. I'm running it from the command line, and among the results I get OK, but there are issues! Tests: 5, Assertions: 15, Warnings: 2, Deprecations: 5.
I can't work ...
1
vote
1
answer
61
views
get_template_directory() returning child theme instead of parent theme when running phpunit
I'm trying to set up testing for a Wordpress child theme. I've use the wp-cliwp scaffold theme-tests command to scaffold some tests. Inside of my child theme's functions.php file, I load some ...
44
votes
14
answers
33k
views
Replace PHPUnit method `withConsecutive` (abandoned in PHPUnit 10)
As method withConsecutive will be deleted in PHPUnit 10 (in 9.6 it's deprecated) I need to replace all of occurrences of this method to new code.
Try to find some solutions and didn't find any of ...
113
votes
32
answers
82k
views
PHPUnit - 'No tests executed' when using configuration file
The Problem
To improve my quality of code, I've decided to try to learn how to test my code using Unit Testing instead of my mediocre-at-best testing solutions.
I decided to install PHPUnit using ...
46
votes
14
answers
102k
views
Laravel and PHPUnit: could not find driver (SQL: PRAGMA foreign_keys = ON;)
I have run my Laravel application with PHPUnit.
Everything is fine until at some point, when I run my test again, it turns out with this error.
Illuminate\Database\QueryException: could not find ...
165
votes
10
answers
180k
views
How to execute specific test class using PHPUnit in Laravel
I want to test specific testClass in my project since there are a lot of test class that's failure and I just want to test one Class at a time.
I've created the test Class in following folder \test\...
221
votes
19
answers
166k
views
PHPUnit: assert two arrays are equal, but order of elements not important
What is a good way to assert that two arrays of objects are equal, when the order of the elements in the array is unimportant, or even subject to change?
3
votes
1
answer
147
views
Laravel API Testing index and show data fail at assertJsonStructure of Nested Json back-and- fort
I have API for books that I want to test using phpunit, but the structure of nested json somehow make index and show test function making one of them fail.
If I wrote this code for both index and show ...
1
vote
2
answers
197
views
ArgumentCountError: Too few arguments to function whatever, 0 passed, even with data provider (PHPUnit 12)
I'm trying to use PHPUnit 12 dataProviders to provide data to a function to test but I can't get it working.
Here's my unit test (that lives at tests/TestTest.php):
<?php declare(strict_types=1);
...
236
votes
21
answers
212k
views
How to output in CLI during execution of PHP Unit tests?
When running a PHPUnit test, I would like to be able to dump output so I can debug one or two things.
I have tried the following (similar to the PHPUnit Manual example);
class theTest extends ...
361
votes
11
answers
196k
views
Best practices to test protected methods with PHPUnit
I found the discussion on Do you test private method informative.
I have decided, that in some classes, I want to have protected methods, but test them.
Some of these methods are static and short. ...
0
votes
1
answer
54
views
PHPUnit in VS Code don't show tests grouped in Test Explorer, despite correct configuration
PHPUnit test suites not showing in Test Explorer, despite correct configuration
in VS-Code it looks like this:
TESTING
[Filters fiel]
6/6 (6 from 6 succesful)
└─ {} SL5
└─ PregContentFinder
└─ {} ...
1
vote
1
answer
565
views
Using Laravel's `Log::spy` with channels
I have a FormRequest that logs validation error details:
protected function failedValidation(Validator $validator)
{
Log::channel('logistics')->info('Validation failed. Submitted ...
1
vote
0
answers
154
views
Laravel 12 uses 'local' env while running php unit tests
I'm trying to run tests on my Laravel application.
here is my phpunit config:
<php>
<env name="APP_ENV" value="testing"/>
<env name="...
0
votes
2
answers
63
views
How to allow a certain differences in a text assertion for a test case that has some expected changes in data using phpunit?
I am setting up a golden master test case, yet the result may be off at certain points due to side-effects changing some strings at known places.
Getting rid of those side-effects would be best, yet ...
1
vote
0
answers
65
views
How To Test Importing File Excel With Real or Fake File Contained Data For Table Database In Laravel PHPUnit
I have a bit of trouble figuring out of how to test import book route of my controller in PHPUnit Testing. I'm Using MaatWebsite Package for the excel.
So, I have real excel file in test/Files/...
2
votes
2
answers
61
views
Laravel job sync driver refresh database
I have a piece of code that does the following:
Create a database row (spatie media library)
Create a job that adds something to column A and saves the object (also spatie media library)
Adds ...
2
votes
1
answer
198
views
PHPUnit / Symfony / Fixtures : Database schema not created, fixtures not loaded
this is my first post so don't hesitate to tell me if something is wrongly said.
I'm working on a projet in Symfony 7.*.
I try to implement unit test in my project but I'm facing some issues.
I use ...
34
votes
4
answers
18k
views
What means: "Your XML configuration validates against a deprecated schema. Migrate your XML configuration using "--migrate-configuration"!"
I have included PHPUnit in a test vanilla PHP project and when I start the test with vendor/bin/phpunit I get the following error message:
Your XML configuration validates against a deprecated schema. ...
6
votes
3
answers
14k
views
How many cores are used by Github Actions Runners?
I am running php artisan test --parallel on github actions using a docker container, and the performance is worse than if I run it without --parallel. If I specify the cores with php artisan test --...
0
votes
1
answer
69
views
PHPUnit on Apache - how to capture "console" output?
I'm implementing the server API (for geni.com) in PHP. The server requires OAuth authentication, so I need a browser because this requires user input (to permit access to their account). THIS all ...
1
vote
1
answer
59
views
Why does phpunit's check for the passed arguments fail when I use named arguments?
I have a simple test where I mock a dependency of the code under test:
<?php
use PHPUnit\Framework\TestCase;
class Dependency {
public function someFunction(int $first, int $second): int {
...
34
votes
11
answers
30k
views
Laravel 5.8 .env.testing file is not working
I created a .env.testing file with my credentials, everything the same as .env besides a different table name for the database.
I tried php artisan config:clear which deletes the cached config file ...
2
votes
2
answers
210
views
Laravel Cloud Error: Call to undefined function Illuminate\Foundation\laravel_cloud()
I am running phpUnit tests using Laravel Framework Version 9.52.20. The tests work just fine in my local environment and in my CI/CD pipeline. But when I try to run the same tests in my dev ...
170
votes
8
answers
100k
views
Difference between assertEquals and assertSame in PHPUnit?
PHPUnit contains an assertEquals() method, but it also has an assertSame() one. At first glance it looks like they do the same thing.
What is the difference between the two? Why are they both ...
174
votes
11
answers
124k
views
How can I get PHPUnit MockObjects to return different values based on a parameter?
I've got a PHPUnit mock object that returns 'return value' no matter what its arguments:
// From inside a test...
$mock = $this->getMock('myObject', 'methodToMock');
$mock->expects($this->...
2
votes
1
answer
493
views
PHPUnit ignore deprecation in dependencies
I’m testing my packages for compatibility with PHP 8.4.1. However, some dependencies trigger deprecation warnings. Is there a way to configure PHPUnit to report deprecation warnings only for my ...
162
votes
6
answers
164k
views
phpunit mock method multiple calls with different arguments
Is there any way to define different mock-expects for different input arguments? For example, I have database layer class called DB. This class has method called Query(string $query), that method ...
2
votes
1
answer
214
views
How to configure ClockMock with Phpunit 10+?
ClockMock is a very useful tool for creating time sensitive tests e.g. if a date is in past A should happen, if it is in the future B should happen.
ClockMock is a part of the phunit-bridge, which ...
0
votes
1
answer
41
views
How To Access Specific Notification Data In Laravel Test
I want to test MarkAsRead function via routing, my route was Route::post('notifications/{id}', [NotificationController::class, 'markAsRead']); and it was in API routing of auth:sanctum
api route
Route:...
1
vote
4
answers
313
views
Laravel is always returning local when checking the environment, even when tests are being executed
I've got a Dockerized Laravel app, here's the docker compose file:
version: "3.9"
services:
app:
build:
context: ./
dockerfile: Dockerfile
image: dmc
container_name:...
27
votes
6
answers
25k
views
PHPUnit warning - No filter is configured, code coverage will not be processed
I'm trying to generate coverage html file with PHPUnit. I get a warning: 'No filter is configured, code coverage will not be processed' and coverage file isn't generated.
This is phpunit.xml file
<?...
2
votes
1
answer
63
views
I don't know how to change from at() to once() in phpunit 10 and above
The method function at() has been removed from phpunit 10 later.
I changed from at() to once() but my tests still don't pass
The test fails because you cannot order the condition() calls.
php code:
......
74
votes
3
answers
32k
views
What is .phpunit.result.cache
When I run tests with PhpUnit on a new package I'm creating for Laravel, it generates the file .phpunit.result.cache.
What to do with that? Do I add it to my .gitignore file or not?
I'm using ...
1
vote
0
answers
49
views
Latest Wordpress CLI tool generates tests incompatible with latest PHPUnit
I'm developing a Wordpress plugin and wanted to use a test-driven approach so I installed PHPUnit and the WP CLI tool. I did wp scaffold plugin-tests my-plugin and it generated the scaffolding files ...
1
vote
1
answer
119
views
Assert that a specific method is called when running API endpoint test
Stack versions:
ApiPlatform 3.2
Symfony 6.4
PhpUnit 9.5
PHP 8.0
Writing a test for the DELETE endpoint of the User resource.
When the endpoint is called, the user must be soft deleted and several ...
0
votes
1
answer
121
views
php artisan test on laravel 8 raises ERROR: Command "test" is not defined. Did you mean one of these?
Laravel version: 8
php version: 8.1
When I run php artisan test, I get Command "tests" is not defined.
I added nunomaduro/collision to my dependencies, and I get the error:
NunoMaduro\...
0
votes
1
answer
89
views
How to write test for Trait
I have a Trait called HasDefault
its main purpose is to ensure that the class has a static field $default which is callable and returns the default instance of the class using the Trait
This is what ...
-1
votes
1
answer
168
views
Migrate and seeding database once for all tests
I am trying to test Laravel API by using Laravel PHP unit test, I want to refresh and seeding of my test database once for all tests. what should i need to do in Testcase Laravel class for get this ...
80
votes
4
answers
54k
views
Phpunit tests gives warning no tests found in class
I am trying to learn how to test with phpunit and laravel. When start the test using phpunit command, I am getting a warning :
There was 1 failure:
1) Warning
No tests found in class "PostsTest".
...