Skip to content

Commit

Permalink
Merge pull request #146 from wmde/valuation-date-utc
Browse files Browse the repository at this point in the history
Store valuation date in UTC
  • Loading branch information
moiikana authored Oct 30, 2023
2 parents c86f4af + 26e05ee commit a1cbb1d
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace WMDE\Fundraising\PaymentContext\Domain\Model\BookingDataTransformers;

use WMDE\Euro\Euro;
use WMDE\Fundraising\PaymentContext\Domain\Model\ValuationDateTimeZone;

class CreditCardBookingTransformer {

Expand Down Expand Up @@ -32,12 +33,16 @@ class CreditCardBookingTransformer {

/**
* @param array<string, scalar> $rawBookingData
* @param \DateTimeImmutable|null $valuationDate
* @param \DateTimeImmutable|null $valuationDate This parameter exists only for testing (and passing in a fixed time). The CreditCardPayment will always omit the parameter.
*/
public function __construct( array $rawBookingData, ?\DateTimeImmutable $valuationDate = null ) {
$this->validateRawData( $rawBookingData );
$this->rawBookingData = $rawBookingData;
$this->valuationDate = $valuationDate ?? new \DateTimeImmutable();
if ( $valuationDate === null ) {
$this->valuationDate = new \DateTimeImmutable( 'now', ValuationDateTimeZone::getTimeZone() );
} else {
$this->valuationDate = $valuationDate->setTimezone( ValuationDateTimeZone::getTimeZone() );
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace WMDE\Fundraising\PaymentContext\Domain\Model\BookingDataTransformers;

use WMDE\Fundraising\PaymentContext\Domain\Model\ValuationDateTimeZone;

class PayPalBookingTransformer {

private const PAYER_ID_KEY = 'payer_id';
Expand Down Expand Up @@ -92,6 +94,7 @@ public function __construct( array $rawBookingData ) {
) );
}

$valuationDate = $valuationDate->setTimezone( ValuationDateTimeZone::getTimeZone() );
$this->valuationDate = $valuationDate;
$this->transactionId = strval( $rawBookingData[self::TRANSACTION_ID_KEY] );
$this->rawBookingData = $this->anonymise( $rawBookingData );
Expand Down
1 change: 1 addition & 0 deletions src/Domain/Model/SofortPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function bookPayment( array $transactionData, PaymentIdRepository $idGene
}
throw new DomainException( $msg );
}
$valuationDate = $valuationDate->setTimezone( ValuationDateTimeZone::getTimeZone() );
$this->valuationDate = $valuationDate;
return $this;
}
Expand Down
12 changes: 12 additions & 0 deletions src/Domain/Model/ValuationDateTimeZone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace WMDE\Fundraising\PaymentContext\Domain\Model;

class ValuationDateTimeZone {

private const TIMEZONE = 'UTC';

public static function getTimeZone(): \DateTimeZone {
return new \DateTimeZone( self::TIMEZONE );
}
}
1 change: 1 addition & 0 deletions tests/Data/PayPalPaymentBookingData.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class PayPalPaymentBookingData {

public const PAYMENT_DATE = '10:54:49 Dec 02, 2012 PST';
public const PAYMENT_DATE_UTC = '2012-12-02 18:54:49';
public const TRANSACTION_ID = 'T4242';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function testStorePayPalPayment(): void {
$this->assertSame( 9900, $insertedPayment['amount'] );
$this->assertSame( 3, $insertedPayment['payment_interval'] );
$this->assertSame( 'PPL', $insertedPayment['payment_method'] );
$this->assertSame( '2012-12-02 10:54:49', $insertedPayment['valuation_date'] );
$this->assertSame( '2012-12-02 18:54:49', $insertedPayment['valuation_date'] );
$this->assertNull( $insertedPayment['parent_payment_id'] );
$this->assertSame( PayPalPaymentBookingData::newEncodedValidBookingData(), $insertedPayment['booking_data'] );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPUnit\Framework\TestCase;
use WMDE\Euro\Euro;
use WMDE\Fundraising\PaymentContext\Domain\Model\BookingDataTransformers\CreditCardBookingTransformer;
use WMDE\Fundraising\PaymentContext\Domain\Model\ValuationDateTimeZone;

/**
* @covers \WMDE\Fundraising\PaymentContext\Domain\Model\BookingDataTransformers\CreditCardBookingTransformer
Expand Down Expand Up @@ -62,15 +63,27 @@ public function testGivenNoValuationDateAndGetValuationDateReturnsCurrentDateTim
$this->assertEqualsWithDelta( time(), $transformer->getValuationDate()->getTimestamp(), 5 );
}

public function testGivenNoValuationDateAndGetValuationDateDateTimeInUTCTimeZone(): void {
$transformer = new CreditCardBookingTransformer( [
'transactionId' => 1,
'amount' => 123,
'sessionId' => 'deadbeef'
] );

$this->assertEqualsCanonicalizing( ValuationDateTimeZone::getTimeZone(), $transformer->getValuationDate()->getTimezone() );
}

public function testGivenValuationDateAndGetValuationDateReturnsValuationDate(): void {
$valuationDate = new \DateTimeImmutable();
$valuationDate = new \DateTimeImmutable( '2023-11-06 0:00:00', new \DateTimeZone( 'Europe/Berlin' ) );
$expectedValuationDate = new \DateTimeImmutable( '2023-11-05 23:00:00', ValuationDateTimeZone::getTimeZone() );
$transformer = new CreditCardBookingTransformer( [
'transactionId' => 1,
'amount' => 123,
'sessionId' => 'deadbeef'
], $valuationDate );

$this->assertEquals( $valuationDate, $transformer->getValuationDate() );
$this->assertEquals( ValuationDateTimeZone::getTimeZone(), $transformer->getValuationDate()->getTimezone() );
$this->assertEquals( $expectedValuationDate, $transformer->getValuationDate() );
}

public function testGivenBadBookingDataThrowsError(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\TestCase;
use WMDE\Fundraising\PaymentContext\Domain\Model\BookingDataTransformers\PayPalBookingTransformer;
use WMDE\Fundraising\PaymentContext\Domain\Model\ValuationDateTimeZone;
use WMDE\Fundraising\PaymentContext\Tests\Data\PayPalPaymentBookingData;

/**
Expand Down Expand Up @@ -74,6 +75,16 @@ public function testGetValuationDate(): void {
$this->assertEquals( new \DateTimeImmutable( PayPalPaymentBookingData::PAYMENT_DATE ), $transformer->getValuationDate() );
}

public function testTransformerConvertsValuationDateToUTC(): void {
$transformer = new PayPalBookingTransformer( PayPalPaymentBookingData::newValidBookingData() );
$expectedValuationDate = new \DateTimeImmutable( PayPalPaymentBookingData::PAYMENT_DATE_UTC, ValuationDateTimeZone::getTimeZone() );

$valuationDate = $transformer->getValuationDate();

$this->assertEquals( ValuationDateTimeZone::getTimeZone(), $valuationDate->getTimezone() );
$this->assertEquals( $expectedValuationDate, $valuationDate );
}

public function testGetTransactionId(): void {
$transformer = new PayPalBookingTransformer( PayPalPaymentBookingData::newValidBookingData() );

Expand Down
6 changes: 5 additions & 1 deletion tests/Unit/Domain/Model/SofortPaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use WMDE\Fundraising\PaymentContext\Domain\Model\PaymentInterval;
use WMDE\Fundraising\PaymentContext\Domain\Model\PaymentReferenceCode;
use WMDE\Fundraising\PaymentContext\Domain\Model\SofortPayment;
use WMDE\Fundraising\PaymentContext\Domain\Model\ValuationDateTimeZone;
use WMDE\Fundraising\PaymentContext\Tests\Fixtures\DummyPaymentIdRepository;
use WMDE\Fundraising\PaymentContext\Tests\Inspectors\SofortPaymentInspector;

Expand Down Expand Up @@ -76,7 +77,10 @@ public function testBookPaymentSetsValuationDate(): void {

$sofortPayment->bookPayment( $this->makeValidTransactionData(), new DummyPaymentIdRepository() );

$this->assertEquals( new \DateTimeImmutable( '2001-12-24T17:30:00Z' ), $sofortPayment->getValuationDate() );
$this->assertEquals(
new \DateTimeImmutable( '2001-12-24T17:30:00', ValuationDateTimeZone::getTimeZone() ),
$sofortPayment->getValuationDate()
);
}

public function testBookPaymentSetsTransactionId(): void {
Expand Down

0 comments on commit a1cbb1d

Please sign in to comment.