|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * EPV :: The phpBB Forum Extension Pre Validator. |
| 5 | + * |
| 6 | + * @copyright (c) 2017 phpBB Limited <https://www.phpbb.com> |
| 7 | + * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | + * |
| 9 | + */ |
| 10 | +namespace Phpbb\Epv\Tests\Tests; |
| 11 | + |
| 12 | +use Phpbb\Epv\Files\FileInterface; |
| 13 | +use Phpbb\Epv\Files\Type\MigrationFile; |
| 14 | +use Phpbb\Epv\Output\Output; |
| 15 | +use Phpbb\Epv\Output\OutputInterface; |
| 16 | +use Phpbb\Epv\Tests\BaseTest; |
| 17 | +use Phpbb\Epv\Tests\Exception\TestException; |
| 18 | +use Phpbb\Epv\Tests\Type; |
| 19 | +use PhpParser\Error; |
| 20 | +use PhpParser\Node; |
| 21 | +use PhpParser\Node\Stmt\Class_; |
| 22 | +use PhpParser\Node\Stmt\ClassMethod; |
| 23 | +use PhpParser\ParserFactory; |
| 24 | + |
| 25 | +class epv_test_validate_revert_schema extends BaseTest |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var \PhpParser\Parser |
| 29 | + */ |
| 30 | + private $parser; |
| 31 | + |
| 32 | + /** |
| 33 | + * @param bool $debug if debug is enabled |
| 34 | + * @param OutputInterface $output |
| 35 | + * @param string $basedir |
| 36 | + * @param string $namespace |
| 37 | + * @param boolean $titania |
| 38 | + * @param string $opendir |
| 39 | + */ |
| 40 | + public function __construct($debug, OutputInterface $output, $basedir, $namespace, $titania, $opendir) |
| 41 | + { |
| 42 | + parent::__construct($debug, $output, $basedir, $namespace, $titania, $opendir); |
| 43 | + |
| 44 | + $this->fileTypeFull = Type::TYPE_MIGRATION; |
| 45 | + $this->parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @param FileInterface $file |
| 50 | + * |
| 51 | + * @throws TestException |
| 52 | + */ |
| 53 | + public function validateFile(FileInterface $file) |
| 54 | + { |
| 55 | + if (!$file instanceof MigrationFile) |
| 56 | + { |
| 57 | + throw new TestException(sprintf('This test expects a migration file, got %s (%s).', get_class($file), $file->getFilename())); |
| 58 | + } |
| 59 | + |
| 60 | + try |
| 61 | + { |
| 62 | + $nodes = $this->parser->parse($file->getFile()); |
| 63 | + |
| 64 | + if ($this->isMissingRevertSchema($nodes)) |
| 65 | + { |
| 66 | + $this->output->addMessage(Output::ERROR, sprintf('Migration file %s is missing the revert_schema() method.', $file->getSaveFilename())); |
| 67 | + } |
| 68 | + } |
| 69 | + catch (Error $e) |
| 70 | + { |
| 71 | + $this->output->addMessage(Output::FATAL, 'PHP parse error in file ' . $file->getSaveFilename() . '. Message: ' . $e->getMessage()); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param Node[] $nodes |
| 77 | + * @return bool |
| 78 | + */ |
| 79 | + protected function isMissingRevertSchema($nodes) |
| 80 | + { |
| 81 | + foreach ($nodes as $node) |
| 82 | + { |
| 83 | + if ($node instanceof Class_ && $this->hasMethod($node, 'update_schema') && !$this->hasMethod($node, 'revert_schema')) |
| 84 | + { |
| 85 | + return true; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @param Class_ $node |
| 94 | + * @param string $methodName |
| 95 | + * @return bool |
| 96 | + */ |
| 97 | + protected function hasMethod($node, $methodName) |
| 98 | + { |
| 99 | + foreach ($node->stmts as $node) |
| 100 | + { |
| 101 | + if ($node instanceof ClassMethod && $node->name === $methodName) |
| 102 | + { |
| 103 | + return true; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @return string |
| 112 | + */ |
| 113 | + public function testName() |
| 114 | + { |
| 115 | + return 'Validate presence of revert_schema()'; |
| 116 | + } |
| 117 | +} |
0 commit comments