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
9185 questions
69
votes
8 answers

Run PHPUnit Tests in Certain Order

Is there a way to get the tests inside of a TestCase to run in a certain order? For example, I want to separate the life cycle of an object from creation to use to destruction but I need to make sure that the object is set up first before I run the…
dragonmantank
  • 14,475
  • 19
  • 80
  • 90
67
votes
4 answers

How do I get HTTP Request body content in Laravel?

I am making an API with Laravel 5 and I'm testing it with PHPUnit. I need to test legacy functionality for compatibility, which is an XML POST. As of right now, my first test looks like: public function testPostMessages() { $response =…
beznez
  • 1,633
  • 3
  • 13
  • 23
65
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
  • 5,890
  • 7
  • 35
  • 57
64
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
  • 10,663
  • 12
  • 44
  • 71
64
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
  • 3,610
  • 1
  • 23
  • 27
61
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
  • 842
  • 1
  • 6
  • 12
60
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,185
  • 8
  • 36
  • 50
60
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,550
  • 1
  • 14
  • 31
60
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,697
  • 6
  • 37
  • 50
59
votes
4 answers

How can I suppress PHPCS warnings using comments?

My TestMyClass.php has two class definitions in the same file (unit testing class), and PHP Code Sniffer complains about each class must be in a file by itself. How can I suppress this warning? class MyClassImpl implements MyInterface { //…
gremo
  • 45,925
  • 68
  • 233
  • 380
58
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
  • 2,893
  • 7
  • 32
  • 61
57
votes
7 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,114
  • 8
  • 37
  • 43
56
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
  • 877
  • 1
  • 6
  • 28
56
votes
6 answers

How to use phpunit installed from composer?

I want to start unit testing my symfony 2 application with phpunit. I installed phpunit using composer (per-project dependancy). http://www.phpunit.de/manual/current/en/installation.html How do I now run the phpunit command on Zend Server? I don't…
Tjorriemorrie
  • 14,436
  • 15
  • 79
  • 119
52
votes
3 answers

How can i get phpunit to run tests from all files in a folder?

From what I've read, it seems like I should be able to set up a folder, e.g. tests/ , put a few files in it with unit test classes, and then run phpunit on that file and have it find and run the tests. For whatever reason, in my installation (on OS…
jsdalton
  • 6,107
  • 3
  • 38
  • 39