Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mpysiak committed Jun 26, 2024
1 parent 8a4ebef commit 3aec3aa
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 72 deletions.
13 changes: 0 additions & 13 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,6 @@ jobs:
name: Run PHPStan
run: vendor/bin/phpstan analyse

# -
# name: Run PHPSpec
# run: vendor/bin/phpspec run --ansi -f progress --no-interaction

-
name: Run PHPUnit
run: vendor/bin/phpunit --colors=always

-
name: Upload Behat logs
uses: actions/upload-artifact@v2
if: failure()
with:
name: Behat logs
path: etc/build/
if-no-files-found: ignore
4 changes: 1 addition & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
-->

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.2/phpunit.xsd" colors="true" cacheDirectory=".phpunit.cache">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" colors="true" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="all">
<directory>tests</directory>
Expand All @@ -22,5 +22,3 @@
<server name="KERNEL_CLASS" value="Tests\Sylius\GmvBundle\Application\TestKernel" />
</php>
</phpunit>


4 changes: 2 additions & 2 deletions src/Command/GmvCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::SUCCESS;
}

foreach ($gmvs as $key => $gmv) {
$output->writeln(sprintf('<comment>GMV in %s:</comment> %s', $key, $gmv));
foreach ($gmvs as $currencyCode => $gmv) {
$output->writeln(sprintf('<comment>GMV in %s:</comment> %s', $currencyCode, $gmv));
}

return Command::SUCCESS;
Expand Down
9 changes: 4 additions & 5 deletions src/Parser/DateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function parseStartOfMonth(string $date): \DateTime
throw new \InvalidArgumentException('Invalid date format. Expected format: m/Y.');
}

return $dateTime->modify('first day of this month')->setTime(0, 0, 0);
return $dateTime->modify('first day of this month 00:00:00');
}

public function parseEndOfMonth(string $date): \DateTime
Expand All @@ -34,23 +34,22 @@ public function parseEndOfMonth(string $date): \DateTime
throw new \InvalidArgumentException('Invalid date format. Expected format: m/Y.');
}

return $dateTime->modify('last day of this month')
->setTime(23, 59, 59);
return $dateTime->modify('last day of this month 23:59:59');
}

public function getDefaultStartDate(): \DateTime
{
$now = new \DateTime();

return (clone $now)
return $now
->modify('first day of -12 months');
}

