Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sending of quotes and invoices #666

Merged
merged 6 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 0 additions & 80 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -870,21 +870,11 @@ parameters:
count: 1
path: src/InvoiceBundle/Action/RecurringTransition.php

-
message: "#^Method SolidInvoice\\\\InvoiceBundle\\\\Action\\\\Transition\\:\\:__invoke\\(\\) has no return type specified\\.$#"
count: 1
path: src/InvoiceBundle/Action/Transition.php

-
message: "#^Negated boolean expression is always false\\.$#"
count: 1
path: src/InvoiceBundle/Action/Transition.php

-
message: "#^Method SolidInvoice\\\\InvoiceBundle\\\\Action\\\\Transition\\\\Send\\:\\:__invoke\\(\\) has no return type specified\\.$#"
count: 1
path: src/InvoiceBundle/Action/Transition/Send.php

-
message: "#^Negated boolean expression is always false\\.$#"
count: 1
Expand Down Expand Up @@ -1070,31 +1060,6 @@ parameters:
count: 1
path: src/InvoiceBundle/Tests/Cloner/InvoiceClonerTest.php

-
message: "#^Method SolidInvoice\\\\InvoiceBundle\\\\Tests\\\\Form\\\\Handler\\\\InvoiceCreateHandlerTest\\:\\:getFormData\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/InvoiceBundle/Tests/Form/Handler/InvoiceCreateHandlerTest.php

-
message: "#^Method SolidInvoice\\\\InvoiceBundle\\\\Tests\\\\Form\\\\Handler\\\\InvoiceCreateHandlerTest\\:\\:getHandlerOptions\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/InvoiceBundle/Tests/Form/Handler/InvoiceCreateHandlerTest.php

-
message: "#^Method SolidInvoice\\\\InvoiceBundle\\\\Tests\\\\Form\\\\Handler\\\\InvoiceEditHandlerTest\\:\\:getFormData\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/InvoiceBundle/Tests/Form/Handler/InvoiceEditHandlerTest.php

-
message: "#^Method SolidInvoice\\\\InvoiceBundle\\\\Tests\\\\Form\\\\Handler\\\\InvoiceEditHandlerTest\\:\\:getHandlerOptions\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/InvoiceBundle/Tests/Form/Handler/InvoiceEditHandlerTest.php

-
message: "#^Property SolidInvoice\\\\InvoiceBundle\\\\Tests\\\\Form\\\\Handler\\\\InvoiceEditHandlerTest\\:\\:\\$invoice has no type specified\\.$#"
count: 1
path: src/InvoiceBundle/Tests/Form/Handler/InvoiceEditHandlerTest.php

-
message: "#^Method SolidInvoice\\\\MailerBundle\\\\Configurator\\\\ConfiguratorInterface\\:\\:configure\\(\\) has parameter \\$config with no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -1140,16 +1105,6 @@ parameters:
count: 1
path: src/MailerBundle/Factory/MailerConfigFactory.php

-
message: "#^Property SolidInvoice\\\\MailerBundle\\\\Factory\\\\MailerConfigFactory\\:\\:\\$config has no type specified\\.$#"
count: 1
path: src/MailerBundle/Factory/MailerConfigFactory.php

-
message: "#^Property SolidInvoice\\\\MailerBundle\\\\Factory\\\\MailerConfigFactory\\:\\:\\$inner has no type specified\\.$#"
count: 1
path: src/MailerBundle/Factory/MailerConfigFactory.php

-
message: "#^Method SolidInvoice\\\\MoneyBundle\\\\Calculator\\:\\:calculateDiscount\\(\\) has parameter \\$entity with no type specified\\.$#"
count: 1
Expand Down Expand Up @@ -1610,16 +1565,6 @@ parameters:
count: 1
path: src/QuoteBundle/Action/Index.php

