Skip to content

Commit

Permalink
Merge b1bed83 into 3aa490b
Browse files Browse the repository at this point in the history
  • Loading branch information
hypeJunction committed Nov 8, 2016
2 parents 3aa490b + b1bed83 commit 49c9ae6
Show file tree
Hide file tree
Showing 16 changed files with 239 additions and 18 deletions.
20 changes: 20 additions & 0 deletions .scripts/is_memcached_enabled.php
@@ -0,0 +1,20 @@
<?php

if (php_sapi_name() !== "cli") {
die('CLI only');
}

if (!class_exists('Memcache')) {
fwrite(STDOUT, 'PHP memcache module not installed');
exit(1);
}

$memcached = new Memcache;
if (!$memcached->connect('127.0.0.1', 11211)) {
fwrite(STDOUT, 'Failed to connect to memcache server');
exit(2);
}

$memcached->close();
fwrite(STDOUT, 'Successfully connected to memcache server');
exit(0);
7 changes: 7 additions & 0 deletions .scripts/travis/elgg-config/core.php
@@ -0,0 +1,7 @@
<?php

global $CONFIG;

if (!isset($CONFIG)) {
$CONFIG = new \stdClass;
}
14 changes: 14 additions & 0 deletions .scripts/travis/elgg-config/core_memcached.php
@@ -0,0 +1,14 @@
<?php

global $CONFIG;

if (!isset($CONFIG)) {
$CONFIG = new \stdClass;
}

// Memcached configuration for Travis
$CONFIG->memcache = true;
$CONFIG->memcache_servers = [
['127.0.0.1', 11211],
];
$CONFIG->memcache_namespace_prefix = 'elgg_';
11 changes: 11 additions & 0 deletions .scripts/travis/elgg-config/simpletest.php
@@ -0,0 +1,11 @@
<?php

global $CONFIG;

if (!isset($CONFIG)) {
$CONFIG = new \stdClass;
}

$CONFIG->debug = 'NOTICE';


18 changes: 18 additions & 0 deletions .scripts/travis/elgg-config/simpletest_memcached.php
@@ -0,0 +1,18 @@
<?php

global $CONFIG;

if (!isset($CONFIG)) {
$CONFIG = new \stdClass;
}

$CONFIG->debug = 'NOTICE';

// Memcached configuration for Travis
$CONFIG->memcache = true;
$CONFIG->memcache_servers = [
['127.0.0.1', 11211],
];
$CONFIG->memcache_namespace_prefix = 'elgg_';


2 changes: 2 additions & 0 deletions .scripts/travis/memcached.ini
@@ -0,0 +1,2 @@
extension = "memcache.so"
extension = "memcached.so"
25 changes: 25 additions & 0 deletions .scripts/travis/phpunit-memcached.xml
@@ -0,0 +1,25 @@
<phpunit bootstrap="../../vendor/autoload.php">
<testsuites>
<testsuite name="core_memcached">
<directory>../../engine/tests/phpunit/</directory>
<directory>../../engine/classes/</directory>
</testsuite>
</testsuites>
<filter>
<blacklist>
<directory suffix=".php">/engine/tests</directory>
<directory suffix=".php">/engine/lib/upgrades</directory>
<file>/engine/settings.example.php</file>
<directory suffix=".php">/vendor</directory>
<directory suffix=".php">/vendors</directory>
<directory suffix=".php">/docs</directory>
</blacklist>
</filter>
<listeners>
<listener class="\Elgg\TestListener">
<arguments>
<string>./.scripts/travis/elgg-config/</string>
</arguments>
</listener>
</listeners>
</phpunit>
33 changes: 25 additions & 8 deletions .travis.yml
Expand Up @@ -26,6 +26,7 @@ matrix:
fast_finish: true
allow_failures:
- php: hhvm

include:
# Lint checks for PHP code and composer.json
- php: 5.6
Expand Down Expand Up @@ -66,6 +67,22 @@ matrix:
- sphinx-intl build --locale-dir=docs/locale/
- sphinx-build -b html -D language=es -n docs docs/_build/html

