Has Craft stopped shipping with unit tests? I'm trying to unit test a plugin and have noticed from several previous answers (e.g PHPUnit testing for Craft plugins or controllers) that it was possible at some stage using Craft's local tests. Have these since been removed? There no longer seems to be a tests folder inside the latest download of Craft/app.

What is the current best practice for setting up Unit Tests using Craft 2 in a plugin?

  • That's correct, Craft no longer ships with PHPunit included. It was removed in Craft 2.5.2750, which was released on Dec 1, 2015.
    – Lindsey D
    Aug 25 '17 at 22:42
  • So whats the best way to bootstrap craft for unit testing in a plugin?
    – lowe_22
    Aug 25 '17 at 22:48
  • That I don't know. Sorry.
    – Lindsey D
    Aug 25 '17 at 22:50

I found a bootstrap that works for me apart from an issue with the database connection.

I can't find the link now but here is the code:

define("CRAFT_ENVIRONMENT", "dev");
// PHP sucks at resolving symlinked directories
define('CRAFT_BASE_PATH', realpath(str_replace("plugins/myplugin", '', dirname(__FILE__))).'/');

// Define app constants
defined('CRAFT_APP_PATH')          || define('CRAFT_APP_PATH',          CRAFT_BASE_PATH.'app/');
defined('CRAFT_CONFIG_PATH')       || define('CRAFT_CONFIG_PATH',       CRAFT_BASE_PATH.'config/');
defined('CRAFT_VENDOR_PATH')       || define('CRAFT_VENDOR_PATH',       CRAFT_BASE_PATH.'app/vendor/');
defined('CRAFT_FRAMEWORK_PATH') || define('CRAFT_FRAMEWORK_PATH', CRAFT_APP_PATH.'framework/');;
defined('CRAFT_PLUGINS_PATH')      || define('CRAFT_PLUGINS_PATH',      CRAFT_BASE_PATH.'plugins/');
defined('CRAFT_STORAGE_PATH')      || define('CRAFT_STORAGE_PATH',      CRAFT_BASE_PATH.'storage/');
defined('CRAFT_TEMPLATES_PATH')    || define('CRAFT_TEMPLATES_PATH',    CRAFT_BASE_PATH.'templates/');
defined('YII_TRACE_LEVEL')          || define('YII_TRACE_LEVEL', 3);
// Load Yii
require_once CRAFT_APP_PATH.'framework/yiit.php';
Yii::$enableIncludePath = false;
require_once CRAFT_APP_PATH.'Craft.php';
require_once CRAFT_APP_PATH.'etc/web/WebApp.php';
require_once CRAFT_APP_PATH.'Info.php';
// Craft shits itself if there is no server or request path
$_SERVER['HTTP_HOST'] = 'dev.copywheat';
// Load test database
$config = require_once CRAFT_APP_PATH.'etc/config/test.php';
$config['params']['dbConfig'] = array();

// Create app instance
$app = new \Craft\WebApp($config);
// Load plugin
require_once CRAFT_PLUGINS_PATH.'myplugin/mypluginPlugin.php';

I'm still having issues with the db connection. Have had to resort to modifying the craft/app/etc/config/defaults.db in order to override the environment file which does work for me at the moment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.