The Clean Architecture in php



Download 2,26 Mb.
Pdf ko'rish
bet145/179
Sana24.06.2021
Hajmi2,26 Mb.
#100337
1   ...   141   142   143   144   145   146   147   148   ...   179
Bog'liq
The-Clean-Architecture-in-PHP-Kristopher-Wilson

class
InvoicingService
{
protected
$orderRepository
;
public function
__construct
(OrderRepositoryInterface
$orderRepository
) {
$this
->
orderRepository
=
$orderRepository
;
}
public function
generateInvoices
() {
$orders
=
$this
->
orderRepository
->
getUninvoicedOrders
();
}
}
We’re simply doing exactly what our test expected: injecting an instance of
OrderRepositoryInterface
and calling its
getUninvoicedOrders()
method when calling the
generateUninvoicedOrders()
method.
Our tests should now pass, so let’s dig deeper into the functionality of this service:


Building Our Domain
115
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
));
});
We’ve now brought the
InvoiceFactory
into the picture, and are testing that its
createFromOrder
method is called with the results of
getUninvoicedOrders
, meaning that each
Order
returned
should be run through the
InvoiceFactory
to generate an
Invoice
.
We’re using the
willReturn()
method of Prophecy to instruct the Mock to return
$orders
whenever
getUninvoicedOrders()
is called, and that
$invoices[0]
should be returned when
createFromOrder()
is called with
$orders[0]
as an argument.
Finally, we’re doing some expectations after instantiating our object and calling the
generateInvoices()
method to ensure that it is returning the proper data.
Since we’re now utilizing the
InvoiceFactory
, which we’ll need to mock as well, as we want to
be able to test the
InvoicingService
in isolation without having to test the
InvoiceFactory
as
well, so let’s add that mock to the
beforeEach()
:
beforeEach(
function
() {
$repo
=
'CleanPhp\Invoicer\Domain\Repository\OrderRepositoryInterface'
;
$this
->
repository
=
$this
->
getProphet
()
->
prophesize
(
$repo
);
$this
->
factory
=
$this
->
getProphet
()
->
prophesize
(
'CleanPhp\Invoicer\Domain\Factory\InvoiceFactory'
);
});
And we’ll have to update the other test to inject it as well, otherwise it will throw errors:


Building Our Domain
116
it(
'should query the repository for uninvoiced Orders'
,
function
() {
$this
->
repository
->
getUninvoicedOrders
()
->
shouldBeCalled
();
$service
=
new
InvoicingService(
$this
->
repository
->
reveal
(),
$this
->
factory
->
reveal
()
);
$service
->
generateInvoices
();
});
Our tests are, of course, failing as the code isn’t setup to meet the expectations of the tests, so
let’s go finalize the
InvoicingService
:
// src/Domain/Service/InvoicingService.php
namespace
CleanPhp\Invoicer\Domain\Service;
use
CleanPhp\Invoicer\Domain\Factory\InvoiceFactory;
use
CleanPhp\Invoicer\Domain\Repository\OrderRepositoryInterface;
class
InvoicingService
{
protected
$orderRepository
;
protected
$invoiceFactory
;
public function
__construct
(
OrderRepositoryInterface
$orderRepository
,
InvoiceFactory
$invoiceFactory
) {
$this
->
orderRepository
=
$orderRepository
;
$this
->
invoiceFactory
=
$invoiceFactory
;
}
public function
generateInvoices
() {
$orders
=
$this
->
orderRepository
->
getUninvoicedOrders
();
$invoices
=
[];
foreach
(
$orders
as
$order
) {
$invoices
[]
=
$this
->
invoiceFactory
->
createFromOrder
(
$order
);
}
return
$invoices
;
}
}
We’re now accepting an instance of
InvoiceFactory
in the constructor, and using it while
looping the results of
getUninvoicedOrders()
to create an Invoice for each Order. When we’re
done, we return this collection. Exactly as our behavior was defined in the test.


Building Our Domain
117
Our tests are passing, and our service is now complete.
This would make a good place to commit your code to source control.
If you’re just reading, but want to see the code in action, you can checkout the tag
02-domain-services:
git
clone
https
://
github
.
com
/
mrkrstphr
/
cleanphp
-
example
.
git
git checkout
02-
domain
-
services
Wrapping it Up
This concludes the domain of our application. The domain model and domain services layers
of our application are central to everything else. They contain our business logic, which is the
portion of our application that will not change depending on which libraries or frameworks we
decide to use, or how we choose to persist our database.
Everything that exists outside of these two layers will utilize them to complete the goals of the
application.
We’re now ready to start building out the front-end portion of the application!


Zend Framework 2 Setup
We’re going to start building our project using Zend Framework 2. ZF2 is the second iteration
of Zend Framework, and is a modular, event-based framework with an extensive set of libraries.
It is, at least out of the box using the sample application, an MVC framework with support for
various database systems.
While ZF2 isn’t my first choice in frameworks, it might be one someone lands upon when first
looking for a framework, especially due to the incorrect assumption that it might actually be
endorsed by or affiliated with PHP itself. This misconception likely stems from the fact that the
current runtime for PHP is called the Zend Engine, and the two people who started it, Andi
Gutmans and Zeev Suraski (Zeev + Andi = Zend), later started a company, Zend Technologies,
which is responsible for Zend Framework.
Regardless, Zend Framework is not directly affiliated with nor endorsed by PHP.

Download 2,26 Mb.

Do'stlaringiz bilan baham:
1   ...   141   142   143   144   145   146   147   148   ...   179




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish