class ProcessOutputCallbackTest
@covers \Drupal\package_manager\ProcessOutputCallback @group package_manager
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\package_manager\Unit\ProcessOutputCallbackTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of ProcessOutputCallbackTest
File
-
core/
modules/ package_manager/ tests/ src/ Unit/ ProcessOutputCallbackTest.php, line 16
Namespace
Drupal\Tests\package_manager\UnitView source
class ProcessOutputCallbackTest extends UnitTestCase {
/**
* Tests what happens when the output buffer has invalid JSON.
*/
public function testInvalidJson() : void {
$callback = new ProcessOutputCallback();
$callback(OutputTypeEnum::OUT, '{A string of invalid JSON! 😈');
$this->expectException(\JsonException::class);
$this->expectExceptionMessage('Syntax error');
$callback->parseJsonOutput();
}
/**
* Tests what happens when there is error output only.
*/
public function testErrorOutputOnly() : void {
$callback = new ProcessOutputCallback();
$logger = new TestLogger();
$callback->setLogger($logger);
$error_text = 'What happened?';
$callback(OutputTypeEnum::ERR, $error_text);
$this->assertSame([
$error_text,
], $callback->getErrorOutput());
// The error should not yet be logged.
$this->assertEmpty($logger->records);
// There should be no output data, but calling getOutput() should log the
// error.
$this->assertSame([], $callback->getOutput());
$this->assertNull($callback->parseJsonOutput());
$this->assertTrue($logger->hasWarning($error_text));
// Resetting the callback should clear the error buffer but the log should
// still have the error from before.
$callback->reset();
$this->assertTrue($logger->hasWarning($error_text));
}
/**
* Tests the full lifecycle of a ProcessOutputCallback object.
*/
public function testCallback() : void {
$callback = new ProcessOutputCallback();
$logger = new TestLogger();
$callback->setLogger($logger);
// The buffers should initially be empty, and nothing should be logged.
$this->assertSame([], $callback->getOutput());
$this->assertSame([], $callback->getErrorOutput());
$this->assertNull($callback->parseJsonOutput());
$this->assertEmpty($logger->records);
// Send valid JSON data to the callback, one line at a time.
$data = [
'value' => 'I have value!',
'another value' => 'I have another value!',
'one' => 1,
];
$json = json_encode($data, JSON_PRETTY_PRINT);
// Ensure the JSON is a multi-line string.
$this->assertGreaterThan(1, substr_count($json, "\n"));
$expected_output = [];
foreach (explode("\n", $json) as $line) {
$callback(OutputTypeEnum::OUT, "{$line}\n");
$expected_output[] = "{$line}\n";
}
$this->assertSame($expected_output, $callback->getOutput());
// Ensure that parseJsonOutput() can parse the data without errors.
$this->assertSame($data, $callback->parseJsonOutput());
$this->assertSame([], $callback->getErrorOutput());
$this->assertEmpty($logger->records);
// If we send error output, it should be logged, but we should still be able
// to get the data we already sent.
$callback(OutputTypeEnum::ERR, 'Oh no, what happened?');
$callback(OutputTypeEnum::ERR, 'Really what happened?!');
$this->assertSame($data, $callback->parseJsonOutput());
$expected_error = [
'Oh no, what happened?',
'Really what happened?!',
];
$this->assertSame($expected_error, $callback->getErrorOutput());
$this->assertTrue($logger->hasWarning('Oh no, what happened?Really what happened?!'));
// Send more output and error data to the callback; they should be appended
// to the data we previously sent.
$callback(OutputTypeEnum::OUT, '{}');
$expected_output[] = '{}';
$callback(OutputTypeEnum::ERR, 'new Error 1!');
$callback(OutputTypeEnum::ERR, 'new Error 2!');
$expected_error[] = 'new Error 1!';
$expected_error[] = 'new Error 2!';
// The output buffer will no longer be valid JSON, so don't try to parse it.
$this->assertSame($expected_output, $callback->getOutput());
$this->assertSame($expected_error, $callback->getErrorOutput());
$this->assertTrue($logger->hasWarning(implode('', $expected_error)));
// The previously logged error output should still be there.
$this->assertTrue($logger->hasWarning('Oh no, what happened?Really what happened?!'));
// Clear all stored output and errors.
$callback->reset();
$this->assertSame([], $callback->getOutput());
$this->assertSame([], $callback->getErrorOutput());
$this->assertNull($callback->parseJsonOutput());
// Send more output and error data.
$callback(OutputTypeEnum::OUT, 'Bonjour!');
$callback(OutputTypeEnum::ERR, 'You continue to annoy me.');
// We should now only see the stuff we just sent...
$this->assertSame([
'Bonjour!',
], $callback->getOutput());
$this->assertSame([
'You continue to annoy me.',
], $callback->getErrorOutput());
$this->assertTrue($logger->hasWarning('You continue to annoy me.'));
// ...but the previously logged errors should still be there.
$this->assertTrue($logger->hasWarning('Oh no, what happened?Really what happened?!new Error 1!new Error 2!'));
$this->assertTrue($logger->hasWarning('Oh no, what happened?Really what happened?!'));
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overrides |
---|---|---|---|---|
ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
ExpectDeprecationTrait::getCallableName | private static | function | Returns a callable as a string suitable for inclusion in a message. | |
ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | |
ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | |
ProcessOutputCallbackTest::testCallback | public | function | Tests the full lifecycle of a ProcessOutputCallback object. | |
ProcessOutputCallbackTest::testErrorOutputOnly | public | function | Tests what happens when there is error output only. | |
ProcessOutputCallbackTest::testInvalidJson | public | function | Tests what happens when the output buffer has invalid JSON. | |
RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |
RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |
RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |
RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
UnitTestCase::$root | protected | property | The app root. | |
UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |
UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase::setUp | protected | function | 358 | |
UnitTestCase::setUpBeforeClass | public static | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.