How do I assert the result is an integer in PHPUnit?
$this->assertInternalType(“int”, $id); Edit: As of PHPUnit 8, the answer is: $this->assertIsInt($id);
$this->assertInternalType(“int”, $id); Edit: As of PHPUnit 8, the answer is: $this->assertIsInt($id);
The xdebug.profiler_enable setting can’t be changed at runtime but only at the start of script. Running phpunit -d foo=bar will just lead to phpunit calling ini_set(“foo”, “bar”); and that doesn’t work since the value can’t change at runtime. See: xdebug.profiler_enable Enables Xdebug’s profiler which creates files in the profile output directory. Those files can be … Read more
Well, at first. You need to tell the autoloader where to find the php file for a class. That’s done by following the PSR-0 standard. The best way is to use namespaces. The autoloader searches for a Acme/Tests/ReturningTest.php file when you requested a Acme\Tests\ReturningTest class. There are some great namespace tutorials out there, just search … Read more
In my case the answer turned out to be quite simple: $this->expects($this->at(0)) ->method(‘write’) ->with(/* first set of params */); $this->expects($this->at(1)) ->method(‘write’) ->with(/* second set of params */); The key is to use $this->at(n), with n being the Nth call of the method. I couldn’t do anything with any of the logicalOr() variants I tried.
Annotations are the answer. /** @test */ public function it_tests_something() { … } Adding that @test tells phpunit to treat the function as a test, regardless of the name.
I believe the way to do this is: $observer->expects($this->once()) ->method(‘method’) ->with($this->equalTo($arg1),$this->equalTo($arg2)); Or $observer->expects($this->once()) ->method(‘method’) ->with($arg1, $arg2); If you need to perform a different type of assertion on the 2nd arg, you can do that, too: $observer->expects($this->once()) ->method(‘method’) ->with($this->equalTo($arg1),$this->stringContains(‘some_string’)); If you need to make sure some argument passes multiple assertions, use logicalAnd() $observer->expects($this->once()) ->method(‘method’) ->with($this->logicalAnd($this->stringContains(‘a’), $this->stringContains(‘b’)));
I’d suggest to check the database servers connection limits and pools. For example if you’ve got a max limit of 100 connections, and some of the tests leaves connections open (“leaks”), you’d hit the limits there. That would also explain why sometimes it works and sometimes it hits the limit, as your database could handle … Read more
I recently came up with another solution that is great if you are using PHP 5.3 namespaces. You can implement a new time() function inside your current namespace and create a shared resource where you set the return value in your tests. Then any unqualified call to time() will use your new function. For further … Read more
I’ll start of by linking to the manual and then going into what I’ve seen and heard in the field. Organizing phpunit test suites Module / Test folder organization in the file system My recommended approach is combining the file system with an xml config. tests/ \ unit/ | – module1 | – module2 – … Read more
@Toader Mihai offered a solid explanation. (+1 from me) How to lower it: Write less complex code OR write better tested code. (See the graph below) Better tested Code ? In this context this just means: A higher code coverage and usually results in writing more tests. Less complex code ? For example: Refactor your … Read more