PHPUnit is a unit testing framework that is written in php. It is based on the JUnit for Java framework
Questions tagged [phpunit]
27 questions
9
votes
4 answers
Is it a bad practice to separate the unit tests for a class?
My Classes normally have about 5-20 methods maximum, that implies that the class that has the unit tests has about the double of methods (counting dataProviders, setUp, ...)
I have thought to separate the tests in 2-3 classes, because even if I try…

luisandani
- 101
- 1
- 5
8
votes
2 answers
Is it possible to mock and inject traits in PHPUnit?
I need to extend a third party class I cannot modify. The class's dependencies are for the most part injected through the constructor, making them easy to mock. However the class also uses methods from a trait that call global services directly,…

TravisCarden
- 217
- 2
- 10
7
votes
1 answer
Unit testing Eloquent outside of Laravel
How can I unit test my Eloquent models when I'm using the ORM outside of Laravel? What I'm hoping to do it run tests on each model but somehow mock the database connection/query/builder(?) object.
Below is something along the lines of what I'm…

Martyn
- 795
- 8
- 21
6
votes
2 answers
Unit Testing: Method per test versus data provider
I'm having a bit of a philosophical argument with one of my colleagues regarding the "right" way to do unit tests (in this case with PHPUnit). I'm of the opinion that you should write one test method in the unit test per test you want to run.
//…

GordonM
- 6,295
- 1
- 21
- 30
6
votes
2 answers
How well am I writing my tests?
I've been coding as a career for about 2 years now but am just now writing my first "real" tests for a non-trivial application. I've been trying to follow best practices I've picked up from the internet here and there but I would like some feedback…

Matt Foxx Duncan
- 203
- 1
- 5
5
votes
3 answers
Time consuming Unit test for support for 100,000 records: Am I doing it right?
I am working on adding test coverage for an application that previously didn't have any. One additional requirement that emerged is that one of the background processes should be able to process 100,000 rows at a time.
I've written a unit test that…

rsman
- 1,319
- 11
- 21
4
votes
2 answers
Bad Practice - having a SUT use a different execution path when in test mode?
I recently ran across some code in a public repo that I'd thought... just plain wrong. The first thing I noticed was
/**
* Sets a test mode status.
*
* @param boolean $mode Mode to set
*/
public function testMode(bool $mode = true)
{
…

DFriend
- 149
- 2
4
votes
0 answers
unit testing a mocked class
I'm currently writing unit tests for my PHP code. I've read that unit tests should not interract with external elements such as network and filesystem.
In my code i have a curl wrapper class to simplify curl calls (i just use $response =…

ᴄʀᴏᴢᴇᴛ
- 141
- 3
4
votes
1 answer
Sharing Unit Tests between several language implementations of one spec?
JsonLogic is a data format (built on top of JSON) for storing and sharing rules between front-end and back-end code. It's essential that the same rule returns the same result whether executed by the JavaScript client or the PHP client.
Currently…

Jeremy Wadhams
- 201
- 1
- 7
3
votes
1 answer
Testing Queries Themselves with Test data is a Unit test or an Integration test?
In a php project that I maintain I have a database structure without migrations, hence no way to reproduce it or make on the fly a test database.
And the queries used to be performed on them are rather complex ones. The pattern in code used with…

Dimitrios Desyllas
- 451
- 2
- 4
- 16
2
votes
1 answer
Is bad idea to utilize helper functions on integration tests?
In my job I have a small disagreement on whether we should utilize helper functions for making datasets especially in laravel framework. A sample for the test is:
namespace Tests\MyAppTests;
use PHPUnit\Framework\TestCase;
use Carbon\Carbon;
class…

Dimitrios Desyllas
- 451
- 2
- 4
- 16
2
votes
4 answers
How to write a unit test for a repository method that returns some data, where data can change over time?
I have some code that reads five numbers from a database:
class Repository extends DatabaseRepository
{
function getCoefficients(string $model)
{
return $this->getDatabaseLink()->query("
select a, b, c, d, e from…

Dennis
- 8,157
- 5
- 36
- 68
2
votes
2 answers
Initialising variable within setUp() method or actual test method when changes are necessary?
I'm starting to wrap my head around PHPunit.
My questions is the following.
Whenever I use variables that do not change within my range of test methods I can initialize them within my setUp() method. Otherwise, if they do change, I should rather…

Magiranu
- 33
- 1
- 3
2
votes
2 answers
Testing an external API that's in beta state
Our company is using an external API that is actually in beta state.
This means it's not stable at all yet, and it changes its requests/responses every week or so.
I'd like to write test to ensure my code is working. Unit tests are already done for…

Steve Chamaillard
- 1,691
- 1
- 11
- 20
2
votes
2 answers
How unit test service method that use repository method
For service methods that call repository methods to interact with database how could I unit test these service methods?
For example,
public function updateSlideshow($data){
// do some logic and method calls (I can test those methods,
// but…

mohsenJsh
- 1,237
- 1
- 10
- 15