Phpunit - reuse mock objects for multiple test suites

I am curious how others approach this. Writing a test is not so bad, but the mocking look sucks and cuts my flow. Is it possible to have a fixtures directory and say mock_db.php, for example, only with this particular mock declaration?

Going further, would it be bad practice to have those bullying abstracted into functions?

Those:

// function to include a db mock include_once 'test/fixtures/dbmock.php'; $mockMYSQL = $dbmock('mysql', 'db1'); $mockMSSQL = $dbmock('mssql', 'db2'); 

JUst is interested in how other experienced testers deal with this. I write scripts to synchronize two databases, so this example can become very relevant.

+6
source share
1 answer

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

Or you can create a cleaner and stand-alone class that you create in your test suites and create your breadboard objects. I would prefer this, but it has one drawback - you probably cannot or should not use the PHPUnit_Framework_TestCase getMock () method. I recommend that you look at this method and try to use its logic in your autonomous class.

Enabling global functions is not very OOP, it is pretty magic that PHP allows, but you should avoid it :)

+3
source

Source: https://habr.com/ru/post/888506/


All Articles