Per Package Unit Testing in FLOW3 using PhpStorm

UPDATE

Do not do this! Check Karstens comment why - and go to his blog post about setting this up correctly.

Everything below this, line is not useful for anything (except perhaps learning that you should not try to create a fix for a problem that does not exist.)

Update: you may add you pear directory as a directory in the project, to ensure that you get them indexed (and get autocomplete on for instance ). On my mac, the pear package is located in /usr/local/share/pear - find where yours is by running:

pear config-show |grep "PEAR directory"

At Arnsbo Media we don't do unit testing on everything. Whereas for some projects (mostly containing models, views and controllers) we feel unit testing [and tdd in generel] is overkill - other projects (or some modules/packages) are prime candidates for tdd. Typically when we are creating service integration we do it test driven. My own definition is that when all users of a given package/module are other packages/modules - and not databases, webservers, end users and so forth - unit testing is a viable way to ensure longevity of the APIs, and solidity of the implementation.

At the moment we are doing several parts of a very big system in FLOW3. Each part is an independent package, and most of them are service layers, and/or abstractions of external systems. Each of these packages should be testable without depending on other packages (apart from the ones required by flow3). Thus we use FLOW3s built-in test framework (based on phpunit), but we have placed our own configuration and bootstrap for testing a package as part of the package.

It makes several assumptions however:

  • The package is placed in the directory FLOW3/Packages/Application
  • The FLOW3 packages are placed in FLOW3/Packages/Framework
  • Phpunit is installed
  • VfsStream is installed

The layout of the package is pretty standard.

Package contents

We have created a Build/PhpUnit directory in which we place the configuration file, and the bootstrap for the package. These files a copy-paste-able to any package. The content of both are copied from the framework supplied files in FLOW3/Build/Common/PhpUnit - and tailored to be used from the packages directory. For both files the only thing changed is how to resolve the directories from the current file. Otherwise everything is left as is.

After this all you have to do is setup a Run Configuration for PhpStorm.

Screen Shot 2011 11 24 at 08 28 54

Screen Shot 2011 11 24 at 08 29 48

All tests are placed in the Tests/Unit directory, and as a rule of thumb I place them in a matching subdirectory to where the class being tested is under the Classes directory, and name it by appending Test to the class- (and subsequently the file-) name. In this case we have a single GoogleHandler class, and a test for it.

Finally all you have to do is run the app using the phpunit run configuration, and you test results are ready for you.