Skip to content

Commit

Permalink
Merge pull request #27 from wmde/division-by-zero
Browse files Browse the repository at this point in the history
Fix devision by zero PHP notice
  • Loading branch information
moiikana committed Apr 1, 2019
2 parents 14e8dba + 9280213 commit e4b1697
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/UseCases/ValidateMembershipFee/ValidateFeeResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class ValidateFeeResult {

public const ERROR_TOO_LOW = 'error-too-low';
public const ERROR_INTERVAL_INVALID = 'error-interval-invalid';

private $errorCode;

Expand All @@ -18,6 +19,10 @@ public static function newTooLowResponse(): self {
return self::newErrorResponse( self::ERROR_TOO_LOW );
}

public static function newIntervalInvalidResponse(): self {
return self::newErrorResponse( self::ERROR_INTERVAL_INVALID );
}

private static function newErrorResponse( string $errorCode ): self {
$result = new self();
$result->errorCode = $errorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function validate( ValidateFeeRequest $request ): ValidateFeeResult {
$this->paymentIntervalInMonths = $request->getPaymentIntervalInMonths();
$this->applicantType = $request->getApplicantType();

if ( $this->paymentIntervalInMonths < 1 ) {
return ValidateFeeResult::newIntervalInvalidResponse();
}

if ( $this->getYearlyPaymentAmount() < $this->getYearlyPaymentRequirement() ) {
return ValidateFeeResult::newTooLowResponse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,30 @@ public function testGivenInvalidCompanyAmount_validationFails(): void {
);
}

public function testGivenInvalidInterval_zero_validationFails() {
$useCase = $this->newUseCase();

$response = $useCase->validate(
ValidateFeeRequest::newInstance()
->withFee( Euro::newFromString( '12.34' ) )
->withApplicantType( ValidateFeeRequest::PERSON_APPLICANT )
->withInterval( 0 )
);

$this->assertSame( ValidateFeeResult::ERROR_INTERVAL_INVALID, $response->getErrorCode() );
}

public function testGivenInvalidInterval_negative_validationFails() {
$useCase = $this->newUseCase();

$response = $useCase->validate(
ValidateFeeRequest::newInstance()
->withFee( Euro::newFromString( '12.34' ) )
->withApplicantType( ValidateFeeRequest::PERSON_APPLICANT )
->withInterval( -1 )
);

$this->assertSame( ValidateFeeResult::ERROR_INTERVAL_INVALID, $response->getErrorCode() );
}

}

0 comments on commit e4b1697

Please sign in to comment.