News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Michelangelo van Dam:
Look mama, no databases
March 18, 2013 @ 10:11:46

In his most recent post Michelangelo van Dam talks about unit testing and databases and how, to effectively test what should be tested (the code, not "the ability to fetch data") you need to correctly mock your database objects.

When I state "as is", I truly mean the way it's being used in production. So the database call collects real data on which business logic is applied. You can see this is not a healthy situation, especially when you also have services that apply business logic on data and store it back into the database. In "Chris Hartjes wrote this one sentence that says it all: "Unit test suites are meant to be testing code, not the ability of a database server to return results". And he's right, you shouldn't use database connections when your testing business rules and functional logic.

He goes on to show a few code examples that show a pre-mocked state of testing where the Product information is pulled directly from a PDO connection. The more correct version mocks out this object, though, and overrides the "execute" and "fetchAll" methods to return mocked results.

0 comments voice your opinion now!
phpunit unittest database mock object pdo database


blog comments powered by Disqus

Similar Posts

Brandon Savage's Blog: 5 (Good) Lessons The Government Teaches Us About Object-Oriented Programming

Sematopia.com: How to - Backup MySQL database & email results using PHP

Damian Sromek's Blog: [How to] Run PHPUnit tests using database 10x faster

Ivo Jansch's Blog: Validating OCL constraints in PHP objects

Evert Pot: Accessing protected properties from objects that share the same ancestry.


Community Events

Don't see your event here?
Let us know!


laravel5 extension framework interview podcast security laravel series language api unittest community introduction symfony voicesoftheelephpant version opinion library threedevsandamaybe release

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework