Skip to content

Commit

Permalink
Merge pull request #194 from wmde/validate_amount
Browse files Browse the repository at this point in the history
Validate amount
  • Loading branch information
JeroenDeDauw committed Feb 24, 2016
2 parents 70c5152 + c321890 commit 46ce1a0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
21 changes: 21 additions & 0 deletions app/routes.php
Expand Up @@ -25,6 +25,7 @@
use WMDE\Fundraising\Frontend\UseCases\CancelDonation\CancelDonationRequest;
use WMDE\Fundraising\Frontend\UseCases\ListComments\CommentListingRequest;
use WMDE\Fundraising\Frontend\UseCases\AddSubscription\SubscriptionRequest;
use WMDE\Fundraising\Frontend\Validation\ConstraintViolation;

$app->get(
'validate-email',
Expand All @@ -37,6 +38,26 @@ function( Request $request ) use ( $app, $ffFactory ) {
}
);

$app->post(
'validate-amount',
function( Request $request ) use ( $app, $ffFactory ) {

$amount = (float) $ffFactory->newDecimalNumberFormatter()->parse( $request->get( 'amount', 0 ) );
$amountValidator = $ffFactory->newAmountValidator();
$validationResult = $amountValidator->validate( $amount, (string) $request->get( 'paymentType', '' ) );

if ( $validationResult->isSuccessful() ) {
return $app->json( [ 'status' => 'OK' ] );
}
else {
$errors = array_map( function ( ConstraintViolation $constraintViolation ) {
return $constraintViolation->getMessage();
}, $validationResult->getViolations() );
return $app->json( [ 'status' => 'ERR', 'message' => implode( "\n", $errors ) ] );
}
}
);

$app->get(
'list-comments.json',
function( Request $request ) use ( $app, $ffFactory ) {
Expand Down
7 changes: 6 additions & 1 deletion src/FunFunFactory.php
Expand Up @@ -19,6 +19,7 @@
use Monolog\Handler\BufferHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use NumberFormatter;
use Psr\Log\LoggerInterface;
use Swift_MailTransport;
use Symfony\Component\Translation\TranslatorInterface;
Expand Down Expand Up @@ -622,7 +623,7 @@ public function getDonationRepository(): DonationRepository {
return $this->pimple['donation_repository'];
}

private function newAmountValidator(): AmountValidator {
public function newAmountValidator(): AmountValidator {
return new AmountValidator(
0.01,
[
Expand All @@ -632,6 +633,10 @@ private function newAmountValidator(): AmountValidator {
);
}

public function newDecimalNumberFormatter(): NumberFormatter {
return new NumberFormatter( $this->config['locale'], NumberFormatter::DECIMAL );
}

public function newAddCommentUseCase(): AddCommentUseCase {
return new AddCommentUseCase(
$this->getCommentRepository()
Expand Down
43 changes: 43 additions & 0 deletions tests/System/Routes/ValidateAmountRouteTest.php
@@ -0,0 +1,43 @@
<?php

declare(strict_types = 1);

namespace WMDE\Fundraising\Frontend\Tests\System\Routes;

use WMDE\Fundraising\Frontend\Tests\System\WebRouteTestCase;
use WMDE\Fundraising\Frontend\Domain\Model\PaymentType;

/**
* @licence GNU GPL v2+
* @author Gabriel Birke < gabriel.birke@wikimedia.de >
*/
class ValidateAmountRouteTest extends WebRouteTestCase {

public function testGivenValidAmount_successResponseIsReturned() {
$client = $this->createClient();

$client->request(
'POST',
'/validate-amount',
[ 'amount' => '23', 'paymentType' => PaymentType::BANK_TRANSFER ]
);

$this->assertJsonSuccessResponse(
[ 'status' => 'OK' ],
$client->getResponse()
);
}

public function testGivenInvalidAmount_failureResponseIsReturned() {
$client = $this->createClient();

$client->request(
'POST',
'/validate-amount',
[ 'amount' => '-1', 'paymentType' => PaymentType::BANK_TRANSFER ]
);

$this->assertErrorJsonResponse( $client->getResponse() );
}

}

0 comments on commit 46ce1a0

Please sign in to comment.