143 lines
6.7 KiB
PHP
143 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace JumPHP\Ide\Tests;
|
|
|
|
use JumPHP\Ide\Async\Deferred;
|
|
use JumPHP\Ide\HttpClient\Collections;
|
|
use JumPHP\Ide\HttpClient\History;
|
|
use JumPHP\Ide\HttpClient\HttpClientCommands;
|
|
use JumPHP\Ide\HttpClient\HttpSender;
|
|
use JumPHP\Ide\HttpClient\Vault;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/** "Refresh from the API": one organised collection, from the API Platform code or from an OpenAPI description, regenerated safely. */
|
|
final class RefreshApiTest extends TestCase
|
|
{
|
|
private string $dir;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->dir = sys_get_temp_dir() . '/jumphp-refresh-' . bin2hex(random_bytes(4));
|
|
mkdir($this->dir, 0777, true);
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
exec('rm -rf ' . escapeshellarg($this->dir));
|
|
}
|
|
|
|
private function commands(string $root, bool $siteUp = false): array
|
|
{
|
|
$sender = new class ($siteUp) implements HttpSender {
|
|
public function __construct(private bool $up)
|
|
{
|
|
}
|
|
|
|
public function send(array $request): Deferred
|
|
{
|
|
return $this->up ? Deferred::resolved(['status' => 200]) : Deferred::rejected(new \RuntimeException('connection refused'));
|
|
}
|
|
|
|
public function poll(): void
|
|
{
|
|
}
|
|
|
|
public function busy(): bool
|
|
{
|
|
return false;
|
|
}
|
|
};
|
|
$commands = [];
|
|
HttpClientCommands::register(function (string $n, callable $h) use (&$commands) {
|
|
$commands[$n] = $h;
|
|
}, $root, $sender, new Vault($root, $this->dir . '/s.json'), new History($root, $this->dir . '/h'));
|
|
return $commands;
|
|
}
|
|
|
|
private function refresh(array $commands, array $p = []): array
|
|
{
|
|
$d = $commands['http.api.refresh']($p);
|
|
self::assertNull($d->error(), (string)$d->error()?->getMessage());
|
|
return $d->result();
|
|
}
|
|
|
|
public function testApiPlatformMakesOneCollectionWithAFolderPerResource(): void
|
|
{
|
|
$root = $this->dir . '/ap';
|
|
mkdir($root);
|
|
exec('cp -a ' . escapeshellarg(__DIR__ . '/fixtures/api-platform') . '/. ' . escapeshellarg($root));
|
|
$commands = $this->commands($root);
|
|
$r = $this->refresh($commands);
|
|
self::assertSame('API', $r['collection']);
|
|
self::assertSame('api-platform', $r['source']);
|
|
self::assertGreaterThan(5, $r['summary']['added']);
|
|
$paths = array_column($r['files'], 'path');
|
|
self::assertContains('Author/list.http', $paths);
|
|
self::assertContains('Livre/create.http', $paths);
|
|
// sorted: the list, then the item, then the writes
|
|
$author = (new Collections($root))->requests('API');
|
|
$seq = [];
|
|
foreach ($author as $q) {
|
|
if (str_starts_with((string)$q['path'], 'Author/') && !str_contains((string)$q['path'], '/sub-')) {
|
|
$seq[(string)$q['path']] = (int)$q['seq'];
|
|
}
|
|
}
|
|
asort($seq);
|
|
self::assertSame('Author/list.http', array_key_first($seq));
|
|
// the environment has the detected site address
|
|
$env = (new Collections($root))->environments('API');
|
|
self::assertMatchesRegularExpression('#^http://localhost(:\d+)?$#', $env['local']['baseUrl']);
|
|
self::assertFileExists("$root/http-requests/API/collection.json");
|
|
// a second refresh changes nothing
|
|
$again = $this->refresh($commands);
|
|
self::assertSame(0, $again['summary']['added'] + $again['summary']['updated'] + $again['summary']['removed'] + $again['summary']['kept']);
|
|
// an edited request is kept, the new version goes beside it; an untouched one follows the code
|
|
$mine = "# @name Mine\nPOST {{baseUrl}}/api/authors\n\n{\"name\": \"mine\"}\n";
|
|
file_put_contents("$root/http-requests/API/Author/create.http", $mine);
|
|
$author = (string)file_get_contents("$root/src/Entity/Author.php");
|
|
file_put_contents("$root/src/Entity/Author.php", \JumPHP\Ide\ApiPlatform\ApiWriter::property($author, 'name', ['apiProperty' => ['example' => 'Frank Herbert']]));
|
|
$third = $this->refresh($commands);
|
|
self::assertSame($mine, file_get_contents("$root/http-requests/API/Author/create.http"));
|
|
self::assertFileExists("$root/http-requests/API/Author/create.theirs.http");
|
|
self::assertSame(1, $third['summary']['kept']);
|
|
self::assertGreaterThanOrEqual(1, $third['summary']['updated']);
|
|
}
|
|
|
|
public function testOpenApiMakesFoldersPerTagAndRegeneratesWithTheSameRules(): void
|
|
{
|
|
$root = $this->dir . '/oa';
|
|
mkdir("$root/docs", 0777, true);
|
|
copy(__DIR__ . '/fixtures/http-client/openapi.json', "$root/docs/openapi.json");
|
|
$commands = $this->commands($root);
|
|
$r = $this->refresh($commands, ['collection' => 'Library']);
|
|
self::assertSame('docs/openapi.json', $r['source']);
|
|
self::assertSame(4, $r['summary']['added']);
|
|
$folders = array_unique(array_map(fn ($f) => explode('/', $f['path'])[0], $r['files']));
|
|
self::assertSame(['Book', 'default'], array_values($folders));
|
|
self::assertFileExists("$root/http-requests/Library/Book/" . basename($r['files'][0]['path']));
|
|
// the same description: nothing to do
|
|
self::assertSame(['added' => 0, 'updated' => 0, 'kept' => 0, 'removed' => 0, 'unchanged' => 4], $this->refresh($commands, ['collection' => 'Library'])['summary']);
|
|
// an edited request stays, a new operation is added, an operation that disappeared is removed when untouched
|
|
$edited = "$root/http-requests/Library/" . $r['files'][0]['path'];
|
|
file_put_contents($edited, (string)file_get_contents($edited) . "\n# mine\n");
|
|
$spec = json_decode((string)file_get_contents("$root/docs/openapi.json"), true);
|
|
$removed = array_key_last($spec['paths']);
|
|
unset($spec['paths'][$removed]);
|
|
$spec['paths']['/api/newthing'] = ['get' => ['tags' => ['Thing'], 'summary' => 'Lists things', 'responses' => ['200' => ['description' => 'ok']]]];
|
|
file_put_contents("$root/docs/openapi.json", json_encode($spec));
|
|
$next = $this->refresh($commands, ['collection' => 'Library']);
|
|
self::assertGreaterThanOrEqual(1, $next['summary']['added']);
|
|
self::assertGreaterThanOrEqual(1, $next['summary']['removed']);
|
|
self::assertSame(1, $next['summary']['kept']);
|
|
self::assertSame(0, $next['summary']['updated']);
|
|
self::assertStringEndsWith("# mine\n", (string)file_get_contents($edited));
|
|
}
|
|
|
|
public function testWithoutAnyDescriptionItSaysSo(): void
|
|
{
|
|
$root = $this->dir . '/none';
|
|
mkdir($root);
|
|
$d = $this->commands($root)['http.api.refresh']([]);
|
|
self::assertNotNull($d->error());
|
|
}
|
|
}
|