What are some OOP strategies for working with a database but keeping things unit testable? Say I have a User class and my production environment works against MySQL. I see a couple possible approaches, shown here using PHP:
Pass in a $data_source with interfaces for
load()
andsave()
, to abstract the backend source of data. When testing, pass a different data store.$user = new User( $mysql_data_source ); $user->load( 'bob' ); $user->setNickname( 'Robby' ); $user->save();
Use a factory that accesses the database and passes the result row to User's constructor. When testing, manually generate the $row parameter, or mock the object in UserFactory::$data_source. (How might I save changes to the record?)
class UserFactory { static $data_source; public static function fetch( $username ) { $row = self::$data_source->get( [params] ); $user = new User( $row ); return $user; } }
I have Design Patterns and Clean Code here next to me, but I'm struggling to find applicable concepts.