6

I am curious as to how others approach this. Writing a test ain't so bad, but mocking kind of sucks a bit and cuts my flow. Is it ok for one to have a 'fixtures' directory and have say mock_db.php for example with just that particular mock declaration?

Going one step further, would it be bad practice to have those mocks abstracted in a function?

Ie:

 // function to include a db mock
   include_once 'test/fixtures/dbmock.php';

   $mockMYSQL = $dbmock('mysql', 'db1');
   $mockMSSQL = $dbmock('mssql', 'db2');

JUst interested to know how other experienced testers handle this. I'm writing scripts to sync 2 databases so this example may become very relevant.

3
  • Don't data providers help you? Commented May 19, 2011 at 6:56
  • Are you talking about PHPUnit mock objects created using $this->getMock('<class>') or are these custom-written mocks? Using fixtures to create test support objects is perfectly valid and recommended. Commented May 19, 2011 at 19:49
  • @David I would like to have mocks reusable for different test suites. Pretty green still with this mocking business, and clueless on how to implement this. Commented May 21, 2011 at 4:34

1 Answer 1

4

I would go either with inheritance - having the common mock objects created and returned in protected get* methods in common parent test case class.

Or you can create cleaner and standalone class that you would instantiate in your test suites and let it create your mock objects. I would prefer this way, but it has one downside - you probably can not or should not use the PHPUnit_Framework_TestCase getMock() method. I recommend you to look at this method and try to use its logic in your standalone class.

Including global functions is not very OOP, it's rather magic that PHP allows but you should avoid it :)

Sign up to request clarification or add additional context in comments.

3 Comments

Hey thanks for the tips, subclassing in the testcase for small classes is what the piece I was missing. I have already created a couple of standalones as per yer suggestion, but I still have to look into a way to reuse getMock() creators.
8 years later... Anyone who could make an example of how to do this? :)
Subclassing TestCase has some issues regarding the $this context which will not refer to the same instance as the one automatically created by PHPUnit for the currently running test case. This will end up with PHPUnit not be aware of your expectations that you set up on the mock object, as the code reusing the mock creation logic will have its own $this. In this case you will either need to pass the $this instance of the class extending TestCase that has reusable mock creation logic and use this $this, or use a PHP trait instead and place your reusable logic in the trait.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.