f6e0fb85daa2897b9f32c17e3a327c0ea9199b10
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Plugin / DMU / Converter / UnitTests.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter;
4
5 use Drupal\drupalmoduleupgrader\ConverterBase;
6 use Drupal\drupalmoduleupgrader\TargetInterface;
7 use Pharborist\DocCommentNode;
8 use Pharborist\RootNode;
9 use Pharborist\WhitespaceNode;
10
11 /**
12  * @Converter(
13  *  id = "unit_tests",
14  *  description = @Translation("Modifies unit test classes.")
15  * )
16  */
17 class UnitTests extends ConverterBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public function isExecutable(TargetInterface $target) {
23     return $target->getIndexer('class')->getQuery()->condition('parent', 'DrupalUnitTestCase')->countQuery()->execute();
24   }
25
26   /**
27    * {@inheritdoc}
28    */
29   public function convert(TargetInterface $target) {
30     $unit_tests = [];
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;
38         }
39       }
40     }
41
42     /** @var \Pharborist\Objects\ClassNode $unit_test */
43     foreach ($unit_tests as $unit_test) {
44       $unit_test->setExtends('\Drupal\Tests\UnitTestCase');
45
46       $comment_text = <<<END
47 @FIXME
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.
50 END;
51       $comment = DocCommentNode::create($comment_text);
52       $unit_test->setDocComment($comment);
53
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);
57
58       $this->write($target, 'tests/src/Unit/' . $unit_test->getName() . '.php', "<?php\n\n$doc");
59     }
60   }
61
62 }