Skip to main content

Questions tagged [phpunit]

PHPUnit is a unit testing framework that is written in php. It is based on the JUnit for Java framework

Filter by
Sorted by
Tagged with
2 votes
2 answers
330 views

Unit testing for non-stateless units - how?

Firstly let me say I have never written a unit test in my life. I am trying to get the hang of PHPUnit, and so far it's working pretty well for me so far as "stateless" functions (that ...
user avatar
-6 votes
1 answer
172 views

Why is automatically generating automated tests frowned upon?

First, the most obvious grouse someone has against this I can think of is the intricacies of an actual method. It's not enough to merely ensure no errors are thrown. Functions usually contain ...
I Want Answers's user avatar
3 votes
1 answer
586 views

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 ...
Dimitrios Desyllas's user avatar
4 votes
2 answers
244 views

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 ...
DFriend's user avatar
  • 149
2 votes
1 answer
623 views

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 ...
Dimitrios Desyllas's user avatar
0 votes
1 answer
619 views

Unit tests involving library functions and objects

I am making a project using the Laravel framework. I plan to create unit tests for my project. But I feel I don't really have an idea what to test because I mostly use library or framework functions. ...
lightning_missile's user avatar
1 vote
1 answer
1k views

How to unit test database queries based on current date?

Is there a feasible way of testing SQL queries that contain things like CURRENT_DATE and NOW() in a unit test? For example: public function deletePastEntries(): bool { $sql = "DELETE FROM queue ...
rink.attendant.6's user avatar
2 votes
4 answers
2k views

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()-&...
Dennis's user avatar
  • 8,247
-1 votes
1 answer
104 views

How to approach writing a 'test' for a series of input->output sets, that will later be used to verify correctness of a new system?

I have a system ("an engine", which is a piece of code) that takes in several parameters returns some output. The input parameters are several distinct and constrained sets. I can enumerate all of ...
Dennis's user avatar
  • 8,247
2 votes
2 answers
3k views

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() ...
Magiranu's user avatar
-2 votes
2 answers
979 views

Writing unit test cases are taking time, any advice?

I am new to unit testing. Started working on unit test using PHPUnit. But it seems to be taking too much time. If consider I take 3 hours to write a Class, its taking my 7 hours to write test case for ...
Pratik Joshi's user avatar
6 votes
2 answers
3k views

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 ...
GordonM's user avatar
  • 6,425
4 votes
0 answers
181 views

unit testing a mocked class [duplicate]

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 ...
ᴄʀᴏᴢᴇᴛ's user avatar
2 votes
2 answers
135 views

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 ...
Steve Chamaillard's user avatar
3 votes
2 answers
3k views

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 ...
mohsenJsh's user avatar
  • 1,315
8 votes
2 answers
7k views

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 ...
TravisCarden's user avatar
4 votes
1 answer
152 views

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 ...
Jeremy Wadhams's user avatar
6 votes
2 answers
2k views

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 ...
Matt Foxx Duncan's user avatar
9 votes
4 answers
5k views

Is it a bad practice to separate the unit tests for a class? [closed]

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 ...
luisandani's user avatar
0 votes
1 answer
167 views

Test logic with external data [duplicate]

I have a site programmed in PHP where I make more call to external server that return me big xml of data. For each record of the xml I have to make other external call to get other data. So my ...
Alessandro Minoccheri's user avatar
1 vote
1 answer
2k views

PHPUnit > Best approach to mock container objects? (e.g. $item->id)

I'm using PHPUnit test doubles to mock models, libraries etc. But when I want to return an array perhaps of container objects, how best to do so. Here is what I'm currently doing: /** * This is just ...
Martyn's user avatar
  • 795
7 votes
1 answer
3k views

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. ...
Martyn's user avatar
  • 795
0 votes
1 answer
199 views

Just jumped into database testing in PHPUnit - not really sure what the purpose of it is

I've been reading about database testing in PHPUnit. I'm not exactly sure what it's purpose is. Should I use this to test schema? E.g. if I have a UNIQUE email column then ensure that I'm not allow ...
Martyn's user avatar
  • 795
1 vote
2 answers
2k views

When running PHPUnit tests - is it common to use a separate database for testing?

I'm running tests from the same database as I use in development. I recall using Rails and I used a separate database for testing (mainly coz the tutorial I was going by said so). Made sense though. I ...
Martyn's user avatar
  • 795
5 votes
3 answers
610 views

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,...
rsman's user avatar
  • 1,319
2 votes
4 answers
2k views

Unit Test code generation

We have a project that was written over a period of 2 years, but has a poorly designed architecture and not unit tests. The software works well, but we're at the point where we want to refactor some ...
zavg's user avatar
  • 121
2 votes
3 answers
2k views

Unit-testing functions without business logic (only checks) [duplicate]

My team is trying to find out the best way to test one of our components; the main scope is to check if the user/actor has the right to complete the operation requested. The component is formed by ...
Jecnua's user avatar
  • 23
2 votes
1 answer
1k views

Specifying Query in Unit Test

When writing unit tests, should I specify the query that will be performed for interacting with the database? I can see both sides of this. On one hand, I want to make sure that the query that I ...
Schleis's user avatar
  • 3,416