Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maximehuran committed Aug 27, 2024
1 parent 6cbbe5b commit 8099732
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 136 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export COMPOSE_PROJECT_NAME=sales-reports
export MIGRATIONS_NAMESPACE=MonsieurBiz\\SyliusSalesReportsPlugin\\Migrations
export USER_UID=$(shell id -u)
PLUGIN_NAME=sylius-${COMPOSE_PROJECT_NAME}-plugin
COMPOSE=docker-compose
COMPOSE=docker compose
YARN=yarn

###
Expand Down
1 change: 1 addition & 0 deletions dist/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SYLIUS_FIXTURES_HOSTNAME=${SYMFONY_DEFAULT_ROUTE_HOST:-localhost}
Empty file removed dist/.gitkeep
Empty file.
1 change: 0 additions & 1 deletion recipes/1.0-dev

This file was deleted.

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions recipes/1.0/manifest.json

This file was deleted.

43 changes: 24 additions & 19 deletions src/Controller/Admin/ReportsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace MonsieurBiz\SyliusSalesReportsPlugin\Controller\Admin;

use DateTimeInterface;
use MonsieurBiz\SyliusSalesReportsPlugin\Event\CustomReportEvent;
use MonsieurBiz\SyliusSalesReportsPlugin\Exception\InvalidDateException;
use MonsieurBiz\SyliusSalesReportsPlugin\Form\Type\DateType;
Expand Down Expand Up @@ -42,6 +43,8 @@ final class ReportsController extends AbstractController

/**
* ReportsController constructor.
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function __construct(
ReportRepository $reportRepository,
Expand All @@ -53,6 +56,8 @@ public function __construct(

/**
* View the report for a single date.
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function indexAction(Request $request): Response
{
Expand Down Expand Up @@ -83,30 +88,30 @@ public function indexAction(Request $request): Response
$isPeriod = true;
}
$channel = $data['channel'];
$from = $data['date'] ?? $data['from'];
$to = $data['date'] ?? $data['to'];
$fromDate = $data['date'] ?? $data['from'];
$toDate = $data['date'] ?? $data['to'];

// Reverse date if from date greater than end date
if ($from > $to) {
$tmp = $to;
$to = $from;
$from = $tmp;
$data['from'] = $from;
$data['to'] = $to;
if ($fromDate > $toDate) {
$tmp = $toDate;
$toDate = $fromDate;
$fromDate = $tmp;
$data['from'] = $fromDate;
$data['to'] = $toDate;
}

Assert::isInstanceOf($channel, ChannelInterface::class);
Assert::isInstanceOf($from, \DateTimeInterface::class);
Assert::isInstanceOf($to, \DateTimeInterface::class);
Assert::isInstanceOf($fromDate, DateTimeInterface::class);
Assert::isInstanceOf($toDate, DateTimeInterface::class);

// Form is valid, we can generate the report
try {
$totalSalesResult = $this->reportRepository->getSalesForChannelForDates($channel, $from, $to);
$averageSalesResult = $this->reportRepository->getAverageSalesForChannelForDates($channel, $from, $to);
$productSalesResult = $this->reportRepository->getProductSalesForChannelForDates($channel, $from, $to);
$productVariantSalesResult = $this->reportRepository->getProductVariantSalesForChannelForDates($channel, $from, $to);
$productOptionSalesResult = $this->reportRepository->getProductOptionSalesForChannelForDates($channel, $from, $to);
$productOptionValueSalesResult = $this->reportRepository->getProductOptionValueSalesForChannelForDates($channel, $from, $to);
$totalSalesResult = $this->reportRepository->getSalesForChannelForDates($channel, $fromDate, $toDate);
$averageSalesResult = $this->reportRepository->getAverageSalesForChannelForDates($channel, $fromDate, $toDate);
$productSalesResult = $this->reportRepository->getProductSalesForChannelForDates($channel, $fromDate, $toDate);
$productVariantSalesResult = $this->reportRepository->getProductVariantSalesForChannelForDates($channel, $fromDate, $toDate);
$productOptionSalesResult = $this->reportRepository->getProductOptionSalesForChannelForDates($channel, $fromDate, $toDate);
$productOptionValueSalesResult = $this->reportRepository->getProductOptionValueSalesForChannelForDates($channel, $fromDate, $toDate);
} catch (InvalidDateException $e) {
$form->addError(new FormError($e->getMessage()));

Expand All @@ -115,14 +120,14 @@ public function indexAction(Request $request): Response
]);
}

$event = new CustomReportEvent($channel, $from, $to);
$event = new CustomReportEvent($channel, $fromDate, $toDate);
$this->eventDispatcher->dispatch($event);

return $this->render('@MonsieurBizSyliusSalesReportsPlugin/Admin/view.html.twig', [
'form' => $form->createView(),
'form_period' => $formPeriod->createView(),
'from' => $from,
'to' => $to,
'from' => $fromDate,
'to' => $toDate,
'channel' => $data['channel'],
'total_sales_result' => $totalSalesResult,
'average_sales_result' => $averageSalesResult,
Expand Down
10 changes: 1 addition & 9 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ final class Configuration implements ConfigurationInterface
*/
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder(MonsieurBizSyliusSalesReportsExtension::EXTENSION_CONFIG_NAME);
if (method_exists($treeBuilder, 'getRootNode')) {
$rootNode = $treeBuilder->getRootNode();
} else {
// BC layer for symfony/config 4.1 and older
$rootNode = $treeBuilder->root(MonsieurBizSyliusSalesReportsExtension::EXTENSION_CONFIG_NAME);
}

