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
48
votes
7 answers

Call to undefined method PHP_CodeCoverage_Filter::getInstance()

I've got a fresh copy of PHPUnit installed on my system (Ubuntu 11), but whenever I type phpunit in the console I get the following error: PHP Fatal error: Call to undefined method PHP_CodeCoverage_Filter::getInstance() in /usr/bin/phpunit on line…
Ian Hunter
  • 9,466
  • 12
  • 61
  • 77
48
votes
3 answers

phpunit - testing is painfully slow

I am diving deeper and deeper in the world of unit testing. One issue I encountered, and this is where I would like feedback, is when one runs multiple test suites, maybe it is just me but I need to use the parameter --process-isolation for my tests…
Stephane Gosselin
  • 9,030
  • 5
  • 42
  • 65
48
votes
8 answers

How to exclude file from PHPUnit test suite in xml config?

I have following, very simple, XML config for PHPUnit: _tests
Ondrej Slinták
  • 31,386
  • 20
  • 94
  • 126
48
votes
10 answers

Passing parameters to PHPUnit

I'm starting to write PHPUnit tests and I'd like the tests to be run from developers machines as well as from our servers. Developers machines are set up differently than the servers and even differently from each other. To run in these different…
dl__
  • 4,500
  • 5
  • 28
  • 34
48
votes
3 answers

PHPUnit's returnValueMap not yielding expected results

I'm trying to use PHPUnit's returnValueMap() to stub out the results of a read. It isn't yielding the expected results, but an equivalent returnCallback() does. I've made my test case available if you'd like to inspect it…
Brad Koch
  • 19,267
  • 19
  • 110
  • 137
47
votes
6 answers

how to unit test curl call in php

How would you go about unit testing a curl implementation? public function get() { $ch = curl_init($this->request->getUrl()); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result =…
Yader Hernandez
  • 575
  • 1
  • 5
  • 5
47
votes
3 answers

How to exclude files / code blocks from code coverage with Netbeans / PHPStorm / PHPUnit integration

Requirements: Netbeans with PHPUnit(6.9) EDIT: Same applies, for example, to PHPStorm How to: Exclude lines from code coverage. Exclude code blocks (lines) from code coverage.
Alfred
  • 60,935
  • 33
  • 147
  • 186
47
votes
3 answers

How to unit test functions that use type hinting

Lets say I have a class that contains a function that uses type hinting like this: class Testable { function foo (Dependency $dependency) { } } And I want to unit test this class Testable using this code: $dependencyMock =…
user3009816
  • 797
  • 2
  • 8
  • 16
47
votes
11 answers

Unit Testing with items that need to send headers

I'm currently working with PHPUnit to try and develop tests alongside what I'm writing, however, I'm currently working on writing the Session Manager, and am having issues doing so... The constructor for the Session handling class is private…
Mez
  • 24,430
  • 14
  • 71
  • 93
47
votes
3 answers

PHPUnit assert no method is called

I have a ClassA that uses a ServiceB. In a certain case, ClassA should end up not invoking any methods of ServiceB. I now want to test this and verity no methods are indeed called. This can be done as follows: $classA->expects( $this->never()…
Jeroen De Dauw
  • 10,321
  • 15
  • 56
  • 79
46
votes
9 answers

PHP Mocking Final Class

I am attempting to mock a php final class but since it is declared final I keep receiving this error: PHPUnit_Framework_Exception: Class "Doctrine\ORM\Query" is declared "final" and cannot be mocked. Is there anyway to get around this final behavior…
DanHabib
  • 824
  • 4
  • 15
  • 25
46
votes
2 answers

Why does phpunit not show any errors in the console

I'm using phpunit with Laravel 4 framework. Why is it that when there's a PHP error during the tests, no error messages are shown (eg: missing method)? How can we get phpunit to show all errors?
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
45
votes
1 answer

PHPUnit mock objects and method type hinting

I'm trying to create a mock object of \SplObserver using PHPunit and attach the mocked object to an \SplSubject. When I try to attach the mocked object to a class that implements \SplSubject, I get a catchable fatal error saying that the mocked…
Michael Dowling
  • 5,098
  • 4
  • 32
  • 28
45
votes
4 answers

phpunit --debug still displays only dots

I want to see which test is currently executed during a phpunit run. I use the --debug param but still only get dots: $ phpunit --debug PHPUnit 3.7.19 by Sebastian Bergmann. Configuration read from…
Alex
  • 32,506
  • 16
  • 106
  • 171
44
votes
3 answers

Mocks vs Stubs in PHPUnit

I know stubs verify state and the mocks verify behavior. How can I make a mock in PHPUnit to verify the behavior of the methods? PHPUnit does not have verification methods (verify()), And I do not know how to make a mock in PHPUnit. In the…
RodriKing
  • 822
  • 2
  • 10
  • 20