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
100
votes
6 answers

How to test if string contains another string in PHPUnit?

I can't seem to find an assertion in PHPUnit that simply tests if a string is contained somewhere in another string. Trying to do something like this: public function testRecipe() { $plaintext = get_bread_recipe(); …
TheStoryCoder
  • 3,403
  • 6
  • 34
  • 64
98
votes
5 answers

PHPUnit: How do I create a function to be called once for all the tests in a class?

I have a PHPUnit test case class (consisting of some test functions). I would like to write a oneTimeSetUp() function to be called once for all my tests in the class (unlike the standard setUp() function which is called once for each test in the…
snakile
  • 52,936
  • 62
  • 169
  • 241
95
votes
7 answers

phpunit avoid constructor arguments for mock

What is the way to avoid phpunit having to call the constructor for a mock object? Otherwise I would need a mock object as constructor argument, another one for that etc. The api seems to be like this: getMock($className, $methods = array(), array…
Koen
92
votes
5 answers

How to run a specific phpunit xml testsuite?

how can i choose a specific testsuite to be executed? $ phpunit --configuration config.xml config.xml: library
Andreas Linden
  • 12,489
  • 7
  • 51
  • 67
92
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
  • 47,186
  • 75
  • 257
  • 421
91
votes
3 answers

How are integration tests written for interacting with external API?

First up, where my knowledge is at: Unit Tests are those which test a small piece of code (single methods, mostly). Integration Tests are those which test the interaction between multiple areas of code (which hopefully already have their own Unit…
Jess Telford
  • 12,880
  • 8
  • 42
  • 51
88
votes
5 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,883
  • 4
  • 17
  • 23
87
votes
11 answers

Mock private method with PHPUnit

I have a question about using PHPUnit to mock a private method inside a class. Let me introduce with an example: class A { public function b() { // some code $this->c(); // some more code } private function c(){ // some…
Tony
  • 3,425
  • 10
  • 30
  • 46
82
votes
9 answers

Laravel 5: PHPUnit and no code coverage driver available

I would like to use PHPUnit to create code coverage reports. I have tried a lot of installation setups found on the web. But nothing seems to work out. I use the latest version of Laravel 5 (>5.2) and PHPUnit v. 5.0.10. Further, I use MAMP on Mac OS…
LaDude
  • 1,383
  • 1
  • 11
  • 27
82
votes
3 answers

PHPUnit: assertInstanceOf() not working when passing type as second argument as non-string

I need to check if a variable is an object of the User type. User is my class $user my object $this->assertInstanceOf($user, User); This is not working. I have a use of undefined constant User - assumed 'User'.
user1173169
80
votes
9 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
  • 15,243
  • 20
  • 84
  • 92
79
votes
17 answers

phpunit require_once() error

I recently installed phpunit on my server via the pear installer. When I go to run a test I get the following error: PHP Warning: require_once(PHPUnit/Util/Filter.php): failed to open stream: No such file or directory in /usr/bin/phpunit on line…
user185305
  • 821
  • 1
  • 6
  • 4
78
votes
2 answers

How to Read / Improve C.R.A.P Index Calculated by PHP

I just started working with PHPUnit and its colorful code coverage reports. I understand all the numbers and percentages save one: The C.R.A.P index. Can anyone offer me a solid explanation of what it means, how to analyze it and how to lower it?
Levi Hackwith
  • 9,232
  • 18
  • 64
  • 115
78
votes
14 answers

Can I "Mock" time in PHPUnit?

... not knowing if 'mock' is the right word. Anyway, I have an inherited code-base that I'm trying to write some tests for that are time-based. Trying not to be too vague, the code is related to looking at the history of an item and determining if…
Narcissus
  • 3,144
  • 3
  • 27
  • 40
78
votes
13 answers

Installing PHPUnit via PEAR

I have problems installing PHPUnit 3.4.6 via PEAR 1.9.0. After I discover channel pear.phpunit.de and try to use one of following commands: pear install phpunit/PHPUnit pear install --alldeps phpunit/PHPUnit pear install --onlyreqdeps…
Ondrej Slinták
  • 31,386
  • 20
  • 94
  • 126