UpdateManagerTest.php in Lightning Core 8.4
File
tests/src/Kernel/UpdateManagerTest.php
View source
<?php
namespace Drupal\Tests\lightning_core\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\lightning_core\UpdateManager;
class UpdateManagerTest extends KernelTestBase {
protected static $modules = [
'lightning_core',
'system',
'user',
];
private $updateManager;
protected function setUp() {
parent::setUp();
$this->updateManager = new class($this->container
->get('container.namespaces'), $this->container
->get('class_resolver'), $this->container
->get('config.factory'), $this->container
->get('extension.list.module')) extends UpdateManager {
public $discovery;
public function getTasks($handler) {
yield from parent::getTasks($handler);
}
};
}
public function testGetAvailable() {
$discovery = $this
->prophesize('\\Drupal\\Component\\Plugin\\Discovery\\DiscoveryInterface');
$discovery
->getDefinitions()
->willReturn([
'fubar:1.2.1' => [
'id' => '1.2.1',
'provider' => 'fubar',
],
'fubar:1.2.2' => [
'id' => '1.2.2',
'provider' => 'fubar',
],
'fubar:1.2.3' => [
'id' => '1.2.3',
'provider' => 'fubar',
],
]);
$this->updateManager->discovery = $discovery
->reveal();
$this->container
->get('config.factory')
->getEditable(UpdateManager::CONFIG_NAME)
->set('fubar', '1.2.2')
->save();
$definitions = $this->updateManager
->getAvailable();
$this
->assertCount(1, $definitions);
$this
->assertArrayHasKey('fubar:1.2.3', $definitions);
}
public function testGetTasks() {
$handler = new TestUpdate();
$this
->assertFalse($handler->invoked);
$tasks = $this->updateManager
->getTasks($handler);
$this
->assertInstanceOf('Generator', $tasks);
$this
->assertTrue($tasks
->valid());
$this
->assertInstanceOf('\\Drupal\\lightning_core\\UpdateTask', $tasks
->current());
$tasks
->current()
->execute($this
->prophesize('\\Symfony\\Component\\Console\\Style\\StyleInterface')
->reveal(), TRUE);
$this
->assertTrue($handler->invoked);
}
}
class TestUpdate {
public $invoked = FALSE;
public function test() {
$this->invoked = TRUE;
}
}