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 ...
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 ...
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
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
...
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 ...
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 ...
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/...
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:...
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 ...
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 ...
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
└─ {} ...
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
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
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
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);
...
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 {
...
0
votes
1
answer
39
views
How to assert variadic function has exactly n parameters with PHPUnit
PHPUnit version : 11.5.6
Given a class Foo with bar method with variadic params:
class Foo
{
public function bar(int ...$params): void {}
}
I mock a Foo instance in a test and want to assert bar ...
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:
......
1
vote
1
answer
140
views
How do you test a Bus::chain()->catch() in laravel with php unit
I'm writing tests with phpunit to test some chained jobs are dispatched.
I know how to use Bus::assertChained() to verify that the expected jobs are chained.
What I'm struggling with is testing the ...
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 ...
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 ...
-1
votes
1
answer
44
views
ErrorException: Undefined variable $original when running a test
Problem
While running tests using PHPUnit and Orchestra Testbench, I encountered the following error:
ErrorException: Undefined variable $original at
/vendor/laravel/framework/src/Illuminate/Database/...
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 ...
0
votes
1
answer
29
views
commandline phpunit always giving file * not found
I installed phpunit on my laptop and all works fine, but on installation on my PC, when I type phpunit . or phpunit xyzTest.php I get:
PHPUnit 12.0.8` by Sebastian Bergmann and contributors.
Test ...
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
vote
1
answer
67
views
Asserting a method call on a Facade not the underlying proxied class
I'm trying to write tests to assert that methods called in my AppServiceProvider boot method are present & enforced logic.
So far I'm using mocks & reflection, however I am having an issue ...
0
votes
0
answers
45
views
Specific unit testing requirement
I am attempting to make a custom upgrade of a project which is based on an old version of Laravel.
Part of this upgrade will be updating it to the current version of Laravel.
This project has no unit ...
1
vote
2
answers
63
views
How to initialize member variable for phpunit test for them to be defined during dataProvider?
I know I can use dataProvider like so:
use PHPUnit\Framework\TestCase;
class DataProviderSetupTest extends TestCase {
/**
* @dataProvider basicDataProvider
*/
public function ...
0
votes
0
answers
26
views
How to resolve Eclipse PHPUnit 7478 port error? [duplicate]
I TRIED the answer suggested in the question this is a "duplicate" of (I didn't get the printer error). The solution there (to downgrade) did NOT work for ME. I mentioned trying it.
I'm ...
0
votes
1
answer
83
views
Laravel HTTP Tests Routing Issue
I'm currently playing around with Laravel HTTP Feature Tests and I noticed when executing this code below:
Test.php
$this->actingUser($user)
->withHeaders([..])
->post(uri: '/api/foo/bar', ...
0
votes
1
answer
33
views
PHPunit risky test with Unintentionally Covered Code in wordpress callbacks
I am writing tests that execute some of WordPress hooks / actions
My hook example
add_filter("asdasd", static function() {
echo 'asdf';
});
It's complaining:
This test executed code ...
0
votes
1
answer
71
views
GitHub Actions Can't Create Files For Mocking in PHPUnit
I've been working on my tests for an hour and it works on my machine but when I try to put it in github actions, it stops and it can't create any files for mocking.
I've no idea how to allow my action ...
1
vote
0
answers
71
views
Custom phpunit TestCase does not recognize .env.testing in my Laravel project
Context
I have a Laravel 9 (9.52.16) project (also tested on newer versions) with a custom api authentication flow. To make a request you need to receive an init_token first. This can be acquired by ...
0
votes
0
answers
187
views
Testing in Parallel fails after updating to Laravel 11 (Call to undefined method class@anonymous::createApplication())
After updating from Laravel 10 to 11, the --parallel tag does not work when testing. Testing normally still works.
# php artisan test --parallel
ParaTest v7.7.0 upon PHPUnit 11.5.2 by Sebastian ...
0
votes
2
answers
81
views
How to group / reuse / mix custom assertions classes for phpunit testcases?
I am aware that one can add custom assertion by extending the default TestCase, e.g. I added assertions to check that an array may only contain one specific value:
<?php
namespace Kopernikus\...
0
votes
1
answer
156
views
Symfony: How to start symfony server within test env?
Question: How to start symfony server within test env?
The command takes no --env parameter and it does not recognize the option configured in .env.local. Prefixing the command with environment ...
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 ...
-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 ...
0
votes
2
answers
166
views
How to Mock Objects in PHP to Work Correctly with Null Coalescing Operator (??)?
I'm trying to write a PHPUnit test using Mockery, and I keep running into issues when using the null coalescing operator (??) with mocked properties. Specifically, ?? seems to always return null, ...
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 ...
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:...
0
votes
1
answer
60
views
How to mock methods called inside a controller method while testing laravel controller methods
The route defined in web.php is
Route::get('about-us', [PageController::class, 'renderAboutUsPage'])->name('pages.about-us');
In my controller, I have methods
class PageController extends ...
0
votes
1
answer
98
views
Laravel 11: Routes Work in Browser and Postman but Not Found in PHPUnit Tests
I am developing a modular project in Laravel 11, where each module defines its routes inside itself. The routes are working perfectly in the browser and Postman, but when testing the same routes using ...
0
votes
1
answer
84
views
Artisan Output not working in a test when calling Model methods
I am trying to add a new Command to a set of existing Commands around an existing Model.
The other tests for the other Commands around this all work just fine as expected.
However, this new test + ...
1
vote
1
answer
110
views
Can you specify exactly which line of code a phpunit test should cover?
Considering that there are 2 cases which would have the same result, is there an annotation or anything I can add to make sure that my test covers a specific line in a function?
Given that code, I ...
0
votes
0
answers
71
views
Can we use php-code-coverage without phpunit?
We are working on a legacy php solution developed in Symfony. The solution consist of around 1000 actions. We have now introduced a test automation team to automate the testing using Selenium. But we ...
1
vote
0
answers
29
views
Using Wordpress phpUnit and wp_mock in vscode
I am new to php and experimenting with unit_testing and wp_mock. I have an issue which I am unable to resolve. Please could somebody point me in the right direction.
I have a basic class Plugin, ...