Skip to content

Commit

Permalink
Change CreditMemo issuedAt to immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
diimpp committed Mar 11, 2020
1 parent 33f5676 commit af5147b
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 16 deletions.
30 changes: 30 additions & 0 deletions migrations/Version20200310185620.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20200310185620 extends AbstractMigration
{
public function getDescription(): string
{
return 'Convert sylius_refund_credit_memo issued_at to datetime_immutable';
}

public function up(Schema $schema): void
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE sylius_refund_credit_memo CHANGE issued_at issued_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\'');
}

public function down(Schema $schema): void
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE sylius_refund_credit_memo CHANGE issued_at issued_at DATETIME NOT NULL');
}
}
2 changes: 1 addition & 1 deletion spec/Entity/CreditMemoSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function let(
[$lineItem->getWrappedObject()],
[$taxItem->getWrappedObject()],
'Comment',
new \DateTime('01-01-2020 10:10:10'),
new \DateTimeImmutable('01-01-2020 10:10:10'),
new CustomerBillingData('Rick', 'Sanchez', 'Main St. 3322', '90802', 'US', 'Curse Purge Plus!', 'Los Angeles', 'Baldwin Hills', '323'),
new ShopBillingData('Needful Things', '000222', 'US', 'Main St. 123', 'Los Angeles', '90001')
);
Expand Down
2 changes: 1 addition & 1 deletion spec/Generator/CreditMemoGeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function it_generates_credit_memo_basing_on_event_data(
LineItemInterface $firstLineItem,
LineItemInterface $secondLineItem,
TaxItemInterface $taxItem,
\DateTime $dateTime
\DateTimeImmutable $dateTime
): void {
$firstUnitRefund = new OrderItemUnitRefund(1, 500);
$secondUnitRefund = new OrderItemUnitRefund(3, 500);
Expand Down
4 changes: 2 additions & 2 deletions spec/Generator/SequentialNumberGeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function it_generates_sequential_number(
EntityManagerInterface $sequenceManager,
CurrentDateTimeProviderInterface $currentDateTimeProvider,
CreditMemoSequenceInterface $sequence,
\DateTime $now
\DateTimeImmutable $now
): void {
$currentDateTimeProvider->now()->willReturn($now);
$now->format('Y/m')->willReturn('2018/05');
Expand All @@ -64,7 +64,7 @@ function it_generates_invoice_number_when_sequence_is_null(
EntityManagerInterface $sequenceManager,
CurrentDateTimeProviderInterface $currentDateTimeProvider,
CreditMemoSequenceInterface $sequence,
\DateTime $now
\DateTimeImmutable $now
): void {
$currentDateTimeProvider->now()->willReturn($now);
$now->format('Y/m')->willReturn('2018/05');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
use PhpSpec\ObjectBehavior;
use Sylius\RefundPlugin\Provider\CurrentDateTimeProviderInterface;

final class CurrentDateTimeProviderSpec extends ObjectBehavior
final class CurrentDateTimeImmutableProviderSpec extends ObjectBehavior
{
function it_implements_current_date_time_provider_interface(): void
{
$this->shouldImplement(CurrentDateTimeProviderInterface::class);
}

function it_provides_current_date_and_time(): void
function it_provides_current_immutable_date_and_time(): void
{
$this->now()->shouldReturnDate(new \DateTime());
$this->now()->shouldReturnDate(new \DateTimeImmutable());
}

public function getMatchers(): array
{
return [
'returnDate' => function (\DateTimeInterface $dateTime): bool {
$now = new \DateTime();
$now = new \DateTimeImmutable();

return $dateTime->format('d/m/Y H:i:s') === $now->format('d/m/Y H:i:s');
},
Expand Down
6 changes: 3 additions & 3 deletions src/Entity/CreditMemo.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CreditMemo implements CreditMemoInterface
/** @var string */
protected $comment;

/** @var \DateTimeInterface */
/** @var \DateTimeImmutable */
protected $issuedAt;

/** @var CustomerBillingDataInterface */
Expand All @@ -62,7 +62,7 @@ public function __construct(
array $lineItems,
array $taxItems,
string $comment,
\DateTimeInterface $issuedAt,
\DateTimeImmutable $issuedAt,
CustomerBillingDataInterface $from,
?ShopBillingDataInterface $to
) {
Expand Down Expand Up @@ -131,7 +131,7 @@ public function getComment(): string
return $this->comment;
}

public function getIssuedAt(): \DateTimeInterface
public function getIssuedAt(): \DateTimeImmutable
{
return $this->issuedAt;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/CreditMemoInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function getTaxItems(): Collection;

public function getComment(): string;

public function getIssuedAt(): \DateTimeInterface;
public function getIssuedAt(): \DateTimeImmutable;

public function getFrom(): CustomerBillingDataInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Sylius\RefundPlugin\Provider;

final class CurrentDateTimeProvider implements CurrentDateTimeProviderInterface
final class CurrentDateTimeImmutableProvider implements CurrentDateTimeProviderInterface
{
public function now(): \DateTimeInterface
{
return new \DateTime();
return new \DateTimeImmutable();
}
}
2 changes: 1 addition & 1 deletion src/Resources/config/doctrine/CreditMemo.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<field name="currencyCode" column="currency_code" type="string" />
<field name="localeCode" column="locale_code" type="string" />
<field name="comment" column="comment" type="text" />
<field name="issuedAt" column="issued_at" type="datetime" />
<field name="issuedAt" column="issued_at" type="datetime_immutable" />

<many-to-one field="channel" target-entity="Sylius\Component\Core\Model\ChannelInterface">
<join-column name="channel_id" />
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services/provider.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<service
id="Sylius\RefundPlugin\Provider\CurrentDateTimeProviderInterface"
class="Sylius\RefundPlugin\Provider\CurrentDateTimeProvider"
class="Sylius\RefundPlugin\Provider\CurrentDateTimeImmutableProvider"
/>

<service id="Sylius\RefundPlugin\Provider\RemainingTotalProvider">
Expand Down

0 comments on commit af5147b

Please sign in to comment.