phpunit avoid constructor arguments for mock

You can use getMockBuilder instead of just getMock: $mock = $this->getMockBuilder(‘class_name’) ->disableOriginalConstructor() ->getMock(); See the section on “Test Doubles” in PHPUnit’s documentation for details. Although you can do this, it’s much better to not need to. You can refactor your code so instead of a concrete class (with a constructor) needing to be injected, you … Read more

How to test if string contains another string in PHPUnit?

As you could tell assertContains is for checking that an array contains a value. Looking to see if the string contains a substring, your simplest query would be to use assertRegexp() $this->assertRegexp(‘/flour/’, $plaintext); You would just need to add the delimiters. If you really want to have an assertStringContains assertion, you can extend PHPUnit_Framework_TestCase and … Read more

Test PHP headers with PHPUnit

The issue is that PHPUnit will print a header to the screen and at that point you can’t add more headers. The work around is to run the test in an isolated process. Here is an example <?php class FooTest extends PHPUnit_Framework_TestCase { /** * @runInSeparateProcess */ public function testBar() { header(‘Location : http://foo.com’); } … Read more

How to skip tests in PHPunit?

The fastest and easiest way to skip tests that are either broken or you need to continue working on later is to just add the following to the top of your individual unit test: $this->markTestSkipped(‘must be revisited.’);

SimpleTest vs PHPunit

This question is quite dated but as it is still getting traffic and answers I though I state my point here again even so I already did it on some other (newer) questions. I’m really really baffled that SimpleTest still is considered an alternative to phpunit. Maybe i’m just misinformed but as far as I’ve … Read more

how to test specific test class using phpunit in laravel

After trying several ways, I found out that I don’t need to include the folder to test the specific test class. This works for me it runs all the test on the class: phpunit –filter ApplicationVersionFormatTest I think it’s because my ApplicationVersionFormatTest extend The TestCase and return application instance which serves as the “glue” for … Read more

How to tell phpunit to stop on failure

Add the stopOnFailure=”true” attribute to your phpunit.xml root element. You can also use it in the CLI: phpunit –stop-on-failure Info from manual and some others that are maybe useful for you: stopOnError – “Stop execution upon first error.” stopOnFailure – “Stop execution upon first error or failure.” stopOnIncomplete – “Stop execution upon first incomplete test.” … Read more