EzDevInfo.com

phpunit interview questions

Top phpunit frequently asked interview questions

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 code
  }
}

How can I stub the result of the private method to test the some more code part of the public function.

Solved partially reading here


Source: (StackOverflow)

Selenium 2 (WebDriver) and Phpunit?

Any one know how to use Selenium 2 with Phpunit? Are there any Selenium 2 samples in PHP?


Source: (StackOverflow)

Advertisements

SimpleTest vs PHPunit

I was wondering if anyone that have experience in both these stuff can shed some light on the significant difference between the two, if any?

Any specific strength of each that makes it suitable for any specific case?


Source: (StackOverflow)

PHPUnit assert that an exception was thrown?

Does anyone know whether there is an assert or something like that which can test whether an exception was thrown in the code being tested?


Source: (StackOverflow)

Testing Abstract Classes

How do I test the concrete methods of an abstract class with PHPUnit?

I'd expect that I'd have to create some sort of object as part of the test. Though, I've no idea the best practice for this or if PHPUnit allows for this.


Source: (StackOverflow)

Difference between assertEquals and assertSame in phpunit?

PHPUnit contains an assertEquals method: https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertEquals

It also has an assertSame method: https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertSame

At first glance it looks like they do the same thing. What is the difference between the two? Why are they both specified?


Source: (StackOverflow)

Best practices to test protected methods with PHPUnit [closed]

I found the discussion on Do you test private method informative.

I have decided, that in some classes, I want to have protected methods, but test them. Some of these methods are static and short. Because most of the public methods make use of them, I will probably be able to safely remove the tests later. But for starting with a TDD approach and avoid debugging, I really want to test them.

I thought of the following:

  • Method Object as adviced in an answer seems to be overkill for this.
  • Start with public methods and when code coverage is given by higher level tests, turn them protected and remove the tests.
  • Inherit a class with a testable interface making protected methods public

Which is best practice? Is there anything else?

It seems, that JUnit automatically changes protected methods to be public, but I did not have a deeper look at it. PHP does not allow this via reflection.


Source: (StackOverflow)

"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 the IDE and the browser. I would like to have the unit testing in the same environment.

I'm looking for something really simple and PHP based - I am planning to get into phpUnderControl (which has the functionality I'm looking for) but not yet.


Source: (StackOverflow)

How to test a second parameter in a PHPUnit mock object

This is what I have:

$observer = $this->getMock('SomeObserverClass', array('method'));
$observer->expects($this->once())
         ->method('method')
         ->with($this->equalTo($arg1));

But the method should take two parameters. I am only testing that the first parameter is being passed correctly (as $arg1).

How do test the second parameter?


Source: (StackOverflow)

How to install an older version of PHPUnit through PEAR?

I would like to downgrade my installation of PHPUnit 3.4 to 3.3. I'm just not sure how to do it.

How do I install version 3.3 of PHPUnit on Ubuntu using PEAR?


Source: (StackOverflow)

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?


Source: (StackOverflow)

phpunit mock method multiple calls with different arguments

Is there any way to define different mock-expects for different input arguments? For example, I have database layer class called DB. This class has method called "Query ( string $query )", that method takes an SQL query string on input. Can I create mock for this class (DB) and set different return values for different Query method calls that depends on input query string?


Source: (StackOverflow)

How to set up database-heavy unit tests in Symfony2 using PHPUnit?

I am quite new to the world of testing and I want to make sure I am on the right track.

I am trying to setup unit tests in a symfony2 project using phpunit.

PHPUnit is working and the simple default controller tests work fine. (Yet this is not about functional testing but unit testing my application.)

My project relies heavily on database interactions though, and as far as I understand from phpunit's documentation, I should set up a class based on \PHPUnit_Extensions_Database_TestCase, then create fixtures for my db and work from there.

Yet, symfony2 only offers a WebTestCase class which only extends from \PHPUnit_Framework_TestCase out of the box.

So am I right to assume that I should create my own DataBaseTestCase which mostly copies WebTestCase, only difference being that it extends from \PHPUnit_Extensions_Database_TestCase and implements all its abstract methods?

Or is there another "built-in" recommended workflow for symfony2 concerning database-centric tests?

As I want to make sure that my models store and retrieve the right data, I do not want to end up testing the specifics of doctrine by accident.


Source: (StackOverflow)

How do I use PHPUnit with Zend Framework?

I would like to know how to write PHPUnit tests with Zend_Test and in general with PHP.


Source: (StackOverflow)

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 Matcher()));

$context->expects($this->any())
   ->method('offsetGet')
   ->with('Logger')
   ->will($this->returnValue(new Logger()));

I use PHPUnit 3.5.10 and it fails when I ask for Matcher because it expects "Logger" argument. It is like the second expectation is rewriting the first one, but when I dump the mock, everything looks ok.


Source: (StackOverflow)