Jump to content

PHP Unit tests: database mocks?

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

Share this post

Link to post
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 :)

Share this post

Link to post
Share on other sites

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

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


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

  • Like 5

Share this post

Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By tpr
      Run Nette Tester tests within ProcessWire admin.
      (continued from here)

      AJAX interface for running Nette Tester tests, in bulk or manually display counter, error message and execution time in a table run all tests at once or launch single tests show formatted test error messages and report PHP syntax errors stop on first failed test (optional) hide passed tests (optional) display failed/total instead passed/total (optional) re-run failed tests only (optional) auto scroll (optional) include or exclude tests based on query parameters start/stop all tests with the spacebar reset one test or all tests (ctrl+click) TracyDebugger File Editor integration https://modules.processwire.com/modules/process-nette-tester/
    • By Mirza
      Hi All,
      I am trying to include the unit tests into our project which is in Processwire(I love it).
      Directory structure for the same will be shown below

      Here PWTestCase.php is the parent file where it is getting inherited from the PHPUNIT framework TestCase class and It is in same namespace ProcessWire; as shown below

      Now I have a basic unit test(FuncTest.php as shown below) to check the language calling the function getLanguage() residing in site/templates/_func.php.

      ***when try to run this test, getting undefined function getLanguage as shown below***

      Since I am using namespace ProcessWire; & bootstraping processwire in PWTestCase why i am not able to access functions? Can you please suggest where i am going wrong?
      Thanks in advance.
    • By FrancisChung
      I'm writing this to give back something to the community that has given so much up front over the past year.
      I noticed there's hardly any discussion about testing in these forums so I decided to write this quick primer to get some discussion going.
      I'm by no means an expert on phpunit or selenium but I had to jump through a few hoops to get it working (especially with PHPStorm), so I thought I figured I should share my experiences with the community. 

      Also, I'm hoping non Phpstorm users can still  pick something up useful in this post.

      Prerequisites : It is assumed Phpunit (https://phpunit.de/) is installed via Composer,   Selenium (http://www.seleniumhq.org/) and Php-webdriver for Selenium (https://github.com/facebook/php-webdriver) is preinstalled.
      For Phpstorm users, there's a fairly detailed installation and unit testing instructions here (https://www.jetbrains.com/help/phpstorm/2016.1/testing.html)
      I found some parts of it leaving me with unanswered questions, so I'm hoping this post will supplement any questions that you might encounter.

      Rather than writing a single monolithic post, I will write several posts covering different topics.
    • By FrancisChung
      Hi fellow Processwirers (?),
      I was wondering if anyone here uses PHPStorm in conjunction with PhpUnit?
      In particular, I'm trying to get it to work with Processwire so I can incorporate the Pages & other intrinsic Processwire objects into the testing.
      Any example or real life configuration examples would be most welcome.

      Thanks in advance.
    • By mr-fan
      Just some strange thing i've tested with PW.
      I've some experience with PW now, but one thing i found i don't have a clue where i've to search why....and i always wanna know WHY something happens...
      You all know online responsive website test services....like:
      and so on....
      but if i'm checking one of my websites build with PW there is gives me a blank screen...no screenshot there...so i have a closer look.
      first i switched browser...check - same result in ervery browser second i looked at other PW sites like http://processwire.com itself.....check no working too! third i picked some websites from the repository that build with pw.....check no working too! some examplelinks:
      ....and so on
      So why is a PW website not working in any of such online testing tools?
      strange regards mr-fan
  • Create New...