-
message: "#^Method SolidInvoice\\\\QuoteBundle\\\\Action\\\\Transition\\:\\:__invoke\\(\\) has no return type specified\\.$#"
count: 1
path: src/QuoteBundle/Action/Transition.php

-
message: "#^Method SolidInvoice\\\\QuoteBundle\\\\Action\\\\Transition\\\\Send\\:\\:__invoke\\(\\) has no return type specified\\.$#"
count: 1
path: src/QuoteBundle/Action/Transition/Send.php

-
message: "#^Method SolidInvoice\\\\QuoteBundle\\\\Cloner\\\\QuoteCloner\\:\\:addItems\\(\\) return type has no value type specified in iterable type Traversable\\.$#"
count: 1
Expand Down Expand Up @@ -1685,31 +1630,6 @@ parameters:
count: 1
path: src/QuoteBundle/Repository/QuoteRepository.php

-
message: "#^Method SolidInvoice\\\\QuoteBundle\\\\Tests\\\\Form\\\\Handler\\\\QuoteCreateHandlerTest\\:\\:getFormData\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/QuoteBundle/Tests/Form/Handler/QuoteCreateHandlerTest.php

-
message: "#^Method SolidInvoice\\\\QuoteBundle\\\\Tests\\\\Form\\\\Handler\\\\QuoteCreateHandlerTest\\:\\:getHandlerOptions\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/QuoteBundle/Tests/Form/Handler/QuoteCreateHandlerTest.php

-
message: "#^Method SolidInvoice\\\\QuoteBundle\\\\Tests\\\\Form\\\\Handler\\\\QuoteEditHandlerTest\\:\\:getFormData\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/QuoteBundle/Tests/Form/Handler/QuoteEditHandlerTest.php

-
message: "#^Method SolidInvoice\\\\QuoteBundle\\\\Tests\\\\Form\\\\Handler\\\\QuoteEditHandlerTest\\:\\:getHandlerOptions\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/QuoteBundle/Tests/Form/Handler/QuoteEditHandlerTest.php

-
message: "#^Property SolidInvoice\\\\QuoteBundle\\\\Tests\\\\Form\\\\Handler\\\\QuoteEditHandlerTest\\:\\:\\$quote has no type specified\\.$#"
count: 1
path: src/QuoteBundle/Tests/Form/Handler/QuoteEditHandlerTest.php

-
message: "#^Method SolidInvoice\\\\SettingsBundle\\\\Action\\\\Index\\:\\:__invoke\\(\\) has no return type specified\\.$#"
count: 1
Expand Down
2 changes: 1 addition & 1 deletion src/InvoiceBundle/Action/Transition.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(RouterInterface $router, StateMachine $stateMachine)
$this->stateMachine = $stateMachine;
}

