Skip to content

Commit

Permalink
tests: change helper scope
Browse files Browse the repository at this point in the history
This seems to be the only way to have the same helpers used between
tests in a manner that works for both standalone phpunit and
autotest.sh.

Signed-off-by: Kyle Fazzari <kyrofa@ubuntu.com>
  • Loading branch information
kyrofa committed Nov 8, 2017
1 parent 06ba1a8 commit 9c24b7b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 46 deletions.
48 changes: 25 additions & 23 deletions tests/lib/Template/CSSResourceLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,6 @@
use OC\Template\SCSSCacher;
use OC\Template\CSSResourceLocator;

function rrmdir($directory) {
$files = array_diff(scandir($directory), array('.','..'));
foreach ($files as $file) {
if (is_dir($directory . '/' . $file)) {
rrmdir($directory . '/' . $file);
} else {
unlink($directory . '/' . $file);
}
}
return rmdir($directory);
}

function randomString() {
return sha1(uniqid(mt_rand(), true));
}

class CSSResourceLocatorTest extends \Test\TestCase {
/** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
protected $appData;
Expand Down Expand Up @@ -95,6 +79,22 @@ private function cssResourceLocator() {
);
}

private function rrmdir($directory) {
$files = array_diff(scandir($directory), array('.','..'));
foreach ($files as $file) {
if (is_dir($directory . '/' . $file)) {
$this->rrmdir($directory . '/' . $file);
} else {
unlink($directory . '/' . $file);
}
}
return rmdir($directory);
}

private function randomString() {
return sha1(uniqid(mt_rand(), true));
}

public function testConstructor() {
$locator = $this->cssResourceLocator();
$this->assertAttributeEquals('theme', 'theme', $locator);
Expand All @@ -107,24 +107,24 @@ public function testConstructor() {

public function testFindWithAppPathSymlink() {
// First create new apps path, and a symlink to it
$apps_dirname = randomString();
$apps_dirname = $this->randomString();
$new_apps_path = sys_get_temp_dir() . '/' . $apps_dirname;
$new_apps_path_symlink = $new_apps_path . '_link';
mkdir($new_apps_path);
symlink($apps_dirname, $new_apps_path_symlink);

// Create an app within that path
mkdir($new_apps_path . '/' . 'test-app');
mkdir($new_apps_path . '/' . 'test-css-app');

// Use the symlink as the app path
\OC::$APPSROOTS[] = [
'path' => $new_apps_path_symlink,
'url' => '/apps-test',
'url' => '/css-apps-test',
'writable' => false,
];

$locator = $this->cssResourceLocator();
$locator->find(array('test-app/test-file'));
$locator->find(array('test-css-app/test-file'));

$resources = $locator->getResources();
$this->assertCount(1, $resources);
Expand All @@ -134,15 +134,17 @@ public function testFindWithAppPathSymlink() {
$webRoot = $resource[1];
$file = $resource[2];

$expectedRoot = $new_apps_path . '/test-app';
$expectedWebRoot = '/apps-test/test-app';
$expectedRoot = $new_apps_path . '/test-css-app';
$expectedWebRoot = \OC::$WEBROOT . '/css-apps-test/test-css-app';
$expectedFile = 'test-file.css';

$this->assertEquals($expectedRoot, $root,
'Ensure the app path symlink is resolved into the real path');
$this->assertEquals($expectedWebRoot, $webRoot);
$this->assertEquals($expectedFile, $file);

rrmdir($new_apps_path);
array_pop(\OC::$APPSROOTS);
unlink($new_apps_path_symlink);
$this->rrmdir($new_apps_path);
}
}
49 changes: 26 additions & 23 deletions tests/lib/Template/JSResourceLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,6 @@
use OCP\ILogger;
use OC\Template\JSResourceLocator;

function rrmdir($directory) {
$files = array_diff(scandir($directory), array('.','..'));
foreach ($files as $file) {
if (is_dir($directory . '/' . $file)) {
rrmdir($directory . '/' . $file);
} else {
unlink($directory . '/' . $file);
}
}
return rmdir($directory);
}

function randomString() {
return sha1(uniqid(mt_rand(), true));
}

class JSResourceLocatorTest extends \Test\TestCase {
/** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
protected $appData;
Expand Down Expand Up @@ -86,6 +70,23 @@ private function jsResourceLocator() {
);
}

private function rrmdir($directory) {
$files = array_diff(scandir($directory), array('.','..'));
foreach ($files as $file) {
if (is_dir($directory . '/' . $file)) {
$this->rrmdir($directory . '/' . $file);
} else {
unlink($directory . '/' . $file);
}
}
return rmdir($directory);
}

private function randomString() {
return sha1(uniqid(mt_rand(), true));
}


public function testConstructor() {
$locator = $this->jsResourceLocator();
$this->assertAttributeEquals('theme', 'theme', $locator);
Expand All @@ -98,24 +99,24 @@ public function testConstructor() {

public function testFindWithAppPathSymlink() {
// First create new apps path, and a symlink to it
$apps_dirname = randomString();
$apps_dirname = $this->randomString();
$new_apps_path = sys_get_temp_dir() . '/' . $apps_dirname;
$new_apps_path_symlink = $new_apps_path . '_link';
mkdir($new_apps_path);
symlink($apps_dirname, $new_apps_path_symlink);

// Create an app within that path
mkdir($new_apps_path . '/' . 'test-app');
mkdir($new_apps_path . '/' . 'test-js-app');

// Use the symlink as the app path
\OC::$APPSROOTS[] = [
'path' => $new_apps_path_symlink,
'url' => '/apps-test',
'url' => '/js-apps-test',
'writable' => false,
];

$locator = $this->jsResourceLocator();
$locator->find(array('test-app/test-file'));
$locator->find(array('test-js-app/test-file'));

$resources = $locator->getResources();
$this->assertCount(1, $resources);
Expand All @@ -125,15 +126,17 @@ public function testFindWithAppPathSymlink() {
$webRoot = $resource[1];
$file = $resource[2];

$expectedRoot = $new_apps_path . '/test-app';
$expectedWebRoot = '/apps-test/test-app';
$expectedRoot = $new_apps_path . '/test-js-app';
$expectedWebRoot = \OC::$WEBROOT . '/js-apps-test/test-js-app';
$expectedFile = 'test-file.js';

$this->assertEquals($expectedRoot, $root,
'Ensure the app path symlink is resolved into the real path');
$this->assertEquals($expectedWebRoot, $webRoot);
$this->assertEquals($expectedFile, $file);

rrmdir($new_apps_path);
array_pop(\OC::$APPSROOTS);
unlink($new_apps_path_symlink);
$this->rrmdir($new_apps_path);
}
}

0 comments on commit 9c24b7b

Please sign in to comment.