Skip to content

Commit

Permalink
Issue #3118477 by mondrake, Mile23: RegistryTest, RegistryLegacyTest …
Browse files Browse the repository at this point in the history
…both define the same class, use mock instead

(cherry picked from commit 0a65cd6076dbb2579634f2b4390f1532db11d9f6)
  • Loading branch information
alexpott committed Mar 11, 2020
1 parent b1a3f0d commit 9b7c87c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 63 deletions.
35 changes: 35 additions & 0 deletions tests/Drupal/Tests/Core/Test/PhpUnitCliTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Drupal\Tests\Core\Test;

use Drupal\Tests\UnitTestCase;
use Symfony\Component\Process\Process;

/**
* @group TestSuites
* @group Test
*/
class PhpUnitCliTest extends UnitTestCase {

/**
* Ensure that the test suites are able to discover tests without incident.
*/
public function testPhpUnitListTests() {
// Generate the list of tests for all the tests the suites can discover.
// The goal here is to successfully generate the list, without any
// duplicate namespace errors or so forth. This keeps us from committing
// tests which don't break under run-tests.sh, but do break under the
// phpunit test runner tool.
$process = new Process('vendor/bin/phpunit --configuration core --verbose --list-tests');
$process->setWorkingDirectory($this->root)
->setTimeout(300)
->setIdleTimeout(300);
$process->run();
$this->assertEquals(0, $process->getExitCode(),
'COMMAND: ' . $process->getCommandLine() . "\n" .
'OUTPUT: ' . $process->getOutput() . "\n" .
'ERROR: ' . $process->getErrorOutput() . "\n"
);
}

}
57 changes: 13 additions & 44 deletions tests/Drupal/Tests/Core/Theme/RegistryLegacyTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?php

/**
* @file
* Contains \Drupal\Tests\Core\Theme\RegistryLegacyTest.
*/

namespace Drupal\Tests\Core\Theme;

use Drupal\Core\Theme\ActiveTheme;
Expand All @@ -21,9 +16,9 @@
class RegistryLegacyTest extends UnitTestCase {

/**
* The tested theme registry.
* The mocked theme registry.
*
* @var \Drupal\Tests\Core\Theme\TestRegistry
* @var \Drupal\Core\Theme\Registry|PHPUnit\Framework\MockObject\MockObject
*/
protected $registry;

Expand Down Expand Up @@ -69,13 +64,6 @@ class RegistryLegacyTest extends UnitTestCase {
*/
protected $themeManager;

/**
* The list of functions that get_defined_functions() should provide.
*
* @var array
*/
public static $functions = [];

/**
* {@inheritdoc}
*/
Expand All @@ -92,14 +80,6 @@ protected function setUp() {
$this->setupTheme();
}

/**
* {@inheritdoc}
*/
protected function tearDown() {
parent::tearDown();
static::$functions = [];
}

/**
* Tests getting legacy theme function registry data defined by a module.
*
Expand Down Expand Up @@ -152,29 +132,18 @@ public function testGetLegacyThemeFunctionRegistryForModule() {
}

protected function setupTheme() {
$this->registry = new TestRegistry($this->root, $this->cache, $this->lock, $this->moduleHandler, $this->themeHandler, $this->themeInitialization);
$this->registry = $this->getMockBuilder(Registry::class)
->setMethods(['getPath'])
->setConstructorArgs([$this->root, $this->cache, $this->lock, $this->moduleHandler, $this->themeHandler, $this->themeInitialization])
->getMock();
$this->registry->expects($this->any())
->method('getPath')
->willReturnCallback(function ($module) {
if ($module == 'theme_legacy_test') {
return 'core/modules/system/tests/modules/theme_legacy_test';
}
});
$this->registry->setThemeManager($this->themeManager);
}

}

class TestRegistry extends Registry {

protected function getPath($module) {
if ($module == 'theme_legacy_test') {
return 'core/modules/system/tests/modules/theme_legacy_test';
}
}

}

namespace Drupal\Core\Theme;

use Drupal\Tests\Core\Theme\RegistryLegacyTest;

/**
* Overrides get_defined_functions() with a configurable mock.
*/
function get_defined_functions() {
return RegistryLegacyTest::$functions ?: \get_defined_functions();
}
33 changes: 14 additions & 19 deletions tests/Drupal/Tests/Core/Theme/RegistryTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?php

/**
* @file
* Contains \Drupal\Tests\Core\Theme\RegistryTest.
*/

namespace Drupal\Tests\Core\Theme;

use Drupal\Core\Theme\ActiveTheme;
Expand All @@ -18,9 +13,9 @@
class RegistryTest extends UnitTestCase {

/**
* The tested theme registry.
* The mocked theme registry.
*
* @var \Drupal\Tests\Core\Theme\TestRegistry
* @var \Drupal\Core\Theme\Registry|PHPUnit\Framework\MockObject\MockObject
*/
protected $registry;

Expand Down Expand Up @@ -190,7 +185,7 @@ public function testPostProcessExtension($defined_functions, $hooks, $expected)
->method('getModuleList')
->willReturn([]);

$class = new \ReflectionClass(TestRegistry::class);
$class = new \ReflectionClass(Registry::class);
$reflection_method = $class->getMethod('postProcessExtension');
$reflection_method->setAccessible(TRUE);
$reflection_method->invokeArgs($this->registry, [&$hooks, $theme->reveal()]);
Expand Down Expand Up @@ -476,22 +471,22 @@ public function providerTestPostProcessExtension() {
}

protected function setupTheme() {
$this->registry = new TestRegistry($this->root, $this->cache, $this->lock, $this->moduleHandler, $this->themeHandler, $this->themeInitialization);
$this->registry = $this->getMockBuilder(Registry::class)
->setMethods(['getPath'])
->setConstructorArgs([$this->root, $this->cache, $this->lock, $this->moduleHandler, $this->themeHandler, $this->themeInitialization])
->getMock();
$this->registry->expects($this->any())
->method('getPath')
->willReturnCallback(function ($module) {
if ($module == 'theme_test') {
return 'core/modules/system/tests/modules/theme_test';
}
});
$this->registry->setThemeManager($this->themeManager);
}

}

class TestRegistry extends Registry {

protected function getPath($module) {
if ($module == 'theme_test') {
return 'core/modules/system/tests/modules/theme_test';
}
}

}

namespace Drupal\Core\Theme;

use Drupal\Tests\Core\Theme\RegistryTest;
Expand Down

0 comments on commit 9b7c87c

Please sign in to comment.