/**
  * @return array
  */
 public function generateProcessAction()
 {
     $invoices = $this->invoicing->generateInvoices();
     $this->invoices->begin();
     foreach ($invoices as $invoice) {
         $this->invoices->persist($invoice);
     }
     $this->invoices->commit();
     return ['invoices' => $invoices];
 }
 /**
  * @return \Illuminate\View\View
  */
 public function generateAction()
 {
     $invoices = $this->invoicing->generateInvoices();
     $this->invoiceRepository->begin();
     foreach ($invoices as $invoice) {
         $this->invoiceRepository->persist($invoice);
     }
     $this->invoiceRepository->commit();
     return view('invoices/generate', ['invoices' => $invoices]);
 }
<?php

use CleanPhp\Invoicer\Domain\Entity\Invoice;
use CleanPhp\Invoicer\Domain\Entity\Order;
use CleanPhp\Invoicer\Domain\Service\InvoicingService;
describe('InvoicingService', function () {
    describe('->generateInvoices()', function () {
        beforeEach(function () {
            $this->repository = $this->getProphet()->prophesize('CleanPhp\\Invoicer\\Domain\\Repository\\OrderRepositoryInterface');
            $this->factory = $this->getProphet()->prophesize('CleanPhp\\Invoicer\\Domain\\Factory\\InvoiceFactory');
        });
        it('should query the repository for uninvoiced Orders', function () {
            $this->repository->getUninvoicedOrders()->shouldBeCalled();
            $service = new InvoicingService($this->repository->reveal(), $this->factory->reveal());
            $service->generateInvoices();
        });
        it('should return an Invoice for each uninvoiced Order', function () {
            $orders = [(new Order())->setTotal(400)];
            $invoices = [(new Invoice())->setTotal(400)];
            $this->repository->getUninvoicedOrders()->willReturn($orders);
            $this->factory->createFromOrder($orders[0])->willReturn($invoices[0]);
            $service = new InvoicingService($this->repository->reveal(), $this->factory->reveal());
            $results = $service->generateInvoices();
            expect($results)->to->be->a('array');
            expect($results)->to->have->length(count($orders));
        });
        afterEach(function () {
            $this->getProphet()->checkPredictions();
        });
    });
});