return $treeBuilder;
return new TreeBuilder(MonsieurBizSyliusSalesReportsExtension::EXTENSION_CONFIG_NAME);
}
}
17 changes: 9 additions & 8 deletions src/Event/CustomReportEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace MonsieurBiz\SyliusSalesReportsPlugin\Event;

use DateTimeInterface;
use MonsieurBiz\SyliusSalesReportsPlugin\Exception\AlreadyExistsReport;
use MonsieurBiz\SyliusSalesReportsPlugin\Exception\NotExistsReport;
use Sylius\Component\Core\Model\ChannelInterface;
Expand All @@ -31,19 +32,19 @@ final class CustomReportEvent extends Event
private $channel;

/**
* @var \DateTimeInterface
* @var DateTimeInterface
*/
private $fromDate;

/**
* @var \DateTimeInterface|null
* @var DateTimeInterface|null
*/
private $toDate;

public function __construct(
ChannelInterface $channel,
\DateTimeInterface $fromDate,
?\DateTimeInterface $toDate = null
DateTimeInterface $fromDate,
?DateTimeInterface $toDate = null
) {
$this->channel = $channel;
$this->fromDate = $fromDate;
Expand All @@ -66,7 +67,7 @@ public function getCustomReports(): array
public function addReport(string $key, array $data): void
{
if (isset($this->customReports[$key])) {
throw new AlreadyExistsReport(sprintf('Report "%s" already exists', $key));
throw new AlreadyExistsReport(\sprintf('Report "%s" already exists', $key));
}
$this->customReports[$key] = $data;
}
Expand All @@ -79,7 +80,7 @@ public function addReport(string $key, array $data): void
public function removeReport(string $key): void
{
if (!isset($this->customReports[$key])) {
throw new NotExistsReport(sprintf('Report "%s" does not exist', $key));
throw new NotExistsReport(\sprintf('Report "%s" does not exist', $key));
}
unset($this->customReports[$key]);
}
Expand All @@ -89,12 +90,12 @@ public function getChannel(): ChannelInterface
return $this->channel;
}

public function getFromDate(): \DateTimeInterface
public function getFromDate(): DateTimeInterface
{
return $this->fromDate;
}

public function getToDate(): ?\DateTimeInterface
public function getToDate(): ?DateTimeInterface
{
return $this->toDate;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Exception/AlreadyExistsReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace MonsieurBiz\SyliusSalesReportsPlugin\Exception;

class AlreadyExistsReport extends \Exception
use Exception;

class AlreadyExistsReport extends Exception
{
}
4 changes: 3 additions & 1 deletion src/Exception/InvalidDateException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace MonsieurBiz\SyliusSalesReportsPlugin\Exception;

class InvalidDateException extends \Exception
use Exception;

class InvalidDateException extends Exception
{
}
4 changes: 3 additions & 1 deletion src/Exception/MissingLocaleException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace MonsieurBiz\SyliusSalesReportsPlugin\Exception;

class MissingLocaleException extends \Exception
use Exception;

class MissingLocaleException extends Exception
{
}
4 changes: 3 additions & 1 deletion src/Exception/NotExistsReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace MonsieurBiz\SyliusSalesReportsPlugin\Exception;

class NotExistsReport extends \Exception
use Exception;

class NotExistsReport extends Exception
{
}
3 changes: 3 additions & 0 deletions src/Form/Type/DateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

class DateType extends AbstractType
{
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
Expand Down
3 changes: 3 additions & 0 deletions src/Form/Type/PeriodType.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

class PeriodType extends AbstractType
{
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
Expand Down
10 changes: 3 additions & 7 deletions src/MonsieurBizSyliusSalesReportsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace MonsieurBiz\SyliusSalesReportsPlugin;

use LogicException;
use Sylius\Bundle\CoreBundle\Application\SyliusPluginTrait;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;
Expand All @@ -24,22 +25,17 @@ final class MonsieurBizSyliusSalesReportsPlugin extends Bundle
/**
* Returns the plugin's container extension.
*
* @throws \LogicException
* @throws LogicException
*
* @return ExtensionInterface|null The container extension
*/
public function getContainerExtension(): ?ExtensionInterface
{
if (null === $this->containerExtension) {
$this->containerExtension = false;
$extension = $this->createContainerExtension();

if (null !== $extension) {
if (!$extension instanceof ExtensionInterface) {
throw new \LogicException(sprintf('Extension %s must implement %s.', \get_class($extension), ExtensionInterface::class));
}
$this->containerExtension = $extension;
} else {
$this->containerExtension = false;
}
}

Expand Down
Loading

0 comments on commit 8099732

Please sign in to comment.