Questions tagged [phpunit]

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks.

Latest stable version: 8.2 (April 2019)

Installation via PHAR

wget https://phar.phpunit.de/phpunit-8.phar
php phunit.phar

Installation via Composer

composer require --dev phpunit/phpunit ^8
9985 questions
44
votes
6 answers

How do you use PHPUnit to test a function if that function is supposed to kill PHP?

Essentially I have a method of a class called killProgram, which is intended to send a hTTP redirect and then kill PHP. How am I supposed to test this? When I run phpunit it doesn't return anything for that test, and closes completely. Right now I'm…
Zee Spencer
  • 3,460
  • 6
  • 29
  • 31
43
votes
5 answers

How to install an older version of PHPUnit through PEAR?

I would like to downgrade my installation of PHPUnit 3.4 to 3.3. I'm just not sure how to do it. How do I install version 3.3 of PHPUnit on Ubuntu using PEAR?
Andrew
  • 227,796
  • 193
  • 515
  • 708
43
votes
3 answers

How to set up database-heavy unit tests in Symfony2 using PHPUnit?

I am quite new to the world of testing and I want to make sure I am on the right track. I am trying to setup unit tests in a symfony2 project using phpunit. PHPUnit is working and the simple default controller tests work fine. (Yet this is not…
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
42
votes
5 answers

Can phpunit use multiple data provider

One question in short: can phpunit use multiple data provider when running test? For example, I have a method called getById, and I need to run both successful and unsuccessful testcases for it. The successful testcases means that it can return a…
imcoddy
  • 803
  • 2
  • 8
  • 10
42
votes
5 answers

Install phpunit on windows

How to install phpunit? I read documentation https://github.com/sebastianbergmann/phpunit, but have an error: >pear upgrade PEAR Nothing to upgrade >pear config-set auto_discover 1 config-set succeeded >pear install pear.phpunit.de/PHPUnit No…
Dmitry
  • 7,457
  • 12
  • 57
  • 83
41
votes
6 answers

Creating a mock in phpunit without mocking any methods?

When I'm unit-testing my php code with PHPUnit, I'm trying to figure out the right way to mock an object without mocking any of its methods. The problem is that if I don't call getMockBuilder()->setMethods(), then all methods on the object will be…
Brian Kendig
  • 2,452
  • 2
  • 26
  • 36
41
votes
6 answers

How do I use PHPUnit with Zend Framework?

I would like to know how to write PHPUnit tests with Zend_Test and in general with PHP.
Thomas Schaaf
41
votes
7 answers

PHPUnit "Mocked method does not exist." when using $mock->expects($this->at(...))

I've run into a strange issue with PHPUnit mock objects. I have a method that should be called twice, so I'm using the "at" matcher. This works for the first time the method is called, but for some reason, the second time it's called, I get "Mocked…
rr.
  • 6,484
  • 9
  • 40
  • 48
41
votes
10 answers

How to specify a different .env file for phpunit in Laravel 5?

I have a .env file containing my database connection details, as is normal for Laravel 5. I want to override these for testing, which I can do in phpunit.xml. However, doing this seems to go against the philosophy of .env which is not to commit…
Gnuffo1
  • 3,478
  • 11
  • 39
  • 53
41
votes
3 answers

Organizing PHPUnit Tests in Namespaces

I see two options for organizing PHPUnit unit tests into a namespace hierarchy. What are the advantages/disadvantages to these two approaches? Are there any obvious flaws I haven't considered that would make one the obvious better choice? Consider a…
Wilco
  • 32,754
  • 49
  • 128
  • 160
41
votes
2 answers

Symfony functional test - custom headers not passing through

For some reason when i sent a new $client->request the headers I specify get lost: public function testGetClientsAction() { $client = static::createClient(); $cookie = new Cookie('locale2', 'en', time() + 3600 * 24 * 7, '/', null, false,…
greg
  • 6,853
  • 15
  • 58
  • 71
40
votes
8 answers

Phpunit, how to test if method does "nothing"?

class Testme() { public function testMe ($a) { if ($a == 1) { throw new Exception ('YAY'); } } } so its easy to test if it threw exception /** * @expectedException Exception */ public function…
John Smith
  • 6,129
  • 12
  • 68
  • 123
40
votes
8 answers

How do I correctly install PHPUnit with PEAR?

I have had to de- and reinstall a newer version of PHPUnit following these directions. Now when I'm launching this line sudo pear install --alldeps phpunit/PHPUnit I see an error message, that looks like this. Unknown remote channel:…
twigmac
  • 1,772
  • 3
  • 21
  • 38
39
votes
3 answers

Unit test for mocking a method called by new class object

I am writing unit test for an existing code which is like this class someClass { public function __construct() { ... } public function someFoo($var) { ... $var = "something"; ... $model = new someClass(); …
Sumitk
  • 1,485
  • 6
  • 19
  • 31
39
votes
5 answers

Keep getting 'You have requested a non-existent service "test.client"' in Symfony2 Beta3

I'm trying to setup a unit test but whenever I run "phpunit -c app" I get this error: Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "test.client". The code in my test case is…
pogo
  • 2,287
  • 2
  • 25
  • 36