Skip to content

Commit

Permalink
Adapt to DI changes (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
samdark committed Nov 26, 2021
1 parent 4ebed1f commit 80bca3d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Command/BaseGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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();
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions src/Generator/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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']),
],
];
}
Expand Down
12 changes: 6 additions & 6 deletions src/Generator/Controller/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.'),
],
]
Expand Down
2 changes: 1 addition & 1 deletion tests/Generators/ControllerGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
12 changes: 6 additions & 6 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 80bca3d

Please sign in to comment.