Skip to content

Commit 4f41d47

Browse files
committed
wip
1 parent 3f7324e commit 4f41d47

File tree

7 files changed

+214
-0
lines changed

7 files changed

+214
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Tempest\Upgrade\Tests;
4+
5+
use Closure;
6+
use PHPUnit\Framework\Assert;
7+
8+
final class RectorTester
9+
{
10+
private(set) string $fixturePath;
11+
private(set) string $actual;
12+
13+
public function __construct(
14+
private readonly string $configPath,
15+
) {}
16+
17+
public function runFixture(string $fixturePath): self
18+
{
19+
$clone = clone $this;
20+
21+
$clone->fixturePath = $fixturePath;
22+
$clone->actual = $this->getActual($fixturePath);
23+
24+
return $clone;
25+
}
26+
27+
/**
28+
* @param Closure(string $actual): void $test
29+
*/
30+
public function assert(Closure $test): self
31+
{
32+
$test($this->actual);
33+
34+
return $this;
35+
}
36+
37+
public function assertMatchesExpected(): self
38+
{
39+
$expected = file_get_contents(str_replace('.input.php', '.expected.php', $this->fixturePath));
40+
$expected = preg_replace('/^<\?php\n/', '', $expected);
41+
$expected = trim($expected);
42+
43+
Assert::assertSame($expected, $this->actual);
44+
45+
return $this;
46+
}
47+
48+
public function assertContains(string $needle): self
49+
{
50+
Assert::assertStringContainsString($needle, $this->actual);
51+
52+
return $this;
53+
}
54+
55+
private function getActual(string $fixturePath): string
56+
{
57+
$command = "vendor/bin/rector process {$fixturePath} --config {$this->configPath} --dry-run --output-format=json";
58+
59+
$output = json_decode(shell_exec($command), associative: true);
60+
61+
return $this->cleanDiff($output['file_diffs'][0]['diff'] ?? '');
62+
}
63+
64+
private function cleanDiff(string $diff): string
65+
{
66+
$diff = preg_replace('/^\+\+\+.*\n/m', '', $diff);
67+
$diff = preg_replace('/^@@.*?@@\n/m', '', $diff);
68+
$diff = preg_replace('/\\\\ No newline at end of file\n/', '', $diff);
69+
70+
$diff = preg_replace('/^\s/m', '', $diff);
71+
72+
$diff = preg_replace('/^-.*\n/m', '', $diff);
73+
$diff = preg_replace('/^\+/m', '', $diff);
74+
75+
return trim($diff);
76+
}
77+
78+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Tempest\Upgrade\Tests\Tempest2\Fixtures;
4+
5+
use Tempest\Database\QueryStatement;
6+
use Tempest\Database\QueryStatements\CreateTableStatement;
7+
use Tempest\Database\QueryStatements\DropTableStatement;
8+
9+
final class MigrateUpAndDownMigration implements \Tempest\Database\MigratesUp, \Tempest\Database\MigratesDown
10+
{
11+
public string $name {
12+
get => '00-00-0000';
13+
}
14+
15+
public function up(): QueryStatement
16+
{
17+
return new CreateTableStatement('table')
18+
->primary()
19+
->datetime('createdAt');
20+
}
21+
22+
public function down(): QueryStatement
23+
{
24+
return new DropTableStatement('table');
25+
}
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Tempest\Upgrade\Tests\Tempest2\Fixtures;
4+
5+
use Tempest\Database\DatabaseMigration;
6+
use Tempest\Database\QueryStatement;
7+
use Tempest\Database\QueryStatements\CreateTableStatement;
8+
use Tempest\Database\QueryStatements\DropTableStatement;
9+
10+
final class MigrateUpAndDownMigration implements DatabaseMigration
11+
{
12+
public string $name {
13+
get => '00-00-0000';
14+
}
15+
16+
public function up(): ?QueryStatement
17+
{
18+
return new CreateTableStatement('table')
19+
->primary()
20+
->datetime('createdAt');
21+
}
22+
23+
public function down(): ?QueryStatement
24+
{
25+
return new DropTableStatement('table');
26+
}
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Tempest\Upgrade\Tests\Tempest2\Fixtures;
4+
5+
use Tempest\Database\QueryStatement;
6+
use Tempest\Database\QueryStatements\CreateTableStatement;
7+
8+
final class MigrateUpMigration implements \Tempest\Database\MigratesUp
9+
{
10+
public string $name {
11+
get => '00-00-0000';
12+
}
13+
14+
public function up(): QueryStatement
15+
{
16+
return new CreateTableStatement('table')
17+
->primary()
18+
->datetime('createdAt');
19+
}
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Tempest\Upgrade\Tests\Tempest2\Fixtures;
4+
5+
use Tempest\Database\DatabaseMigration;
6+
use Tempest\Database\QueryStatement;
7+
use Tempest\Database\QueryStatements\CreateTableStatement;
8+
9+
final class MigrateUpMigration implements DatabaseMigration
10+
{
11+
public string $name {
12+
get => '00-00-0000';
13+
}
14+
15+
public function up(): ?QueryStatement
16+
{
17+
return new CreateTableStatement('table')
18+
->primary()
19+
->datetime('createdAt');
20+
}
21+
22+
public function down(): ?QueryStatement
23+
{
24+
return null;
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Tempest\Upgrade\Tests\Tempest2;
4+
5+
use Tempest\Generation\Tests\TestCase;
6+
use Tempest\Upgrade\Tests\RectorTester;
7+
8+
final class Tempest2RectorTest extends TestCase
9+
{
10+
private RectorTester $rector {
11+
get => new RectorTester(__DIR__ . '/tempest2_rector.php');
12+
}
13+
14+
public function test_migration_with_only_up(): void
15+
{
16+
$this->rector
17+
->runFixture(__DIR__ . '/Fixtures/MigrateUpMigration.input.php')
18+
->assertMatchesExpected();
19+
}
20+
21+
public function test_migration_with_up_and_down(): void
22+
{
23+
$this->rector
24+
->runFixture(__DIR__ . '/Fixtures/MigrateUpAndDownMigration.input.php')
25+
->assertContains('implements \Tempest\Database\MigratesUp, \Tempest\Database\MigratesDown')
26+
->assertContains('return new DropTableStatement(\'table\')')
27+
->assertContains('public function down(): QueryStatement');
28+
}
29+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
use Rector\Config\RectorConfig;
4+
5+
return RectorConfig::configure()
6+
->withSets([
7+
__DIR__ . '/../../src/tempest2.php',
8+
]);

0 commit comments

Comments
 (0)