From 80bca3d98d667461f335788824bee55e6d4188f1 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Fri, 26 Nov 2021 23:40:36 +0300 Subject: [PATCH] Adapt to DI changes (#46) --- src/Command/BaseGenerateCommand.php | 2 +- src/Controller/DefaultController.php | 6 +++--- src/Generator/AbstractGenerator.php | 5 +++-- src/Generator/Controller/Generator.php | 12 ++++++------ tests/Generators/ControllerGeneratorTest.php | 2 +- tests/TestCase.php | 12 ++++++------ 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/Command/BaseGenerateCommand.php b/src/Command/BaseGenerateCommand.php index d9c08d18b..2de91eeb9 100644 --- a/src/Command/BaseGenerateCommand.php +++ b/src/Command/BaseGenerateCommand.php @@ -46,7 +46,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $generator = $this->getGenerator(); $generator->load(array_filter(array_merge($input->getOptions(), $input->getArguments()))); $output->writeln("Running '{$generator->getName()}'...\n"); - if ($generator->validate() && !$generator->hasErrors()) { + if ($generator->validate()->isValid() && !$generator->hasErrors()) { $this->generateCode($generator, $input, $output); } else { $this->displayValidationErrors($generator, $output); diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index ded2f2699..50f6db833 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -54,7 +54,7 @@ public function view(ServerRequestInterface $request): string $answers = $request->getAttribute('answers'); if ($preview !== null || $generate !== null) { - if ($generator->validate()) { + if ($generator->validate()->isValid()) { $generator->saveStickyAttributes(); $files = $generator->generate(); if ($generate !== null && !empty($answers)) { @@ -81,7 +81,7 @@ public function preview(ServerRequestInterface $request) $id = $request->getAttribute('id'); $file = $request->getAttribute('file'); $generator = $this->loadGenerator($id, $request); - if ($generator->validate()) { + if ($generator->validate()->isValid()) { foreach ($generator->generate() as $f) { if ($f->getId() === $file) { $content = $f->preview(); @@ -109,7 +109,7 @@ public function diff(ServerRequestInterface $request) $id = $request->getAttribute('id'); $file = $request->getAttribute('file'); $generator = $this->loadGenerator($id, $request); - if ($generator->validate()) { + if ($generator->validate()->isValid()) { foreach ($generator->generate() as $f) { if ($f->getId() === $file) { return $this->render( diff --git a/src/Generator/AbstractGenerator.php b/src/Generator/AbstractGenerator.php index a5ab1e343..c1f7e1af1 100644 --- a/src/Generator/AbstractGenerator.php +++ b/src/Generator/AbstractGenerator.php @@ -174,6 +174,7 @@ public function getDescription(): string final public function validate(): ResultSet { + /** @psalm-suppress PossiblyInvalidArgument */ $results = (new Validator())->validate($this, $this->rules()); foreach ($results as $attribute => $resultItem) { if (!$resultItem->isValid()) { @@ -197,8 +198,8 @@ public function rules(): array { return [ 'template' => [ - (new Required())->message('A code template must be selected.'), - (new Callback([$this, 'validateTemplate'])), + Required::rule()->message('A code template must be selected.'), + Callback::rule([$this, 'validateTemplate']), ], ]; } diff --git a/src/Generator/Controller/Generator.php b/src/Generator/Controller/Generator.php index 60a372742..6b3edd847 100644 --- a/src/Generator/Controller/Generator.php +++ b/src/Generator/Controller/Generator.php @@ -52,20 +52,20 @@ public function rules(): array parent::rules(), [ 'controllerClass' => [ - new Required(), - (new MatchRegularExpression('/^[A-Z][\w]*Controller$/')) + Required::rule(), + MatchRegularExpression::rule('/^[A-Z][\w]*Controller$/') ->message( 'Only word characters are allowed, and the class name must start with a capital letter and end with "Controller".' ), - (new Callback([$this, 'validateNewClass'])), + Callback::rule([$this, 'validateNewClass']), ], 'baseClass' => [ - new Required(), - (new MatchRegularExpression('/^[\w\\\\]*$/')) + Required::rule(), + MatchRegularExpression::rule('/^[\w\\\\]*$/') ->message('Only word characters and backslashes are allowed.'), ], 'actions' => [ - (new MatchRegularExpression('/^[a-z][a-z0-9\\-,\\s]*$/')) + MatchRegularExpression::rule('/^[a-z][a-z0-9\\-,\\s]*$/') ->message('Only a-z, 0-9, dashes (-), spaces and commas are allowed.'), ], ] diff --git a/tests/Generators/ControllerGeneratorTest.php b/tests/Generators/ControllerGeneratorTest.php index 985a88876..135285079 100644 --- a/tests/Generators/ControllerGeneratorTest.php +++ b/tests/Generators/ControllerGeneratorTest.php @@ -83,7 +83,7 @@ public function testCustomTemplate(): void $this->assertFalse( $generator->hasErrors(), - implode("\n", array_values($validationResult->getResult('template')->getErrors())) + implode("\n", $validationResult->getResult('template')->getErrors()) ); $this->assertContainsOnlyInstancesOf(CodeFile::class, $generator->generate()); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 70ef0d7c9..d6277ab29 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -11,11 +11,11 @@ use Psr\Log\NullLogger; use Yiisoft\Aliases\Aliases; use Yiisoft\Di\Container; +use Yiisoft\Di\ContainerConfig; use Yiisoft\EventDispatcher\Dispatcher\Dispatcher; use Yiisoft\EventDispatcher\Provider\Provider; use Yiisoft\Files\FileHelper; use Yiisoft\View\View; -use Yiisoft\View\WebView; /** * GiiTestCase is the base class for all gii related test cases @@ -28,8 +28,9 @@ protected function setUp(): void { parent::setUp(); FileHelper::ensureDirectory(__DIR__ . '/runtime'); - $this->container = new Container( - [ + + $config = ContainerConfig::create() + ->withDefinitions([ \Yiisoft\Yii\Gii\GiiInterface::class => new \Yiisoft\Yii\Gii\Factory\GiiFactory( [ 'controller' => \Yiisoft\Yii\Gii\Generator\Controller\Generator::class, @@ -47,13 +48,12 @@ protected function setUp(): void ListenerProviderInterface::class => Provider::class, LoggerInterface::class => NullLogger::class, View::class => [ - 'class' => WebView::class, '__construct()' => [ 'basePath' => '@views', ], ], - ] - ); + ]); + $this->container = new Container($config); } protected function tearDown(): void