Questions tagged [phpunit]
PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks.
9,995
questions
-1
votes
0
answers
35
views
Data provider laravel phpunit doesn't work: Argument #2 ($code) must be of type int, string given
I'm trying to do testing on a Laravel application using Phpunit and data provider (I want to test for each field being wrong, so I thought it would be awesome to use a Data Provider)
However, I can't ...
0
votes
0
answers
21
views
PHPUnit 10: parseTestMethodAnnotations() is not working as it is removed from PHPUnit/Util/Test
Recently I have upgraded from PHPUnit 9.6 to 10.4 and I am facing some difficulties while upgrading. For ex, I have below code:
use PHPUnit\Util\Test as TestUtil;
$annotations = TestUtil::...
0
votes
0
answers
38
views
Laravel PHPUnit : Can't use Config::get() when setUp() method is present
I have a unit test similar to this in Laravel 9:
namespace Tests\Unit;
use Tests\TestCase;
use App\Models\CompanyContact;
use Config;
class CompanyContactTest extends TestCase
{
private $...
0
votes
1
answer
19
views
How to return different values from mock object depending on parameter
I want to mock the return values of a redis server. Depending on the key I need to get back different fake values.
Since this would be still easy I want as a return value the content of a file on my ...
0
votes
0
answers
22
views
How do I test Laravel Cashier Checkout Redirect in PEST PHP tests?
I have this subscribeToPlan method which will redirect the logged in user to Stripe's hosted checkout page.
public function subscribeToPlan(StripePlan $plan): \Laravel\Cashier\Checkout
{
return ...
0
votes
3
answers
38
views
PHPUnit: Running Tests that Use a Specific Attribute
I'm using the RequiresFunction attribute in a few of my tests. I want to pass arguments to phpunit that will run the tests with that attribute and the name of the required function (if possible).
...
0
votes
0
answers
16
views
Run Codeception Acceptance Test within two Integration Tests coded in simple PHPUnit
Consider the following Integration test file extract based on PHPUnit:
...
/**
* @return void
*/
public function test_first() {
$this->assertTrue(true);
}
/**
* @depends ...
2
votes
1
answer
19
views
phpunit setup in Docker Container
I have following docker-compose.yml file for PHP Docker application. I tried to setup PHPUnit in this docker container. But It is showing following error
Error response from daemon: failed to create ...
1
vote
0
answers
22
views
Call to undefined method Test::component() in Laravel package testing
I have a Laravel package that will provide UI components (in Blade) and I want to write some unit tests for these components. I am using PHPunit 10.4 and orchestra testbench 8.15.
Base testcase:
class ...
1
vote
1
answer
22
views
PHPUnit Mocking methods calls in a mocked method
I'm trying to write a basic unit test to see if a method is called in another method.
I have a class like the following:
class Hello
{
public function doSomething($something)
{
if ($...
-1
votes
1
answer
68
views
Unit tеsting against protеcted properties [duplicate]
Ideas how to write good OOP code, SOLID, incapsulation, loose coupling and so lead to
not using public properties
use as little getters as possible
Say we have code
class Some
{
protected int $x;
...
0
votes
0
answers
15
views
Can PHPUnit code coverage produce HTML diff of two coverages that correspond to two git commits
In the same sense that when I want to review code, I am reviewing only changes, I would also be interested only in the changes of unit coverage. So, what I'd like to see is the difference of the code ...
0
votes
0
answers
46
views
Php unit test gives error in default laravel application
in default laravl application when I run phpunit it always gives me the error:
PHP Fatal error: Uncaught Error: Call to undefined function each() in C:\xampp\php\pear\PHPUnit\Util\Getopt.php:80
Stack ...
-2
votes
4
answers
161
views
How to assign a belongsTo relation in Laravel without saving to database?
I have the following 2 models:
<?php
namespace App\Models;
class Product extends Model
{
//some other property of the Model
public function productType() {
return $this->...
0
votes
1
answer
18
views
Why can't my Drupal PHPUnit test seem to autoload a shared test fixture/utility class?
In my Drupal 10.x project, I have a test that is attempting to mock a class that I created to be used by multiple tests (call it Drupal\Tests\my_module\MyTestObject). The code looks something like ...
0
votes
0
answers
32
views
How to use `setMockClassName` from phpunit with a class-string correctly?
Recently, the maintainer of PHPUnit made a change to the "setMockClassName" method so that it now accepts only class-strings instead of regular strings. I'm currently handling this ...
0
votes
0
answers
33
views
PHPUnitCode Coverage readonly classes
I have a project with Laravel 9, phpunit/php-code-coverage: "^9.2" and phpunit/phpunit: "^9.3.3".
I can run individual tests and everything works correctly, but if I try to ...
0
votes
0
answers
38
views
Issues in executing unit test in Laravel 8
I am using Laravel 8 and PHP 7.4
When I run the below command
php artisan test --filter ActivityTests
I get error:
"Warning: TTY mode is not supported on Windows platform.
PHPUnit requires the &...
0
votes
0
answers
33
views
Laravel Mockery phpunit error: Unresolvable dependency resolving
Fairly new to testing but can't this test with a facade to work correctly. The code itself is working correctly. Just want the test to work also :')
The serviceprovider:
<?php
class ...
0
votes
2
answers
35
views
Test multi storage in laravel
I have a issue while trying to mock multi Storage in laravel test enviromnment.
Here is my my code:
public function sftp ( Sibling $sibling ) {
$file_paths = Storage::build($sibling->config)...
0
votes
0
answers
49
views
Laravel: mocking service class in job
I want to mock a service class for a test, but I can not.
My test
protected function setUp() : void
{
parent::setUp();
Queue::fake();
$this->client = $this->createClient();
}
...
0
votes
0
answers
32
views
Symfony 6.2 - EasyAdmin 4 phpunit test - Mandatory _locale
Within an application that I am building I got a admin with an locale variable (en/nl)
#[Route('/{_locale<%app.supported_locales%>}/admin', name: 'admin')]
public function index(): Response
...
0
votes
0
answers
40
views
Tests not hitting breakpoints in Docker + XDebug + VSCode setup
Already spent good part of the night and a several hours today, going through dozens of articles, videos and whatnot, I haven't been able to make it work.
This is a Laravel 10 application, where I'm ...
0
votes
1
answer
54
views
PHPUnit how to mock or change the value of a class constant
I've got a class that has a constant that is accessed by various methods in the class. It has been a constant for long time (legacy code) and we don't wanna change it to be a regular var so I'm stuck ...
0
votes
1
answer
41
views
PHPUnit: Ignore tests completely based on dataProvider parameters
I want to test (functional) my Symfony controllers with PHPUnit 9.6 and in order to not have to have the same test cases in each test class, I had the idea to make an abstract class that contains the ...
0
votes
1
answer
9
views
phpunit error Argument 1 passed to PHPUnit\Runner\TestSuiteSorter::reorderTestsInSuite must be an instance of PHPUnit\Framework\Test
I'm using
PHPUnit 9.6.10 by Sebastian Bergmann and contributors.
and when i run phpunit why can i see the following error?
Argument 1 passed to PHPUnit\Runner\TestSuiteSorter::reorderTestsInSuite()
...
-1
votes
1
answer
48
views
Wrong request parameters when testing an api route
I have a test that calls a function in a controller:
public function testAuthentication()
{
$user = $this->createUser();
$this->actingAs($user)->withoutMiddleware();
$project = $...
0
votes
0
answers
54
views
phpunit configuration file doesn't load - Uncaught PHPUnit\Util\Xml\Exception
I have a phpunit.xml file in the root of my project. When I run vendor/bin/phpunit web/core/tests/Drupal/KernelTests/Core/DependencyInjection/AutowireTest.php, this file is picked up and the tests run ...
0
votes
0
answers
12
views
PHPUnit 8 exit code 0 on database connection faluire
I'm running into what I think it's a strange behavior for PHPUnit (8.5.26 on PHP 7.4): if the database connection fails, the app throws an exception but the exit code is still 0.
Is this an expected ...
1
vote
1
answer
79
views
php artisan test ignore phpunit.xml
When I run my tests with Phpstorm everthing works as expected and the connection switches to mysql_test. But when I run php artisan test manually the connection wont switch and remain mysql.
phpunit....
0
votes
0
answers
34
views
Is it possible to only run tests that touch changed code in php
I have a testsuite that takes several minutes to complete so it starts to get annoying to run them all before each push (they will still be run in the deployment pipeline as well)
So I was wondering ...
0
votes
1
answer
24
views
AspectMock not working in Codeception framework & yii2 framework
I want to mock the static methods, so I am using the AspectMock library https://github.com/Codeception/AspectMock.
I tried to mock one static method but it doesnt return the expected mocked result.
...
0
votes
1
answer
20
views
In CoreClassReflection.php Class Does Not Exist While Using Infection
I try to use Infection to evaluate my testcase using php, I have 2 file (Displayuser.php, displayuserTest.php)
for the first file Displayuser.php the code like this, this file under func folder
<?...
1
vote
0
answers
31
views
How can I properly test DateTime in my service?
I am having difficulties on how to test my service properly, as it uses DateTime.
From what I have read, everything I need to do is to "isolate" the "collaborator" and a good way ...
-1
votes
1
answer
60
views
How to override $request query parameters in phpunit
I am modifying subject_type query parameter value inside the controller using $request->merge, it is working as expected while sending request using the browser or any API testing client. But the ...
0
votes
1
answer
33
views
Uncaught PHP Exception LogicException: "Unable to add global "ea" as the runtime or the extensions
In browser, easyadmin is working fine. But when I run test case which asserts redirection logic to EasyAdmin's dashboard controller after login, it failed with the following error:
Uncaught PHP ...
0
votes
1
answer
32
views
PHPUnit calling same method twice with willReturnOnConsecutiveCalls results in empty data
Problem: How to test same method (getNextUniqueBunch) with different results?
function exampleQuestion() {
$rows = [];
$rowsAgain = [];
while ($bunch = $this->_dataSourceModel->...
0
votes
0
answers
54
views
PHPUnit server side coverage with curl requests [duplicate]
I have some tests for an old PHP app, and it have inline tests and some curl requests to test APIs. The PHPUnit coverage, using Xdebug, isn't detecting the curl requests.
Is there a way to follow this ...
0
votes
0
answers
86
views
Laravel sail test works but vendor/bin/phpunit doesn't
I've setup a work flow using github actions and when it gets to this command there are 3 failed tests
vendor/bin/phpunit
But during this command on my machine they all pass
sail artisan test
The ...
0
votes
1
answer
71
views
how to remove deprecation on doctrine AbstractFixture::getReference()
In PHPUnit 10,
I have somes fixtures between entities and they share references.
I have the following deprecation and i can't manage to remove it :
Argument $class of Doctrine\Common\DataFixtures\...
0
votes
1
answer
25
views
How to Mock $wpdb->prefix using WP_Mock and Mockery?
🗺️ My Environment
PHP
7.4
WordPress
6.2
WP_Mock
1.0
PHPUnit
9.6.11
Mockery
1.6.6
🖥️ Code: TestCase
use PHPUnit\Framework\TestCase;
use WP_Mock;
use Mockery;
class WPAB_Database_Service_Test ...
0
votes
1
answer
73
views
PHPUnit 10 specify different method arguments on consecutive calls to the same method
I have a method that's called several times with different arguments as part of a larger method.
$query->where("one", $id);
$query->where("two", "LIKE %{$name}%");
$...
0
votes
3
answers
77
views
How to validate a not updatable column with symfony/validator?
I have a column that it's a DateTimeImmutable that save when the user has joined the platform. I'm making the phpunit test and i would like to validate that the colum can't be updated. I would like to ...
0
votes
0
answers
19
views
Compress dataprovider output when tests pass phpunit
I have a testcase for phpunit 10 that uses a dataprovider:
#[DataProviderExternal(\Tests\DataProvider::class, 'invalidEmailAddresses')]
public function test_validates_email_address(string $email): ...
1
vote
0
answers
60
views
how to write phpunit test
we have function testLink. the method $this->authorize() is invoked twice inside. How to write unit test to test business logic of invoking of method $this->authorize()?
public function testLink(...
0
votes
2
answers
85
views
Is there a simple way to invert a test or test group?
Assume you have written a failing test to exploit a bug. You want to push this to upstream and then create a story that is about fixing the code so that the test passes. However, this is prevented by ...
0
votes
0
answers
34
views
PHP PHPUnit Test Case
Write a class 'Skill' that handles any function which starts with ‘has_’ keyword return function ‘exist’ and any other function that starts with any word that should return ‘not exist’ and checking a ...
0
votes
0
answers
34
views
How to properly mock partially class with params in Laravel?
I have a service called GenerateReportService in which I need to mock one method generatePdf. The problem is that I need to check the params which will be received in this method after others methods ...
1
vote
0
answers
241
views
Run PHPUnit rather than Pest
I have a project with a mixture of tests wrtten in Pest and regular PHPUnit Test Classes. There is a lot missing from Pest for package developers and its just simpler to use the PHPunit for areas ...
0
votes
1
answer
39
views
phpunit test - validation for store boolean fields which fills by non-boolean and invalid value
sorry for my bad english :) .
I want to test a boolean field validation error, when user entered a non-boolean value (from browser inspection,for example) .
for example, if in browser inspect, I set '...