public function __invoke(Request $request, string $action, Invoice $invoice)
public function __invoke(Request $request, string $action, Invoice $invoice): RedirectResponse
{
if (! $this->stateMachine->can($invoice, $action)) {
throw new InvalidTransitionException($action);
Expand Down
17 changes: 4 additions & 13 deletions src/InvoiceBundle/Action/Transition/Send.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,11 @@ final class Send
{
use SaveableTrait;

/**
* @var StateMachine
*/
private $stateMachine;
private StateMachine $stateMachine;

/**
* @var MailerInterface
*/
private $mailer;
private MailerInterface $mailer;

/**
* @var RouterInterface
*/
private $router;
private RouterInterface $router;

public function __construct(StateMachine $stateMachine, MailerInterface $mailer, RouterInterface $router)
{
Expand All @@ -50,7 +41,7 @@ public function __construct(StateMachine $stateMachine, MailerInterface $mailer,
$this->router = $router;
}

public function __invoke(Request $request, Invoice $invoice)
public function __invoke(Request $request, Invoice $invoice): RedirectResponse
{
if (Graph::STATUS_PENDING !== $invoice->getStatus() && $this->stateMachine->can($invoice, Graph::TRANSITION_ACCEPT)) {
$this->stateMachine->apply($invoice, Graph::TRANSITION_ACCEPT);
Expand Down
18 changes: 16 additions & 2 deletions src/InvoiceBundle/Form/Handler/AbstractInvoiceHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use SolidInvoice\CoreBundle\Response\FlashResponse;
use SolidInvoice\CoreBundle\Traits\SaveableTrait;
use SolidInvoice\InvoiceBundle\Email\InvoiceEmail;
use SolidInvoice\InvoiceBundle\Entity\Invoice;
use SolidInvoice\InvoiceBundle\Entity\RecurringInvoice;
use SolidInvoice\InvoiceBundle\Form\Type\InvoiceType;
Expand All @@ -29,6 +30,7 @@
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Workflow\StateMachine;
Expand All @@ -43,11 +45,18 @@ abstract class AbstractInvoiceHandler implements FormHandlerInterface, FormHandl

private RouterInterface $router;

public function __construct(StateMachine $invoiceStateMachine, StateMachine $recurringInvoiceStateMachine, RouterInterface $router)
{
private MailerInterface $mailer;

public function __construct(
StateMachine $invoiceStateMachine,
StateMachine $recurringInvoiceStateMachine,
RouterInterface $router,
MailerInterface $mailer
) {
$this->invoiceStateMachine = $invoiceStateMachine;
$this->recurringInvoiceStateMachine = $recurringInvoiceStateMachine;
$this->router = $router;
$this->mailer = $mailer;
}

public function getForm(FormFactoryInterface $factory, Options $options)
Expand All @@ -72,6 +81,11 @@ public function onSuccess(FormRequest $form, $data): ?Response
}

$this->save($data);

if (Graph::STATUS_PENDING === $action && ! $isRecurring) {
$this->mailer->send(new InvoiceEmail($data));
}

$route = $this->router->generate($isRecurring ? '_invoices_view_recurring' : '_invoices_view', ['id' => $data->getId()]);

return new class($route) extends RedirectResponse implements FlashResponse {
Expand Down
4 changes: 4 additions & 0 deletions src/InvoiceBundle/Manager/InvoiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ private function createFromObject($object): Invoice
$invoice->addItem($invoiceItem);
}

if ($object instanceof Quote) {
$invoice->setQuote($object);
}

return $invoice;
}

Expand Down
21 changes: 16 additions & 5 deletions src/InvoiceBundle/Tests/Form/Handler/InvoiceCreateHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Workflow\Definition;
use Symfony\Component\Workflow\MarkingStore\MethodMarkingStore;
use Symfony\Component\Workflow\StateMachine;
use Symfony\Component\Workflow\Transition;

class InvoiceCreateHandlerTest extends FormHandlerTestCase
/**
* @covers \SolidInvoice\InvoiceBundle\Form\Handler\InvoiceCreateHandler
*/
final class InvoiceCreateHandlerTest extends FormHandlerTestCase
{
public function getHandler()
public function getHandler(): InvoiceCreateHandler
{
$dispatcher = new EventDispatcher();
$notification = M::mock(NotificationManager::class);
Expand Down Expand Up @@ -69,16 +73,17 @@ public function getHandler()
->withAnyArgs()
->andReturn('/invoices/1');

$handler = new InvoiceCreateHandler($stateMachine, $recurringStateMachine, $router);
$handler = new InvoiceCreateHandler($stateMachine, $recurringStateMachine, $router, M::mock(MailerInterface::class));
$handler->setDoctrine($this->registry);

return $handler;
}

/**
* @param Invoice $invoice
*/
protected function assertOnSuccess(?Response $response, FormRequest $form, $invoice): void
{
/** @var Invoice $invoice */

self::assertSame(Graph::STATUS_DRAFT, $invoice->getStatus());
self::assertInstanceOf(RedirectResponse::class, $response);
self::assertInstanceOf(FlashResponse::class, $response);
Expand All @@ -91,6 +96,9 @@ protected function assertResponse(FormRequest $formRequest): void
self::assertInstanceOf(Template::class, $formRequest->getResponse());
}

/**
* @return array<string, mixed>
*/
protected function getHandlerOptions(): array
{
return [
Expand All @@ -101,6 +109,9 @@ protected function getHandlerOptions(): array
];
}

/**
* @return array<string, mixed>
*/
public function getFormData(): array
{
return [
Expand Down
32 changes: 25 additions & 7 deletions src/InvoiceBundle/Tests/Form/Handler/InvoiceEditHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@

use Mockery as M;
use Money\Currency;
use Money\Money;
use SolidInvoice\CoreBundle\Entity\Discount;
use SolidInvoice\CoreBundle\Response\FlashResponse;
use SolidInvoice\CoreBundle\Templating\Template;
use SolidInvoice\FormBundle\Test\FormHandlerTestCase;
use SolidInvoice\InvoiceBundle\Email\InvoiceEmail;
use SolidInvoice\InvoiceBundle\Entity\Invoice;
use SolidInvoice\InvoiceBundle\Form\Handler\InvoiceEditHandler;
use SolidInvoice\InvoiceBundle\Listener\WorkFlowSubscriber;
Expand All @@ -28,15 +30,19 @@
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Workflow\Definition;
use Symfony\Component\Workflow\MarkingStore\MethodMarkingStore;
use Symfony\Component\Workflow\StateMachine;
use Symfony\Component\Workflow\Transition;

class InvoiceEditHandlerTest extends FormHandlerTestCase
/**
* @covers \SolidInvoice\InvoiceBundle\Form\Handler\InvoiceEditHandler
*/
final class InvoiceEditHandlerTest extends FormHandlerTestCase
{
private $invoice;
private Invoice $invoice;

protected function setUp(): void
{
Expand All @@ -48,13 +54,13 @@ protected function setUp(): void
$discount->setType(Discount::TYPE_PERCENTAGE);
$discount->setValue(10);
$this->invoice->setDiscount($discount);
$this->invoice->setBalance(new \Money\Money(1000, new Currency('USD')));
$this->invoice->setBalance(new Money(1000, new Currency('USD')));

$this->em->persist($this->invoice);
$this->em->flush();
}

public function getHandler()
public function getHandler(): InvoiceEditHandler
{
$dispatcher = new EventDispatcher();
$notification = M::mock(NotificationManager::class);
Expand Down Expand Up @@ -87,16 +93,22 @@ public function getHandler()
->withAnyArgs()
->andReturn('/invoices/1');

$handler = new InvoiceEditHandler($stateMachine, $recurringStateMachine, $router);
$mailer = M::mock(MailerInterface::class);
$mailer->shouldReceive('send')
->zeroOrMoreTimes()
->with(M::type(InvoiceEmail::class));

$handler = new InvoiceEditHandler($stateMachine, $recurringStateMachine, $router, $mailer);
$handler->setDoctrine($this->registry);

return $handler;
}

/**
* @param Invoice $invoice
*/
protected function assertOnSuccess(?Response $response, FormRequest $form, $invoice): void
{
/** @var Invoice $invoice */

self::assertSame(Graph::STATUS_PENDING, $invoice->getStatus());
self::assertSame(20.0, $invoice->getDiscount()->getValue());
self::assertInstanceOf(RedirectResponse::class, $response);
Expand All @@ -110,6 +122,9 @@ protected function assertResponse(FormRequest $formRequest): void
self::assertInstanceOf(Template::class, $formRequest->getResponse());
}

/**
* @return array<string, mixed>
*/
protected function getHandlerOptions(): array
{
return [
Expand All @@ -120,6 +135,9 @@ protected function getHandlerOptions(): array
];
}

/**
* @return array<string, mixed>
*/
public function getFormData(): array
{
return [
Expand Down