# Memcached enabled
- php: 5.6
services:
- memcached
- mysql
env: VARIA=true
before_install:
- phpenv config-rm xdebug.ini
- phpenv config-add ./.scripts/travis/memcached.ini
install:
- composer travis:install-with-mysql
script:
- php -f ./.scripts/is_memcached_enabled.php
- ./vendor/bin/phpunit --configuration ./.scripts/travis/phpunit-memcached.xml
- php ./engine/tests/suite.php --config ./.scripts/travis/elgg-config/simpletest_memcached.php

# End to end tests
- php: 5.6
env: E2E=true
Expand All @@ -80,19 +97,19 @@ matrix:
- ./vendor/bin/phpunit
- php -f ./engine/tests/suite.php

services:
- mysql
services:
- mysql

before_install:
- phpenv config-rm xdebug.ini
- composer config -g github-oauth.github.com ${GITHUB_TOKEN}
- phpenv config-rm xdebug.ini
- composer config -g github-oauth.github.com ${GITHUB_TOKEN}

install: composer travis:install-with-mysql

script:
- ./vendor/bin/phpunit
- php -f ./engine/tests/suite.php
- ./vendor/bin/phpunit
- php -f ./engine/tests/suite.php

notifications:
email:
secure: exC/ws07lLOj3Y43C89jiaKpyB8Yt7DPGSCShV4R3Wkw/hVVzjxt1BinPxzsyL5DC7APUMcTHGOhDB2oCE4ynDE6o6L9bH79fc+V8IYAiNaEIGL0AOuHdnRdGN9GMrr2jv78cZ5MctuUTkeYLaoOEyDGHmkMhqa6SufIDAY8b58=
Expand Down
2 changes: 1 addition & 1 deletion engine/classes/Elgg/Ajax/Service.php
Expand Up @@ -206,7 +206,7 @@ private function buildHttpResponse(AjaxResponse $api_response, $allow_removing_h
if ($ttl > 0) {
// Required to remove headers set by PHP session
if (!isset($allow_removing_headers)) {
$allow_removing_headers = !defined('PHPUNIT_ELGG_TESTING_APPLICATION');
$allow_removing_headers = !elgg()->isTestingApplication();
}

if ($allow_removing_headers) {
Expand Down
25 changes: 24 additions & 1 deletion engine/classes/Elgg/Application.php
Expand Up @@ -34,6 +34,11 @@ class Application {
*/
private $engine_dir;

/**
* @var bool
*/
private static $testing_app;

/**
* Property names of the service provider to be exposed via __get()
*
Expand Down Expand Up @@ -237,7 +242,7 @@ public function bootCore() {

$config = $this->services->config;

if (defined('PHPUNIT_ELGG_TESTING_APPLICATION')) {
if ($this->isTestingApplication()) {
throw new \RuntimeException('Unit tests should not call ' . __METHOD__);
}

Expand Down Expand Up @@ -598,4 +603,22 @@ private function allowPathRewrite() {
$this->services->setValue('request', $new);
_elgg_set_initial_context($new);
}

/**
* Flag this application as running for testing (PHPUnit)
*
* @param bool $testing Is testing application
* @return void
*/
public static function setTestingApplication($testing = true) {
self::$testing_app = $testing;
}

/**
* Checks if the application is running in PHPUnit
* @return bool
*/
public static function isTestingApplication() {
return (bool) self::$testing_app;
}
}
2 changes: 1 addition & 1 deletion engine/classes/Elgg/BootService.php
Expand Up @@ -233,7 +233,7 @@ private function getBootData(\stdClass $CONFIG, Database $db) {
* @return \Stash\Interfaces\ItemInterface
*/
private function getStashItem(\stdClass $CONFIG, $site_guid) {
if (!empty($CONFIG->memcache)) {
if (!empty($CONFIG->memcache) && class_exists('Memcache')) {
$options = [];
if (!empty($CONFIG->memcache_servers)) {
$options['servers'] = $CONFIG->memcache_servers;
Expand Down
25 changes: 22 additions & 3 deletions engine/tests/phpunit/Elgg/TestCase.php
Expand Up @@ -27,7 +27,7 @@ abstract class TestCase extends PHPUnit_Framework_TestCase {

/**
* Constructs a test case with the given name.
* Boostraps testing environment
* Bootstraps testing environment
*
* @param string $name
* @param array $data
Expand All @@ -51,7 +51,7 @@ public static function getInstance() {
}

/**
* Boostraps test suite
* Bootstraps test suite
*
* @global stdClass $CONFIG Global config
* @global stdClass $_ELGG Global vars
Expand Down Expand Up @@ -79,6 +79,7 @@ public static function bootstrap() {
$sp->config->getCookieConfig();

$app = new Application($sp);
Application::setTestingApplication(true);
Application::$_instance = $app;

// loadCore bails on repeated calls, so we need to manually inject this to make
Expand All @@ -88,6 +89,10 @@ public static function bootstrap() {

_elgg_filestore_boot();

// Invalidate memcache
_elgg_get_memcache('new_entity_cache')->clear();
_elgg_get_memcache('metastrings_memcache')->clear();

self::$_mocks = null; // reset mocking service
}

Expand All @@ -96,7 +101,13 @@ public static function bootstrap() {
* @return array
*/
public static function getTestingConfigArray() {
return [
global $CONFIG;

if (!isset($CONFIG)) {
$CONFIG = new \stdClass;
}

$conf = [
'Config_file' => false,
'dbprefix' => 'elgg_t_i_',
'boot_complete' => false,
Expand Down Expand Up @@ -128,6 +139,14 @@ public static function getTestingConfigArray() {
'site',
],
];

foreach ($conf as $key => $val) {
if (!isset($CONFIG->$key)) {
$CONFIG->$key = $val;
}
}

return (array) $CONFIG;
}

/**
Expand Down
41 changes: 41 additions & 0 deletions engine/tests/phpunit/Elgg/TestListener.php
@@ -0,0 +1,41 @@
<?php

namespace Elgg;

use PHPUnit_Framework_BaseTestListener;
use PHPUnit_Framework_TestSuite;

class TestListener extends PHPUnit_Framework_BaseTestListener {

/**
* @var string
*/
private $config_dir;

/**
* Constructor
*
* @param string $config_dir Directory containing suite-speicific config files
*/
public function __construct($config_dir) {
$this->config_dir = $config_dir;
}

/**
* {@inheritdoc}
*/
public function startTestSuite(PHPUnit_Framework_TestSuite $suite) {

// reset config before each suite
global $CONFIG;
unset($CONFIG);

$name = $suite->getName();
$dir = rtrim($this->config_dir, '/');

if (file_exists("$dir/$name.php")) {
require_once "$dir/$name.php";
}
}

}
8 changes: 8 additions & 0 deletions engine/tests/phpunit/elgg-config/core.php
@@ -0,0 +1,8 @@
<?php

/**
* Configuration specific to a "core" test suite
*/

global $CONFIG;

14 changes: 12 additions & 2 deletions engine/tests/suite.php
Expand Up @@ -24,13 +24,22 @@
if (!TextReporter::inCli()) {
elgg_admin_gatekeeper();
} else {

$cli_opts = getopt('', [
'config::', // path to config file
]);

if (!empty($cli_opts['config']) && file_exists($cli_opts['config'])) {
// File with custom config options for the suite
require_once $cli_opts['config'];
echo "Loaded custom configuration for the suite.\n";
}

$admin = array_shift(elgg_get_admins(array('limit' => 1)));
if (!login($admin)) {
echo "Failed to login as administrator.";
exit(1);
}

global $CONFIG;

// activate plugins that are not activated on install
foreach ($plugins as $key => $id) {
Expand All @@ -42,6 +51,7 @@
$plugin->activate();
}

global $CONFIG;
$CONFIG->debug = 'NOTICE';
}

Expand Down

0 comments on commit 49c9ae6

Please sign in to comment.