Open Journal Systems  3.3.0
phpunit-bootstrap.php
1 <?php
11 // This script may not be executed remotely.
12 if (isset($_SERVER['SERVER_NAME'])) {
13  die('This script can only be executed from the command-line.');
14 }
15 
16 
17 // Configure the index file location, assume that pkp-lib is
18 // included within a PKP application.
19 // FIXME: This doesn't work if lib/pkp is symlinked.
20 // realpath($_['SCRIPT_FILENAME'].'/../../index.php') could work
21 // but see http://bugs.php.net/bug.php?id=50366
22 define('INDEX_FILE_LOCATION', dirname(dirname(dirname(dirname(__FILE__)))).'/index.php');
23 chdir(dirname(INDEX_FILE_LOCATION));
24 
25 // Configure PKP error handling for tests
26 define('DONT_DIE_ON_ERROR', true);
27 
28 // Don't support sessions
29 define('SESSION_DISABLE_INIT', true);
30 
31 // Configure assertions for tests
32 ini_set('assert.active', true);
33 ini_set('assert.bail', false);
34 ini_set('assert.warning', true);
35 ini_set('assert.callback', null);
36 ini_set('assert.quiet_eval', false);
37 
38 // NB: Our test framework provides the possibility to
39 // import mock classes to replace regular classes.
40 // This is necessary to mock static method calls.
41 // Unfortunately we can only define one mock environment
42 // per test run as PHP does not allow to change a class
43 // implementation while running.
44 // We therefore need to define the mock environment globally
45 // so that tests can check their environment requirement
46 // before they start importing.
47 if (isset($_SERVER['PKP_MOCK_ENV'])) {
48  define('PHPUNIT_CURRENT_MOCK_ENV', $_SERVER['PKP_MOCK_ENV']);
49  $mockEnvs = '';
50  foreach(array('lib/pkp/tests/mock/', 'tests/mock/') as $testDir) {
51  $normalizedMockEnv = normalizeMockEnvironment($testDir . $_SERVER['PKP_MOCK_ENV']);
52  if ($normalizedMockEnv) {
53  if (!empty($mockEnvs)) $mockEnvs .= ';';
54  $mockEnvs .= $normalizedMockEnv;
55  }
56  }
57  define('PHPUNIT_ADDITIONAL_INCLUDE_DIRS', $mockEnvs);
58 } else {
59  // Use the current test folder as mock environment
60  // if no environment has been explicitly set.
61  // The phpunit cli tool's last parameter is the test class, file or directory
62  define('PHPUNIT_CURRENT_MOCK_ENV', '__NONE__');
63  assert(is_array($_SERVER['argv']) and count($_SERVER['argv'])>1);
64  $testDir = end($_SERVER['argv']);
65  define('PHPUNIT_ADDITIONAL_INCLUDE_DIRS', normalizeMockEnvironment($testDir));
66 }
67 
76 function require_mock_env($mockEnv) {
77  if (PHPUNIT_CURRENT_MOCK_ENV == '__NONE__' || PHPUNIT_CURRENT_MOCK_ENV != $mockEnv) {
78  // Tests that require different mock environments cannot run
79  // in the same test batch as this would require re-defining
80  // already defined classes.
81  debug_print_backtrace();
82  die(
83  'You are trying to run a test in the wrong mock environment ('
84  . PHPUNIT_CURRENT_MOCK_ENV . ' rather than ' . $mockEnv.')!'
85  );
86  }
87 }
88 
98 function import($class) {
99  static $mockEnvArray = null;
100 
101  // Expand and verify additional include directories.
102  if (is_null($mockEnvArray)) {
103  if (defined('PHPUNIT_ADDITIONAL_INCLUDE_DIRS')) {
104  $mockEnvArray = explode(';', PHPUNIT_ADDITIONAL_INCLUDE_DIRS);
105  foreach($mockEnvArray as $mockEnv) {
106  if (!is_dir($mockEnv)) die ('Invalid mock environment directory ' . $mockEnv . '!');
107  }
108  } else {
109  $mockEnvArray = array();
110  }
111  }
112 
113  // Test whether we have a mock implementation of
114  // the requested class.
115  foreach($mockEnvArray as $mockEnv) {
116  $classParts = explode('.', $class);
117  $mockClassFile = $mockEnv . '/Mock'.array_pop($classParts) . '.inc.php';
118  if (file_exists($mockClassFile)) {
119  require_once($mockClassFile);
120  return;
121  }
122  }
123 
124  // No mock implementation found, do the normal import
125  require_once('./'.str_replace('.', '/', $class) . '.inc.php');
126 }
127 
136 function normalizeMockEnvironment($mockEnv) {
137  if (substr($mockEnv, 0, 1) != '/') {
138  $mockEnv = getcwd() . '/' . $mockEnv;
139  }
140  if (!is_dir($mockEnv)) {
141  $mockEnv = dirname($mockEnv);
142  }
143  $mockEnv = realpath($mockEnv);
144 
145  // Test whether this is a valid directory.
146  if (is_dir($mockEnv)) {
147  return $mockEnv;
148  } else {
149  // Make sure that we do not try to
150  // identify a mock env again but mark
151  // it as "not found".
152  return false;
153  }
154 }
155 
156 // Set up minimal PKP application environment
157 require_once('./lib/pkp/includes/bootstrap.inc.php');
158 
159 // Make sure ADOdb doesn't "clean up" our /tmp folder.
160 $ADODB_CACHE_DIR = CacheManager::getFileCachePath() . DIRECTORY_SEPARATOR . '_db';
161 
162 // Remove the PKP error handler so that PHPUnit
163 // can set its own error handler and catch errors for us.
164 error_reporting(E_ALL & ~E_STRICT & ~E_DEPRECATED);
165 
166 // Show errors in the UI
167 ini_set('display_errors', true);
CacheManager\getFileCachePath
static getFileCachePath()
Definition: CacheManager.inc.php:112
INDEX_FILE_LOCATION
const INDEX_FILE_LOCATION
Definition: index.php:64
Seboettg\Collection\count
count()
Definition: ArrayListTrait.php:253