TestCoverageTest.php in Drupal 9
File
core/modules/jsonapi/tests/src/Functional/TestCoverageTest.php
View source
<?php
namespace Drupal\Tests\jsonapi\Functional;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Extension\ExtensionLifecycle;
use Drupal\Tests\BrowserTestBase;
class TestCoverageTest extends BrowserTestBase {
protected $definitions;
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$all_modules = \Drupal::service('extension.list.module')
->getList();
$stable_core_modules = array_filter($all_modules, function ($module) {
return $module->origin === 'core' && empty($module->info['hidden']) && $module->status == FALSE && $module->info['package'] !== 'Testing' && $module->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER] !== ExtensionLifecycle::EXPERIMENTAL;
});
$this->container
->get('module_installer')
->install(array_keys($stable_core_modules));
$this
->rebuildContainer();
$this->definitions = $this->container
->get('entity_type.manager')
->getDefinitions();
$this->definitions = array_filter($this->definitions, function (EntityTypeInterface $entity_type) {
return !$entity_type
->isInternal();
});
}
public function testEntityTypeRestTestCoverage() {
$problems = [];
foreach ($this->definitions as $entity_type_id => $info) {
$class_name_full = $info
->getClass();
$parts = explode('\\', $class_name_full);
$class_name = end($parts);
$module_name = $parts[1];
$possible_paths = [
'Drupal\\Tests\\jsonapi\\Functional\\CLASSTest',
'\\Drupal\\Tests\\' . $module_name . '\\Functional\\Jsonapi\\CLASSTest',
];
foreach ($possible_paths as $path) {
$missing_tests = [];
$class = str_replace('CLASS', $class_name, $path);
if (class_exists($class)) {
continue 2;
}
$missing_tests[] = $class;
}
if (!empty($missing_tests)) {
$missing_tests_list = implode(', ', $missing_tests);
$problems[] = "{$entity_type_id}: {$class_name} ({$class_name_full}) (expected tests: {$missing_tests_list})";
}
}
$all = count($this->definitions);
$good = $all - count($problems);
$this
->assertSame([], $problems, $this
->getLlamaMessage($good, $all));
}
protected function getLlamaMessage($g, $a) {
return "\n☼\n _________________________\n / Hi! \\\n | It's llame to not have |\n | complete JSON:API tests! |\n | |\n | Progress: {$g}/{$a}. |\n | _________________________/\n |/\n// o\nl'>\nll\nllama\n|| ||\n'' ''\n";
}
}
Classes
Name |
Description |
TestCoverageTest |
Checks that all core content/config entity types have JSON:API test coverage. |