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
61
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,555
  • 4
  • 40
  • 39
61
votes
10 answers

Laravel testing, get JSON content

In Laravel's unit test, I can test a JSON API like that: $this->post('/user', ['name' => 'Sally']) ->seeJson([ 'created' => true, ]); But what if I want to use the response. How can I get the JSON response (as an array) using…
rap-2-h
  • 30,204
  • 37
  • 167
  • 263
60
votes
2 answers

PHPUnit: expects method meaning

When I create a new mock I need to call the expects method. What exactly it does? What about its arguments? $todoListMock = $this->getMock('\Model\Todo_List'); $todoListMock->expects($this->any()) ->method('getItems') …
thom
  • 691
  • 1
  • 5
  • 5
59
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
  • 16,818
  • 20
  • 89
  • 131
56
votes
3 answers

PHPunit result output on the CLI not showing test names

I'm running a brand new test suite in PHPUnit, I'd like to see the result of each test with the test name next to it. It would make fixing broken tests and TDD easier. PHPunit does output the broken messages afterwards but your eyes go wonky after…
Jujhar Singh
  • 3,641
  • 5
  • 29
  • 38
56
votes
6 answers

phpunit - mockbuilder - set mock object internal property

Is it possible to create a mock object with disabled constructor and manually setted protected properties? Here is an idiotic example: class A { protected $p; public function __construct(){ $this->p = 1; } public function…
inf3rno
  • 24,976
  • 11
  • 115
  • 197
55
votes
3 answers

In PHPUnit, how do I indicate different with() on successive calls to a mocked method?

I want to call my mocked method twice with different expected arguments. This doesn't work because expects($this->once()) will fail on the second call. $mock->expects($this->once()) ->method('foo') …
james
  • 3,543
  • 8
  • 31
  • 39
53
votes
3 answers

unit testing and Static methods

Reading up and picking up on unit testing, trying to make sense of the following post on that explains the hardships of static function calls. I don't clearly understand this issue. I have always assumed static functions were a nice way of…
Stephane Gosselin
  • 9,030
  • 5
  • 42
  • 65
53
votes
4 answers

PHPUnit. Error: No code coverage driver is available. (having xdebug installed)

There is an answer for similar question Laravel 5: PHPUnit and no code coverage driver available that works, but i have xdebug installed. (if this matter I'm using: windows7, netbeans8.1, php7, wamp3) Displaying code coverage in Netbeans result in:…
53
votes
3 answers

Can I mock an interface implementation with PHPUnit?

I've got an interface I'd like to mock. I know I can mock an implementation of that interface, but is there a way to just mock the interface?
Dmitry Minkovsky
  • 36,185
  • 26
  • 116
  • 160
52
votes
7 answers

Mock in PHPUnit - multiple configuration of the same method with different arguments

Is it possible to configure PHPUnit mock in this way? $context = $this->getMockBuilder('Context') ->getMock(); $context->expects($this->any()) ->method('offsetGet') ->with('Matcher') ->will($this->returnValue(new…
Václav Novotný
  • 804
  • 2
  • 7
  • 13
52
votes
7 answers

Setting up PHPUnit on OSX

Though I'm sure others have eventually managed to figure this out, I've been following the various documentation out there and have been having a heck of a rough time of it. http://www.phpunit.de/manual/current/en/installation.html Makes it sound…
Alex C
  • 16,624
  • 18
  • 66
  • 98
52
votes
5 answers

How do I run all my PHPUnit tests?

I have script called Script.php and tests for it in Tests/Script.php, but when I run phpunit Tests it does not execute any tests in my test file. How do I run all my tests with phpunit? PHPUnit 3.3.17, PHP 5.2.6-3ubuntu4.2, latest Ubuntu Output: $…
JJ.
  • 1,099
  • 1
  • 8
  • 12
51
votes
15 answers

PHPUnit - does nothing, no errors, no output

Sorry for another 'phpunit doesn't work' question. It used to work for years now. Today I reinstalled PEAR and phpunit for reasons not connected to this problem. Now when I run phpunit as I usually did. Nothing happens. The cli just shows me a new…
markus
  • 40,136
  • 23
  • 97
  • 142
51
votes
11 answers

"Web interface" to PHPUnit tests?

Is there a simple "Web interface" to running PHPUnit test suites? i.e. a PHP script that runs the test on the command line, and outputs a nicely formatted HTML result. I develop web applications, and the day-to-day workflow usually switches between…
Pekka
  • 442,112
  • 142
  • 972
  • 1,088