Skip to content
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

Sync, blog, part 2 #624

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Binary file modified blog-api/tests/Support/Data/database.db
Binary file not shown.
3 changes: 2 additions & 1 deletion blog/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@
"yiisoft/yii-runner-http": "^2.0",
"yiisoft/yii-sentry": "^2.0.1",
"yiisoft/yii-swagger": "^2.1.1",
"yiisoft/yii-view-renderer": "^7.1"
"yiisoft/yii-view-renderer": "^7.1",
"zircote/swagger-php": "^4.0"
},
"require-dev": {
"codeception/c3": "^2.6",
Expand Down
10 changes: 9 additions & 1 deletion blog/src/Auth/Form/LoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
use App\Auth\AuthService;
use Yiisoft\FormModel\FormModel;
use Yiisoft\Translator\TranslatorInterface;
use Yiisoft\Validator\PropertyTranslator\ArrayPropertyTranslator;
use Yiisoft\Validator\PropertyTranslatorInterface;
use Yiisoft\Validator\PropertyTranslatorProviderInterface;
use Yiisoft\Validator\Result;
use Yiisoft\Validator\Rule\Callback;
use Yiisoft\Validator\Rule\Required;
use Yiisoft\Validator\RulesProviderInterface;

final class LoginForm extends FormModel implements RulesProviderInterface
final class LoginForm extends FormModel implements RulesProviderInterface, PropertyTranslatorProviderInterface
{
private string $login = '';
private string $password = '';
Expand Down Expand Up @@ -64,4 +67,9 @@ private function passwordRules(): array
),
];
}

public function getPropertyTranslator(): ?PropertyTranslatorInterface
{
return new ArrayPropertyTranslator($this->getPropertyLabels());
}
}
10 changes: 9 additions & 1 deletion blog/src/Auth/Form/SignupForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
use App\User\UserRepository;
use Yiisoft\FormModel\FormModel;
use Yiisoft\Translator\TranslatorInterface;
use Yiisoft\Validator\PropertyTranslator\ArrayPropertyTranslator;
use Yiisoft\Validator\PropertyTranslatorInterface;
use Yiisoft\Validator\PropertyTranslatorProviderInterface;
use Yiisoft\Validator\Result;
use Yiisoft\Validator\Rule\Equal;
use Yiisoft\Validator\Rule\Length;
use Yiisoft\Validator\Rule\Required;
use Yiisoft\Validator\RulesProviderInterface;

final class SignupForm extends FormModel implements RulesProviderInterface
final class SignupForm extends FormModel implements RulesProviderInterface, PropertyTranslatorProviderInterface
{
private string $login = '';
private string $password = '';
Expand Down Expand Up @@ -85,4 +88,9 @@ function ($value): Result {
],
];
}

public function getPropertyTranslator(): ?PropertyTranslatorInterface
{
return new ArrayPropertyTranslator($this->getPropertyLabels());
}
}
21 changes: 8 additions & 13 deletions blog/src/Contact/ContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\FormModel\FormHydrator;
use Yiisoft\Http\Header;
use Yiisoft\Http\Method;
use Yiisoft\Http\Status;
use Yiisoft\Router\UrlGeneratorInterface;
use Yiisoft\Yii\View\Renderer\ViewRenderer;
Expand All @@ -29,21 +28,17 @@ public function __construct(

public function contact(
FormHydrator $formHydrator,
ServerRequestInterface $request
ServerRequestInterface $request,
): ResponseInterface {
$body = $request->getParsedBody();
$form = new ContactForm();
if (($request->getMethod() === Method::POST)
&& $formHydrator->populate($form, $body)
&& $form->isValid()
) {
$this->mailer->send($form, $request);

return $this->responseFactory
->createResponse(Status::FOUND)
->withHeader(Header::LOCATION, $this->url->generate('site/contact'));
if (!$formHydrator->populateFromPostAndValidate($form, $request)) {
return $this->viewRenderer->render('form', ['form' => $form]);
}

return $this->viewRenderer->render('form', ['form' => $form]);
$this->mailer->send($form);

return $this->responseFactory
->createResponse(Status::FOUND)
->withHeader(Header::LOCATION, $this->url->generate('site/contact'));
}
}
10 changes: 9 additions & 1 deletion blog/src/Contact/ContactForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

use Yiisoft\FormModel\FormModel;
use Yiisoft\Input\Http\Attribute\Parameter\UploadedFiles;
use Yiisoft\Validator\PropertyTranslator\ArrayPropertyTranslator;
use Yiisoft\Validator\PropertyTranslatorInterface;
use Yiisoft\Validator\PropertyTranslatorProviderInterface;
use Yiisoft\Validator\Rule\Email;
use Yiisoft\Validator\Rule\Required;
use Yiisoft\Validator\RulesProviderInterface;

final class ContactForm extends FormModel implements RulesProviderInterface
final class ContactForm extends FormModel implements RulesProviderInterface, PropertyTranslatorProviderInterface
{
private string $name = '';
private string $email = '';
Expand Down Expand Up @@ -44,4 +47,9 @@ public function getRules(): array
'body' => [new Required()],
];
}

public function getPropertyTranslator(): ?PropertyTranslatorInterface
{
return new ArrayPropertyTranslator($this->getPropertyLabels());
}
}
4 changes: 2 additions & 2 deletions blog/tests/Acceptance/LoginAcceptanceCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function testLoginEmptyDataTest(AcceptanceTester $I): void
$I->click('Submit', '#loginForm');

$I->expectTo('see validations errors.');
$I->see('Value cannot be blank');
$I->see('Value cannot be blank');
$I->see('Login cannot be blank.');
$I->see('Password cannot be blank.');
$I->seeElement('button', ['name' => 'login-button']);
}

Expand Down
7 changes: 4 additions & 3 deletions blog/tests/Acceptance/SignupAcceptanceCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ public function testRegisterEmptyData(AcceptanceTester $I): void
$I->click('Submit', '#signupForm');

$I->expectTo('see registration register validation.');
$I->see('Value cannot be blank');
$I->see('Value cannot be blank');
$I->see('Value cannot be blank');
$I->see('Login cannot be blank.');
$I->see('Password cannot be blank.');
$I->see('Password must contain at least 8 characters.');
$I->see('Confirm password cannot be blank.');
$I->seeElement('button', ['name' => 'register-button']);
}

Expand Down
8 changes: 6 additions & 2 deletions blog/tests/Functional/ContactCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public function submitEmptyForm(FunctionalTester $I)
{
$I->submitForm('#form-contact', []);
$I->expectTo('see validations errors');
$I->see('Value cannot be blank.');
$I->see('Name cannot be blank.');
$I->see('Email cannot be blank.');
$I->see('Email is not a valid email address.');
$I->see('Subject cannot be blank.');
$I->see('Body cannot be blank.');
}

public function submitFormWithIncorrectEmail(FunctionalTester $I)
Expand All @@ -37,7 +41,7 @@ public function submitFormWithIncorrectEmail(FunctionalTester $I)
'ContactForm[verifyCode]' => 'testme',
]);
$I->expectTo('see that email address is wrong');
$I->see('This value is not a valid email address.');
$I->see('Email is not a valid email address.');
}

public function submitFormSuccessfully(FunctionalTester $I)
Expand Down
Loading