rob-revisited/ide/tests/RepositoryCompletionTest.php

224 lines
13 KiB
PHP

<?php
namespace JumPHP\Ide\Tests;
use JumPHP\Ide\Php\Index;
use JumPHP\Ide\Php\Intelligence;
use JumPHP\Ide\Workspace\MemoryWorkspace;
use PHPUnit\Framework\TestCase;
/** Completion on a Symfony / Doctrine repository: members inherited from vendor, generics, class_alias, a file that does not parse. */
final class RepositoryCompletionTest extends TestCase
{
private Intelligence $php;
protected function setUp(): void
{
$psr4 = "<?php\n\$vendorDir = dirname(__DIR__);\n\$baseDir = dirname(\$vendorDir);\nreturn array(\n"
. " 'Doctrine\\\\ORM\\\\' => array(\$vendorDir . '/doctrine/orm/src'),\n"
. " 'Doctrine\\\\Persistence\\\\' => array(\$vendorDir . '/doctrine/persistence/src'),\n"
. " 'Doctrine\\\\Common\\\\Collections\\\\' => array(\$vendorDir . '/doctrine/collections/src'),\n"
. " 'Doctrine\\\\Bundle\\\\DoctrineBundle\\\\' => array(\$vendorDir . '/doctrine/doctrine-bundle/src'),\n"
. " 'App\\\\' => array(\$baseDir . '/src'),\n);\n";
$ws = new MemoryWorkspace([
'composer.json' => '{"autoload": {"psr-4": {"App\\\\": "src/"}}}',
'vendor/composer/autoload_psr4.php' => $psr4,
'vendor/doctrine/collections/src/Selectable.php' => "<?php\nnamespace Doctrine\\Common\\Collections;\n\n/** @template TKey @template-covariant T */\ninterface Selectable\n{\n public function matching(object \$criteria): array;\n}\n",
'vendor/doctrine/persistence/src/ObjectRepository.php' => <<<'PHP'
<?php
namespace Doctrine\Persistence;
/** @template-covariant T of object */
interface ObjectRepository
{
/** @return object|null @phpstan-return T|null */
public function find(mixed $id);
/** @return T[] */
public function findAll(): array;
public function getClassName(): string;
}
PHP,
'vendor/doctrine/orm/src/EntityRepository.php' => <<<'PHP'
<?php
namespace Doctrine\ORM;
use Doctrine\Common\Collections\Selectable;
use Doctrine\Persistence\ObjectRepository;
/**
* @template T of object
* @template-implements Selectable<int,T>
* @template-implements ObjectRepository<T>
*/
class EntityRepository implements ObjectRepository, Selectable
{
/**
* @return object|null The entity
* @phpstan-return ?T
*/
public function find(mixed $id, int|null $lockMode = null): object|null { return null; }
/** @return list<T> */
public function findAll(): array { return []; }
/** @return list<T> */
public function findBy(array $criteria, ?array $orderBy = null): array { return []; }
/** @phpstan-return T|null */
public function findOneBy(array $criteria): object|null { return null; }
public function count(array $criteria = []): int { return 0; }
public function createQueryBuilder(string $alias): QueryBuilder { return new QueryBuilder(); }
public function matching(object $criteria): array { return []; }
public function getClassName(): string { return ''; }
protected function getEntityManager(): object { return new \stdClass(); }
}
PHP,
'vendor/doctrine/orm/src/QueryBuilder.php' => "<?php\nnamespace Doctrine\\ORM;\n\nclass QueryBuilder\n{\n public function where(string \$w): static { return \$this; }\n}\n",
// recent versions: the class every repository extends, with the entity's template, overriding find() without a doc
'vendor/doctrine/doctrine-bundle/src/Repository/ServiceEntityRepository.php' => <<<'PHP'
<?php
namespace Doctrine\Bundle\DoctrineBundle\Repository;
use Doctrine\ORM\EntityRepository;
/**
* @template T of object
* @template-extends EntityRepository<T>
*/
class ServiceEntityRepository extends EntityRepository implements ServiceEntityRepositoryInterface
{
public function find(mixed $id, int|null $lockMode = null): object|null { return null; }
}
PHP,
'vendor/doctrine/doctrine-bundle/src/Repository/ServiceEntityRepositoryInterface.php' => "<?php\nnamespace Doctrine\\Bundle\\DoctrineBundle\\Repository;\n\ninterface ServiceEntityRepositoryInterface\n{\n}\n",
// older / transitional versions: the proxy name is an alias, declared conditionally
'vendor/doctrine/doctrine-bundle/src/Repository/ServiceEntityRepositoryProxy.php' => <<<'PHP'
<?php
namespace Doctrine\Bundle\DoctrineBundle\Repository;
if (!class_exists(ServiceEntityRepositoryProxy::class, false)) {
class_alias(ServiceEntityRepository::class, ServiceEntityRepositoryProxy::class);
}
PHP,
'vendor/doctrine/doctrine-bundle/src/Repository/LazyServiceEntityRepository.php' => <<<'PHP'
<?php
namespace Doctrine\Bundle\DoctrineBundle\Repository;
use Doctrine\ORM\EntityRepository;
if (!class_exists(LazyServiceEntityRepository::class, false)) {
/**
* @template T of object
* @template-extends EntityRepository<T>
*/
class LazyServiceEntityRepository extends EntityRepository
{
}
}
PHP,
'src/Entity/Xxx.php' => "<?php\nnamespace App\\Entity;\n\nclass Xxx\n{\n private ?int \$id = null;\n private ?string \$name = null;\n public function getName(): ?string { return \$this->name; }\n}\n",
'src/Repository/XxxRepository.php' => "<?php\nnamespace App\\Repository;\n\nuse App\\Entity\\Xxx;\nuse Doctrine\\Bundle\\DoctrineBundle\\Repository\\ServiceEntityRepository;\n\n/**\n * @extends ServiceEntityRepository<Xxx>\n */\nclass XxxRepository extends ServiceEntityRepository\n{\n}\n",
'src/Repository/ProxyRepository.php' => "<?php\nnamespace App\\Repository;\n\nuse App\\Entity\\Xxx;\nuse Doctrine\\Bundle\\DoctrineBundle\\Repository\\ServiceEntityRepositoryProxy;\n\n/**\n * @extends ServiceEntityRepositoryProxy<Xxx>\n */\nclass ProxyRepository extends ServiceEntityRepositoryProxy\n{\n}\n",
'src/Repository/LazyRepository.php' => "<?php\nnamespace App\\Repository;\n\nuse App\\Entity\\Xxx;\nuse Doctrine\\Bundle\\DoctrineBundle\\Repository\\LazyServiceEntityRepository;\n\n/**\n * @extends LazyServiceEntityRepository<Xxx>\n */\nclass LazyRepository extends LazyServiceEntityRepository\n{\n}\n",
]);
$index = new Index($ws);
$index->refresh();
$this->php = new Intelligence($index);
}
/** @return list<string> */
private function labels(string $text): array
{
$offset = strpos($text, '|');
$before = substr($text, 0, $offset);
$line = substr_count($before, "\n") + 1;
$col = strlen($before) - (strrpos($before, "\n") === false ? 0 : strrpos($before, "\n") + 1) + 1;
return array_map(fn ($i) => $i['label'], $this->php->complete('src/Controller/C.php', str_replace('|', '', $text), $line, $col)['items']);
}
private function action(string $repository, string $body, string $attribute = "#[Route('/')]\n "): string
{
return "<?php\nnamespace App\\Controller;\n\nuse App\\Repository\\$repository;\nuse Symfony\\Component\\HttpFoundation\\Response;\n\n"
. "class C\n{\n {$attribute}public function index($repository \$toto): Response\n {\n $body\n }\n}\n";
}
public function testTheInheritedMembersOfAVendorRepositoryAreListed(): void
{
$labels = $this->labels($this->action('XxxRepository', '$toto->find|'));
foreach (['find', 'findAll', 'findBy', 'findOneBy', 'count', 'createQueryBuilder', 'getClassName', 'matching'] as $method) {
self::assertContains($method, $labels);
}
self::assertNotContains('getEntityManager', $labels, 'a protected method is not offered outside the class');
}
public function testItWorksWhileTheFileDoesNotParse(): void
{
// no ";", no closing brace at all
$text = "<?php\nnamespace App\\Controller;\nuse App\\Repository\\XxxRepository;\nclass C\n{\n #[Route('/')]\n public function index(XxxRepository \$toto): Response\n {\n \$toto->find|";
self::assertContains('findAll', $this->labels($text));
self::assertContains('findOneBy', $this->labels($this->action('XxxRepository', '$toto->| $x = ;')));
}
public function testAnAttributeBeforeTheActionDoesNotBreakTheContext(): void
{
self::assertContains('findAll', $this->labels($this->action('XxxRepository', '$toto->|', "#[Route('/', name: 'a')]\n #[IsGranted('ROLE_USER')]\n ")));
}
public function testGenericsGiveTheEntityToFindAndFindAll(): void
{
self::assertContains('getName', $this->labels($this->action('XxxRepository', '$toto->find(1)->|')));
self::assertContains('getName', $this->labels($this->action('XxxRepository', '$toto->findAll()[0]->|')));
self::assertContains('getName', $this->labels($this->action('XxxRepository', '$toto->findOneBy([])->|')));
self::assertContains('getName', $this->labels($this->action('XxxRepository', 'foreach ($toto->findBy([]) as $x) { $x->| }')));
self::assertContains('where', $this->labels($this->action('XxxRepository', '$toto->createQueryBuilder("x")->|')));
}
public function testMagicFindByFieldsOfTheEntity(): void
{
$labels = $this->labels($this->action('XxxRepository', '$toto->findOneBy|'));
self::assertContains('findOneByName', $labels);
self::assertContains('findByName', $labels);
self::assertContains('countByName', $labels);
}
public function testAClassAliasIsFollowed(): void
{
$labels = $this->labels($this->action('ProxyRepository', '$toto->|'));
self::assertContains('findAll', $labels);
self::assertContains('createQueryBuilder', $labels);
self::assertContains('getName', $this->labels($this->action('ProxyRepository', '$toto->find(1)->|')));
}
public function testAClassDeclaredInAnIfIsFound(): void
{
self::assertContains('findBy', $this->labels($this->action('LazyRepository', '$toto->|')));
self::assertContains('getName', $this->labels($this->action('LazyRepository', '$toto->findAll()[0]->|')));
}
/** The real vendor/ of a Symfony project, when this computer has one (read only: linked from a temporary project). */
public function testWithTheRealVendorOfASymfonyProject(): void
{
$vendor = (getenv('JUMPHP_TEST_SF_VENDOR') ?: (getenv('HOME') ?: '') . '/JumPHP-Projects/testSF/vendor');
if (!is_file("$vendor/doctrine/doctrine-bundle/src/Repository/ServiceEntityRepository.php")) {
self::markTestSkipped('no Symfony project with doctrine-bundle here');
}
$root = sys_get_temp_dir() . '/jph-repo-' . getmypid();
mkdir("$root/src/Repository", 0777, true);
mkdir("$root/src/Entity", 0777, true);
symlink($vendor, "$root/vendor");
copy(dirname($vendor) . '/composer.json', "$root/composer.json");
file_put_contents("$root/src/Entity/Xxx.php", "<?php\nnamespace App\\Entity;\n\nclass Xxx\n{\n private ?string \$name = null;\n public function getName(): ?string { return \$this->name; }\n}\n");
file_put_contents("$root/src/Repository/XxxRepository.php", "<?php\nnamespace App\\Repository;\n\nuse App\\Entity\\Xxx;\nuse Doctrine\\Bundle\\DoctrineBundle\\Repository\\ServiceEntityRepository;\n\n/**\n * @extends ServiceEntityRepository<Xxx>\n */\nclass XxxRepository extends ServiceEntityRepository\n{\n}\n");
try {
$index = new Index(new \JumPHP\Ide\Workspace\LocalWorkspace($root));
$index->refresh();
$this->php = new Intelligence($index);
$labels = $this->labels($this->action('XxxRepository', '$toto->find|'));
foreach (['find', 'findAll', 'findBy', 'findOneBy', 'count', 'createQueryBuilder'] as $method) {
self::assertContains($method, $labels);
}
self::assertContains('getName', $this->labels($this->action('XxxRepository', '$toto->find(1)->|')));
self::assertContains('getName', $this->labels($this->action('XxxRepository', '$toto->findAll()[0]->|')));
} finally {
@unlink("$root/vendor");
exec('rm -rf ' . escapeshellarg($root));
}
}
}