Skip to content

Commit f926914

Browse files
authored
Merge pull request #61 from kasimi/reverse-schema
Added reverse_schema test
2 parents c62a2a3 + aae03aa commit f926914

5 files changed

Lines changed: 219 additions & 0 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
class existing_revert_schema_migration extends \phpbb\db\migration\migration
4+
{
5+
public function update_schema()
6+
{
7+
8+
}
9+
10+
public function revert_schema()
11+
{
12+
13+
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
class missing_revert_schema_migration extends \phpbb\db\migration\migration
4+
{
5+
public function update_schema()
6+
{
7+
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
class missing_update_schema_migration extends \phpbb\db\migration\migration
4+
{
5+
public function dummy()
6+
{
7+
8+
}
9+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/**
4+
*
5+
* EPV :: The phpBB Forum Extension Pre Validator.
6+
*
7+
* @copyright (c) 2017 phpBB Limited <https://www.phpbb.com>
8+
* @license GNU General Public License, version 2 (GPL-2.0)
9+
*
10+
*/
11+
12+
use Phpbb\Epv\Files\FileLoader;
13+
use Phpbb\Epv\Output\OutputInterface;
14+
use Phpbb\Epv\Tests\Mock\Output;
15+
use Phpbb\Epv\Tests\Tests\epv_test_validate_revert_schema;
16+
17+
class validate_revert_schema_test extends PHPUnit_Framework_TestCase
18+
{
19+
public static function setUpBeforeClass()
20+
{
21+
require_once('./tests/Mock/Output.php');
22+
}
23+
24+
/**
25+
* @param string $file
26+
* @param callable $configure
27+
*/
28+
private function validateFile($file, $configure) {
29+
/** @var OutputInterface $output */
30+
$output = $this->getMock('Phpbb\Epv\Output\OutputInterface');
31+
$configure($output);
32+
33+
$file_loader = new FileLoader(new Output(), false, '.', '.');
34+
$file = $file_loader->loadFile($file);
35+
36+
$tester = new epv_test_validate_revert_schema(false, $output, '/a/b/', 'epv/test', false, '/a/');
37+
$tester->validateFile($file);
38+
}
39+
40+
public function test_missing_update_schema()
41+
{
42+
$this->validateFile('tests/testFiles/migrations/missing_update_schema.php', function($output)
43+
{
44+
/** @var PHPUnit_Framework_MockObject_MockObject $output */
45+
$output->expects($this->never())
46+
->method('addMessage');
47+
});
48+
}
49+
50+
public function test_existing_revert_schema()
51+
{
52+
$this->validateFile('tests/testFiles/migrations/existing_revert_schema.php', function($output)
53+
{
54+
/** @var PHPUnit_Framework_MockObject_MockObject $output */
55+
$output->expects($this->never())
56+
->method('addMessage');
57+
});
58+
}
59+
60+
public function test_missing_revert_schema()
61+
{
62+
$this->validateFile('tests/testFiles/migrations/missing_revert_schema.php', function($output)
63+
{
64+
/** @var PHPUnit_Framework_MockObject_MockObject $output */
65+
$output->expects($this->once())
66+
->method('addMessage')
67+
->with(OutputInterface::ERROR, 'Migration file tests/testFiles/migrations/missing_revert_schema.php is missing the revert_schema() method.');
68+
});
69+
}
70+
}

0 commit comments

Comments
 (0)