-
Notifications
You must be signed in to change notification settings - Fork 1
PHPORM-439 Initialize the make:document command
#3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GromNaN
wants to merge
2
commits into
doctrine:0.1.x
Choose a base branch
from
GromNaN:PHPORM-439
base: 0.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| '', | ||
| ]); | ||
|
|
||
| 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(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does |
||
| } | ||
|
|
||
| $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), | ||
| '', | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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], | ||
| ], | ||
| ]); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
35
tests/fixtures/make-document/GeneratedDocumentTest.php.twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add no additional fieldsI'm guessing?