Jump to content

PHP Unit tests: database mocks?


chrizz
 Share

Recommended Posts

I am currently developing a little application which is supposed to run on a raspberry. I decided to take advantage of PW's capabilities to deal with data not only in the sense of a common website but more as an application. 

I wrote a lot of modules for this and I wrote tests for some of the methods (using PHPUnit). This works fine so far for methods which are independent from database (e.g. calculating an upcoming date/time by a given schedule). Now I am thinking of extending this by writing tests also for methods which rely on data from the database (e.g. it need to be checked if something exists on that given date/time). This information is currently stored as page in PW. 

One of my thoughts: create necessary pages in PW for running the test and delete them afterwards. But somehow this just feels wrong. 

Any kind of input how you would tackle this would be great!

Thanks a lot! 

 

  • Like 1
Link to comment
Share on other sites

yeah, that might be a approach I can test. Since I use a very comfortable way of modifying the date/time by freezing it at some point this could definitively work. 

Thanks for the idea of using a multi-instance :)

Link to comment
Share on other sites

  • 2 weeks later...

Just as a heads up if someone needs database testing: I am trying a different approach at the moment: I simply added a database configuration to the config.php which is used if PW is called from PHPUnit.

in config.php

if(isset($_ENV['UNITTEST']) && $_ENV['UNITTEST'] == true) {

	$config->dbHost = 'localhost';
	$config->dbName = 'db-test';
	$config->dbUser = '...';
	$config->dbPass = '...';
	$config->dbPort = '3306';	
	$config->httpHosts = array('localhost');
}

 

and my PHPUnit configuration.xml looks like this

<phpunit
	... some settings here ...
>
	<php>
		<env name="UNITTEST" value="true" />
	</php>
</phpunit>

 

maybe this will save someone a headache in the future:)

  • Like 5
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...