Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"phpstan/phpstan": "^2.1.30",
"phpstan/phpstan-phpunit": "^2.0.7",
"phpunit/phpunit": "^12.5",
"symfony/framework-bundle": "^7.4|^8"
"symfony/framework-bundle": "^7.4|^8",
"twig/twig": "^3.22"
},
"license": "MIT",
"autoload": {
Expand Down
3 changes: 3 additions & 0 deletions config/help/MakeDocument.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The <info>%command.name%</info> command creates or updates a document and repository class.

<info>php %command.full_name% BlogPost</info>
5 changes: 5 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
failOnAllIssues="true"
displayDetailsOnAllIssues="true"
>
<php>
<ini name="error_reporting" value="-1" />
<env name="MONGODB_URI" value="mongodb://localhost:27017" />
</php>

<testsuites>
<testsuite name="Doctrine MongoDB Maker Bundle Test Suite">
<directory>./tests/</directory>
Expand Down
20 changes: 17 additions & 3 deletions src/Maker/MakeDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@
use Symfony\Bundle\MakerBundle\InputAwareMakerInterface;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;

use function dirname;
use function file_get_contents;
use function sprintf;

final class MakeDocument extends AbstractMaker implements InputAwareMakerInterface
{
public static function getCommandName(): string
{
return 'doctrine:mongodb:make:document';
return 'make:document';
}

public static function getCommandDescription(): string
Expand All @@ -27,7 +33,9 @@ public static function getCommandDescription(): string

public function configureCommand(Command $command, InputConfiguration $inputConfig): void
{
// TODO: Implement configureCommand() method.
$command
->addArgument('name', InputArgument::OPTIONAL, sprintf('Class name of the entity to create or update (e.g. <fg=yellow>%s</>)', Str::asClassName(Str::getRandomTerm())))
->setHelp(file_get_contents(dirname(__DIR__, 2) . '/config/help/MakeDocument.txt'));
}

public function configureDependencies(DependencyBuilder $dependencies, InputInterface|null $input = null): void
Expand All @@ -37,6 +45,12 @@ public function configureDependencies(DependencyBuilder $dependencies, InputInte

public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
// TODO: Implement generate() method.
$entityClassDetails = $generator->createClassNameDetails(
$input->getArgument('name'),
'Document\\'
);


$generator->writeChanges();
}
}
130 changes: 130 additions & 0 deletions tests/Maker/MakeDocumentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Doctrine\Bundle\MongoDBMakerBundle\Tests\Maker;

use Doctrine\Bundle\MongoDBMakerBundle\Maker\MakeDocument;
use Generator;
use Symfony\Bundle\MakerBundle\Test\MakerTestCase;
use Symfony\Bundle\MakerBundle\Test\MakerTestDetails;
use Symfony\Bundle\MakerBundle\Test\MakerTestRunner;
use Symfony\Component\HttpKernel\KernelInterface;

use function getenv;
use function sprintf;
use function strpos;
use function substr;

class MakeDocumentTest extends MakerTestCase
{
protected function getMakerClass(): string
{
return MakeDocument::class;
}

private static function createMakeDocumentTest(bool $withDatabase = true): MakerTestDetails
{
return self::buildMakerTest()
->preRun(static function (MakerTestRunner $runner) use ($withDatabase): void {
// @todo We have to re-install the bundles to get the recipe-contrib applied
// https://github.com/symfony/flex/issues/1076
// https://github.com/symfony/recipes/pull/1509
$runner->runProcess('composer remove --dev doctrine/mongodb-maker-bundle doctrine/mongodb-odm-bundle --no-scripts');
$runner->runProcess('composer config extra.symfony.allow-contrib true');
$runner->runProcess('composer require --dev doctrine/mongodb-maker-bundle doctrine/mongodb-odm-bundle');

if (! $withDatabase) {
return;
}

$runner->replaceInFile(
'.env',
'mongodb://localhost:27017',
getenv('MONGODB_URI'),
);

self::runDocumentTest($runner);
});
}

protected function createKernel(): KernelInterface
{
return new MakerTestKernel('dev', true);
}

public static function getTestDetails(): Generator
{
yield 'it_creates_a_new_class_basic' => [
self::createMakeDocumentTest()
->run(static function (MakerTestRunner $runner): void {
$runner->runMaker([
// document class name
'User',
// add not additional fields

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add no additional fields I'm guessing?

'',
]);

self::runDocumentTest($runner, ['firstName' => 'Dev']);
}),
];
}

/** @param array<string, mixed> $data */
private static function runDocumentTest(MakerTestRunner $runner, array $data = []): void
{
$runner->renderTemplateFile(
'make-document/GeneratedDocumentTest.php.twig',
'tests/GeneratedDocumentTest.php',
['data' => $data],
);

//$runner->updateSchema();
$runner->runTests();
}

private static function runCustomTest(MakerTestRunner $runner, string $filename, bool $withDatabase = true): void
{
$runner->copy(
'make-document/tests/' . $filename,
'tests/GeneratedDocumentTest.php',
);

if ($withDatabase) {
$runner->updateSchema();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does updateSchema actually do in a MongoDB context?

}

$runner->runTests();
}

private static function copyDocument(MakerTestRunner $runner, string $filename): void
{
$documentClassName = substr(
$filename,
0,
strpos($filename, '-'),
);

$runner->copy(
sprintf('make-document/documents/attributes/%s', $filename),
sprintf('src/Document/%s.php', $documentClassName),
);
}

private static function copyDocumentDirectory(MakerTestRunner $runner, string $directory): void
{
$runner->copy(
sprintf('make-document/%s/attributes', $directory),
'',
);
}
}
38 changes: 38 additions & 0 deletions tests/Maker/MakerTestKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MongoDBMakerBundle\Tests\Maker;

use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
use Doctrine\Bundle\MongoDBMakerBundle\MongoDBMakerBundle;
use Symfony\Bundle\MakerBundle\Test\MakerTestKernel as MakerBundleTestKernel;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class MakerTestKernel extends MakerBundleTestKernel
{
public function registerBundles(): iterable
{
yield from parent::registerBundles();
yield new DoctrineMongoDBBundle();
yield new MongoDBMakerBundle();
}

public function registerContainerConfiguration(LoaderInterface $loader): void
{
parent::registerContainerConfiguration($loader);

$loader->load(static function (ContainerBuilder $container): void {
$container->loadFromExtension('doctrine_mongodb', [
'default_connection' => 'default',
'connections' => [
'default' => ['server' => 'mongodb://localhost:27017'],
],
'document_managers' => [
'default' => ['auto_mapping' => true],
],
]);
});
}
}
59 changes: 59 additions & 0 deletions tests/TestKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Doctrine\Bundle\MongoDBMakerBundle\Tests;

use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
use Doctrine\Bundle\MongoDBMakerBundle\MongoDBMakerBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass\MakeCommandRegistrationPass;
use Symfony\Bundle\MakerBundle\MakerBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;

class TestKernel extends Kernel implements CompilerPassInterface
{
use MicroKernelTrait;

public function registerBundles(): iterable
{
return [
new FrameworkBundle(),
new DoctrineMongoDBBundle(),
new MakerBundle(),
new MongoDBMakerBundle(),
];
}

public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(static function (ContainerBuilder $container): void {
$container->loadFromExtension('framework', ['secret' => 'S0ME_SECRET']);
$container->loadFromExtension('doctrine_mongodb', [
'default_connection' => 'default',
'connections' => [
'default' => ['server' => 'mongodb://localhost:27017'],
],
'document_managers' => [
'default' => ['auto_mapping' => true],
],
]);
$container->loadFromExtension('maker', []);
$container->loadFromExtension('mongodb_maker', ['generate_final_documents' => true]);
});
}

public function process(ContainerBuilder $container): void
{
/**
* Makes all makers public to help the tests
* @see \Symfony\Bundle\MakerBundle\Test\MakerTestKernel::process()
*/
foreach ($container->findTaggedServiceIds(MakeCommandRegistrationPass::MAKER_TAG) as $id => $tags) {
$defn = $container->getDefinition($id);
$defn->setPublic(true);
}
}
}
35 changes: 35 additions & 0 deletions tests/fixtures/make-document/GeneratedDocumentTest.php.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Tests;

use App\Document\User;
use Doctrine\ODM\MongoDB\DocumentManager;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class GeneratedDocumentTest extends KernelTestCase
{
public function testGeneratedDocument()
{
self::bootKernel();
/** @var DocumentManager $dm */
$dm = self::$kernel->getContainer()
->get('doctrine_mongodb')
->getManager();

$dm->createQueryBuilder(User::class)
->remove()
->getQuery()
->execute();

$user = new User();
{% for field, value in data %}
$user->{{ field }} = '{{ value }}';
{% endfor %}
$dm->persist($user);
$dm->flush();

$actualUser = $dm->getRepository(User::class)->findAll();

$this->assertcount(1, $actualUser);
}
}
15 changes: 15 additions & 0 deletions tests/fixtures/make-document/documents/User-basic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

#[ODM\Document]
class User
{
#[ODM\Id]
public ?int $id = null;

#[ODM\Field]
public ?string $firstName = null;
}