class WardenManagerTest in Warden 8
Same name and namespace in other branches
- 8.2 tests/src/Unit/Service/WardenManagerTest.php \Drupal\Tests\warden\Unit\Service\WardenManagerTest
- 3.x tests/src/Unit/Service/WardenManagerTest.php \Drupal\Tests\warden\Unit\Service\WardenManagerTest
@coversDefaultClass \Drupal\warden\Service\WardenManager @group warden
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\warden\Unit\Service\WardenManagerTest
Expanded class hierarchy of WardenManagerTest
File
- tests/
src/ Unit/ Service/ WardenManagerTest.php, line 13
Namespace
Drupal\Tests\warden\Unit\ServiceView source
class WardenManagerTest extends UnitTestCase {
/**
* @var WardenManager
*/
protected $wardenManager;
/**
* @var string
*/
protected $token;
/**
* @var \Drupal\Core\Extension\InfoParser|\PHPUnit_Framework_MockObject_MockObject
*/
protected $infoParser;
/**
* @var \Drupal\Core\Config\ConfigFactory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $configFactory;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->token = hash('sha256', 42);
$this->infoParser = $this
->getMock('Drupal\\Core\\Extension\\InfoParser', array(), array(), '', FALSE);
$this->configFactory = $this
->getMock('Drupal\\Core\\Config\\ConfigFactory', array(), array(), '', FALSE);
$this
->expectConfigFactoryAccess();
$this->wardenManager = new WardenManager($this->configFactory);
$this->wardenManager
->setBaseUrl('http://www.example.com')
->setTime(12345678)
->setInfoParser($this->infoParser)
->setThemes([])
->setModules([]);
}
/**
* Expect access to the config factory via the WardenManager constructor.
*/
protected function expectConfigFactoryAccess() {
$wardenConfig = $this
->getMock('Drupal\\Core\\Config\\Config', array(), array(), '', FALSE);
$systemConfig = $this
->getMock('Drupal\\Core\\Config\\Config', array(), array(), '', FALSE);
$this->configFactory
->expects($this
->any())
->method('get')
->will($this
->returnCallback(function ($setting_name) use ($wardenConfig, $systemConfig) {
$settings = [
'warden.settings' => $wardenConfig,
'system.site' => $systemConfig,
];
return $settings[$setting_name];
}));
$systemConfig
->expects($this
->once())
->method('get')
->with('name')
->willReturn('My Website');
$wardenConfig
->expects($this
->any())
->method('get')
->will($this
->returnCallback(function ($setting_name) {
$settings = [
'warden_preg_match_contrib' => '{^modules\\/contrib\\/*}',
'warden_preg_match_custom' => '{^modules\\/custom\\/*}',
'warden_match_custom' => TRUE,
'warden_match_contrib' => TRUE,
'warden_token' => $this->token,
'warden_server_host_path' => 'http://warden.example.com',
'warden_http_username' => '',
'warden_http_password' => '',
'warden_certificate_path' => '',
];
return $settings[$setting_name];
}));
}
/**
* @param string $name
* @param string $type
* @return Extension|\PHPUnit_Framework_MockObject_MockObject
*/
protected function getMockExtension($name, $type = 'contrib') {
/** @var Extension|\PHPUnit_Framework_MockObject_MockObject $module_b */
$extension = $this
->getMock('Drupal\\Core\\Extension\\Extension', array(), array(), '', FALSE);
$extension
->expects($this
->once())
->method('getPathname')
->willReturn("modules/{$type}/{$name}/{$name}.info.yml");
return $extension;
}
/**
* @param array $moduleNames
* A list of module names (e.g. ['module_a', 'module_b']
* The info parser will expect there to be 2 calls to parse
* and it will return an array each time of the form
* ['version' => '8.x-1.0']
*/
protected function expectContribModules(array $moduleNames) {
$results = [];
foreach ($moduleNames as $moduleName) {
$results["modules/contrib/{$moduleName}/{$moduleName}.info.yml"] = [
'version' => '8.x-1.0',
];
}
$this->infoParser
->expects($this
->exactly(count($moduleNames)))
->method('parse')
->will($this
->returnCallback(function ($info_filename) use ($results) {
return $results[$info_filename];
}));
}
/**
* Tests generating module data with no modules.
*/
public function testGenerateDataNoModules() {
$expected_data = [
'core' => [
'drupal' => [
'version' => \Drupal::VERSION,
],
],
'contrib' => [],
'custom' => [],
'url' => 'http://www.example.com',
'site_name' => 'My Website',
'key' => $this->token,
'time' => 12345678,
];
$actual_data = $this->wardenManager
->generateSiteData();
$this
->assertEquals($expected_data, $actual_data);
}
/**
* Tests generating module data with one module.
*/
public function testGenerateDataOneModule() {
$expected_data = [
'core' => [
'drupal' => [
'version' => \Drupal::VERSION,
],
],
'contrib' => [
'Module A' => [
'version' => '8.x-1.0',
],
],
'custom' => [],
'url' => 'http://www.example.com',
'site_name' => 'My Website',
'key' => $this->token,
'time' => 12345678,
];
$this->wardenManager
->setModules([
'module_a' => $this
->getMockExtension('module_a'),
]);
$this->infoParser
->expects($this
->once())
->method('parse')
->with('modules/contrib/module_a/module_a.info.yml')
->willReturn([
'project' => 'Module A',
'version' => '8.x-1.0',
]);
$actual_data = $this->wardenManager
->generateSiteData();
$this
->assertEquals($expected_data, $actual_data);
}
/**
* Tests generating module data with two modules.
*/
public function testGenerateDataTwoModules() {
$expected_data = [
'core' => [
'drupal' => [
'version' => \Drupal::VERSION,
],
],
'contrib' => [
'module_a' => [
'version' => '8.x-1.0',
],
'module_b' => [
'version' => '8.x-1.0',
],
],
'custom' => [],
'url' => 'http://www.example.com',
'site_name' => 'My Website',
'key' => $this->token,
'time' => 12345678,
];
$this->wardenManager
->setModules([
'module_a' => $this
->getMockExtension('module_a'),
'module_b' => $this
->getMockExtension('module_b'),
]);
$this
->expectContribModules([
'module_a',
'module_b',
]);
$actual_data = $this->wardenManager
->generateSiteData();
$this
->assertEquals($expected_data, $actual_data);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
WardenManagerTest:: |
protected | property | ||
WardenManagerTest:: |
protected | property | ||
WardenManagerTest:: |
protected | property | ||
WardenManagerTest:: |
protected | property | ||
WardenManagerTest:: |
protected | function | Expect access to the config factory via the WardenManager constructor. | |
WardenManagerTest:: |
protected | function | ||
WardenManagerTest:: |
protected | function | ||
WardenManagerTest:: |
protected | function |
Overrides UnitTestCase:: |
|
WardenManagerTest:: |
public | function | Tests generating module data with no modules. | |
WardenManagerTest:: |
public | function | Tests generating module data with one module. | |
WardenManagerTest:: |
public | function | Tests generating module data with two modules. |