public function getDefaultEndDate(): \DateTime
{
$now = new \DateTime();

return (clone $now)
return $now
->modify('last day of last month');
}
}
19 changes: 8 additions & 11 deletions src/Provider/GmvProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Sylius\Bundle\MoneyBundle\Formatter\MoneyFormatterInterface;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderCheckoutStates;
use Sylius\Component\Core\OrderPaymentStates;
Expand Down Expand Up @@ -50,7 +51,7 @@ private function findCurrenciesInOrders(\DateTimeInterface $periodStart, \DateTi
{
$queryBuilder = $this->orderRepository->createQueryBuilder('o');

$query = $queryBuilder
$currencies = $queryBuilder
->select('o.currencyCode')
->andWhere('o.checkoutCompletedAt >= :periodStart')
->andWhere('o.checkoutCompletedAt <= :periodEnd')
Expand All @@ -61,9 +62,8 @@ private function findCurrenciesInOrders(\DateTimeInterface $periodStart, \DateTi
->setParameter('completedState', OrderCheckoutStates::STATE_COMPLETED)
->setParameter('cancelledState', OrderPaymentStates::STATE_CANCELLED)
->groupBy('o.currencyCode')
->getQuery();

$currencies = $query->getScalarResult();
->getQuery()
->getScalarResult();

Assert::isArray($currencies);

Expand All @@ -74,7 +74,7 @@ private function calculateGmvForPeriodAndCurrency(\DateTimeInterface $periodStar
{
$queryBuilder = $this->orderRepository->createQueryBuilder('o');

$totalItemsQuery = $queryBuilder
$totalItemsQuery = (int) $queryBuilder
->select('SUM(o.itemsTotal) as totalItems')
->andWhere('o.checkoutCompletedAt >= :periodStart')
->andWhere('o.checkoutCompletedAt <= :periodEnd')
Expand All @@ -89,7 +89,7 @@ private function calculateGmvForPeriodAndCurrency(\DateTimeInterface $periodStar
->getQuery()
->getSingleScalarResult();

$totalTaxQuery = $queryBuilder
$totalTaxQuery = (int) $queryBuilder
->select('SUM(adjustment.amount) as totalTaxes')
->leftJoin('o.items', 'items')
->leftJoin('items.units', 'units')
Expand All @@ -103,14 +103,11 @@ private function calculateGmvForPeriodAndCurrency(\DateTimeInterface $periodStar
->setParameter('periodEnd', $periodEnd)
->setParameter('completedState', OrderCheckoutStates::STATE_COMPLETED)
->setParameter('cancelledState', OrderPaymentStates::STATE_CANCELLED)
->setParameter('taxType', 'tax')
->setParameter('taxType', AdjustmentInterface::TAX_ADJUSTMENT)
->setParameter('currencyCode', $currencyCode)
->getQuery()
->getSingleScalarResult();

$totalItems = (int) $totalItemsQuery;
$totalTaxes = (int) $totalTaxQuery;

return $totalItems - $totalTaxes;
return $totalItemsQuery - $totalTaxQuery;
}
}
20 changes: 10 additions & 10 deletions src/Validator/InputParametersValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ public function __construct(private readonly DateParserInterface $dateParser)
{
}

public function validateDateFormat(string $date): bool
{
return preg_match('/^(0[1-9]|1[0-2])\/\d{4}$/', $date) === 1;
}

public function validateDates(\DateTime $startDate, \DateTime $endDate): bool
{
return $startDate < $endDate;
}

public function validate(string $periodStart, string $periodEnd): bool
{
if (!$this->validateDateFormat($periodStart) || !$this->validateDateFormat($periodEnd)) {
Expand All @@ -42,4 +32,14 @@ public function validate(string $periodStart, string $periodEnd): bool

return $this->validateDates($startDate, $endDate);
}

private function validateDateFormat(string $date): bool
{
return preg_match('/^(0[1-9]|1[0-2])\/\d{4}$/', $date) === 1;
}

private function validateDates(\DateTime $startDate, \DateTime $endDate): bool
{
return $startDate < $endDate;
}
}
4 changes: 0 additions & 4 deletions src/Validator/InputParametersValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,5 @@

interface InputParametersValidatorInterface
{
public function validateDateFormat(string $date): bool;

public function validateDates(\DateTime $startDate, \DateTime $endDate): bool;

public function validate(string $periodStart, string $periodEnd): bool;
}
32 changes: 16 additions & 16 deletions tests/Application/config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
imports:
- { resource: '@SyliusCoreBundle/Resources/config/app/config.yml' }
- { resource: '@SyliusGmvBundle/config/services.xml' }
- { resource: '@SyliusCoreBundle/Resources/config/app/config.yml' }
- { resource: '@SyliusGmvBundle/config/services.xml' }

parameters:
locale: en_US
locale: en_US

security:
firewalls:
main:
security: false
firewalls:
main:
security: false

framework:
test: true
session:
handler_id: null
csrf_protection: ~
router:
resource: ~
secret: 'test'
test: true
session:
handler_id: null
csrf_protection: ~
router:
resource: ~
secret: 'test'

doctrine:
dbal:
driver: pdo_sqlite
path: '%kernel.project_dir%/var/data/test.sqlite'
dbal:
driver: pdo_sqlite
path: '%kernel.project_dir%/var/data/test.sqlite'
9 changes: 1 addition & 8 deletions tests/Functional/GmvProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
use Sylius\GmvBundle\Parser\DateParserInterface;
use Sylius\GmvBundle\Provider\GmvProviderInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

Expand All @@ -25,15 +24,12 @@ class GmvProviderTest extends KernelTestCase

private EntityManagerInterface $entityManager;

private DateParserInterface $dateParser;

protected function setUp(): void
{
self::bootKernel(['environment' => 'test']);

$container = static::getContainer();
$this->entityManager = $container->get('doctrine.orm.entity_manager');
$this->dateParser = $container->get('sylius_gmv.parser.date');
$this->gmvProvider = $container->get('sylius_gmv.provider.gmv');

$this->createDatabaseSchema();
Expand All @@ -46,10 +42,7 @@ public function testGmvNoSales(): void

$gmv = $this->gmvProvider->getGmvForPeriod($periodStart, $periodEnd);

$this->assertEquals(
[],
$gmv,
);
$this->assertEmpty($gmv);
}

public function testGmvNoShippingNoTaxes(): void
Expand Down

0 comments on commit 3aec3aa

Please sign in to comment.