3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter;
5 use Drupal\drupalmoduleupgrader\ConverterBase;
6 use Drupal\drupalmoduleupgrader\TargetInterface;
7 use Pharborist\DocCommentNode;
8 use Pharborist\RootNode;
9 use Pharborist\WhitespaceNode;
14 * description = @Translation("Modifies unit test classes.")
17 class UnitTests extends ConverterBase {
22 public function isExecutable(TargetInterface $target) {
23 return $target->getIndexer('class')->getQuery()->condition('parent', 'DrupalUnitTestCase')->countQuery()->execute();
29 public function convert(TargetInterface $target) {
31 $test_files = $target->getIndexer('class')->getQuery(['file'])->condition('parent', 'DrupalUnitTestCase')->execute()->fetchCol();
32 foreach ($test_files as $test_file) {
33 /** @var \Pharborist\Objects\Classnode[] $tests */
34 $tests = $target->open($test_file)->find(Filter::isInstanceOf('\Pharborist\Objects\SingleInheritanceNode'))->toArray();
35 foreach ($tests as $test) {
36 if ((string) $test->getExtends() === 'DrupalUnitTestCase') {
37 $unit_tests[] = $test;
42 /** @var \Pharborist\Objects\ClassNode $unit_test */
43 foreach ($unit_tests as $unit_test) {
44 $unit_test->setExtends('\Drupal\Tests\UnitTestCase');
46 $comment_text = <<<END
48 Unit tests are now written for the PHPUnit framework. You will need to refactor
49 this test in order for it to work properly.
51 $comment = DocCommentNode::create($comment_text);
52 $unit_test->setDocComment($comment);
54 $ns = 'Drupal\Tests\\' . $target->id() . '\Unit';
55 $doc = RootNode::create($ns)->getNamespace($ns)->append($unit_test->remove());
56 WhitespaceNode::create("\n\n")->insertBefore($unit_test);
58 $this->write($target, 'tests/src/Unit/' . $unit_test->getName() . '.php', "<?php\n\n$doc");