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
77
votes
2 answers

PHPUnit best practices to organize tests

I am currently going to start from scratch with the phpunit tests for a project. So I was looking into some projects (like Zend) to see how they are doing things and how they organizing their tests. Most things are pretty clear, only thing I have…
enricog
  • 4,226
  • 5
  • 35
  • 54
75
votes
3 answers

How to force a failure with phpunit

Is there a more official way to force a phpunit failure than $this->assertTrue(false)?
Parris Varney
  • 11,320
  • 12
  • 47
  • 76
74
votes
4 answers

Phpunit tests gives warning no tests found in class

I am trying to learn how to test with phpunit and laravel. When start the test using phpunit command, I am getting a warning : There was 1 failure: 1) Warning No tests found in class "PostsTest". FAILURES! Tests: 2,…
ytsejam
  • 3,291
  • 7
  • 39
  • 69
73
votes
1 answer

phpunit restarting tests randomly

I am trying to test my symfony2 application using PHPUnit. I got one project where everything works as expected, but on my other project I have this strange behaviour that PHPUnit either stops executing the Test Suite randomly near the end of all…
Sgoettschkes
  • 13,141
  • 5
  • 60
  • 78
73
votes
1 answer

How to test a second parameter in a PHPUnit mock object

This is what I have: $observer = $this->getMock('SomeObserverClass', array('method')); $observer->expects($this->once()) ->method('method') ->with($this->equalTo($arg1)); But the method should take two parameters. I am only…
Joel
  • 11,431
  • 17
  • 62
  • 72
72
votes
4 answers

PHPUnit: how do I mock multiple method calls with multiple arguments?

I am writing a unit test for a method using PHPUnit. The method I am testing makes a call to the same method on the same object 3 times but with different sets of arguments. My question is similar to the questions asked here and here The questions…
Thomas
  • 962
  • 1
  • 6
  • 12
69
votes
3 answers

Remove "Remaining deprecation notices" in Symfony 2.8

I'm new on Symfony and PHPUnit. I would like to run some tests, there are a lot of deprecation notices. But it does not matter because I'll stay on Symfony 2.8 for now. Do you know if it's possible to remove them? I try to find out by myself, but…
Chilipote
  • 1,037
  • 1
  • 8
  • 30
69
votes
5 answers

Autoloading classes in PHPUnit using Composer and autoload.php

I have just installed PHPUnit version 3.7.19 by Sebastian Bergmann via Composer and have written a class I would like to unit test. I would like to have all my classes autoloaded into each unit test without having to use include or require at the…
Jasdeep Khalsa
  • 6,740
  • 8
  • 38
  • 58
69
votes
9 answers

How can I get XDebug to run with PHPUnit on the CLI?

I've tried running the following CLI command: phpunit -d xdebug.profiler_enable=on XYZTestCase.php but it just runs as normal. Can anyone point me in the right direction?? Thx! Here's the XDebug settings: xdebug xdebug support =>…
blacktie24
  • 4,985
  • 6
  • 41
  • 52
68
votes
1 answer

test if array contains value using PHPUnit

I created this array of objects: $ad_1 = new AdUnit(array('id' => '1', 'name' => 'Ad_1', 'description' => 'great ad', 'code' => 'alpha', 'widget_id' => '123')); $ad_2 = new AdUnit(array('id' => '2', 'name' => 'Ad_2', 'description' => 'good ad',…
Donoven Rally
  • 1,670
  • 2
  • 17
  • 34
67
votes
6 answers

How do I assert the result is an integer in PHPUnit?

I would like to be able test that a result is an integer (1,2,3...) where the function could return any number, e.g.: $new_id = generate_id(); I had thought it would be something like: $this->assertInstanceOf('int', $new_id); But I get this…
bnp887
  • 5,128
  • 2
  • 26
  • 30
66
votes
3 answers

How to unit test PHP traits

I want to know if there is a solution on how to unit-test a PHP trait. I know we can test a class which is using the trait, but I was wondering if there are better approaches. Thanks for any advice in advance :) EDIT One alternative is to use the…
Ali
  • 2,993
  • 3
  • 19
  • 42
64
votes
8 answers

PHPUnit Mock Objects and Static Methods

I am looking for the best way to go about testing the following static method (specifically using a Doctrine Model): class Model_User extends Doctrine_Record { public static function create($userData) { $newUser = new self(); …
rr.
  • 6,484
  • 9
  • 40
  • 48
62
votes
3 answers

What is .phpunit.result.cache

When I run tests with PhpUnit on a new package I'm creating for Laravel, it generates the file .phpunit.result.cache. What to do with that? Do I add it to my .gitignore file or not? I'm using PHPUnit 8.0.4
Davide Casiraghi
  • 15,591
  • 9
  • 34
  • 56
61
votes
2 answers

How do I use PHPUnit with CodeIgniter?

I have read and read articles on PHPUnit, SimpleTest, and other Unit Testing frameworks. They all sound so great! I finally got PHPUnit working with Codeigniter thanks to https://bitbucket.org/kenjis/my-ciunit/overview Now my question is, how do I…
zechdc
  • 3,374
  • 9
  • 40
  • 52