DDD with Behat
-
Upload
anton-serdyuk -
Category
Software
-
view
398 -
download
0
description
Transcript of DDD with Behat
![Page 1: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/1.jpg)
DDD with Behatby Anton Serdyuk
![Page 2: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/2.jpg)
Agile project and team evolution
1 2 3 4 5 6
![Page 3: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/3.jpg)
1. No tests at all
● You can write very good code, but this code WILL suck later
● “Refactoring” is a swearword● That is why constant “No time for
refactoring”● “We need to rewrite it from scratch” at the
end
1 2 3 4 5 6
![Page 4: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/4.jpg)
2. So called “Unittests”
● Not actual unittests● xUnit based tests for controllers, entities,
repositories etc without any order● Complex fixtures● Hard to support● “There is no time for tests” because of no
visible profit
21 3 4 5 6
![Page 5: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/5.jpg)
3. Selenium acceptance tests
● Written by QA automators usually in Java even in PHP and .NET projects
● This approach is widely spread, well known and may perform well, but I think autotests implementation is not a QA job
1 2 4 5 63
![Page 6: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/6.jpg)
4. Behat acceptance tests
● This approach may perform very well● Can be written by QA (and even sometimes by BA)● Some people call it BDD, but it is not BDD actuallyGiven there is registered user Bob
And I am on homepage
When I follow “Sign Up”
And I fill “Bob” in “Username”
And I press “Sign Up”
Then I should see “This username is already in use”
1 2 3 5 64
![Page 7: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/7.jpg)
5. BDD
● BDD is not about tests● It is the process of finding out and delivering features
which provide value to users● Should be used not only in dev team but in whole team
beginning from Product Owner
Given there is registered user Bob
When I try to sign up with username “Bob”
Then I should get error that this user is already signed up
1 2 3 4 65
![Page 8: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/8.jpg)
6. Lean
● The last point of Agile team and project journey
● It is all about how to deliver more value to customer and how to build sustainable business around this product
1 2 3 4 5 6
![Page 9: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/9.jpg)
4,5. DDD with Behat
● Not a BDD yet● Not just acceptance tests with Behat
BDD
Behat acceptance tests
DDD with Behat
![Page 10: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/10.jpg)
Functional tests with Behat
https://github.com/diaspora/diaspora/blob/develop/features/desktop/change_email.feature
![Page 11: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/11.jpg)
They are really cool
● Provide Continuous Integration and Continuous Delivery possibility
● Easy to write● Can be written by QA and even sometimes
by BA
![Page 12: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/12.jpg)
But...
● Long execution time○ Less green builds○ Wasted developers’ time
● Non-deterministic failures○ Developers try to rebuild every failing build instead
of investigating○ Wasted developers’ time
![Page 13: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/13.jpg)
But...
● They are too specific○ It is difficult to dig up business value from them○ less benefits for use by (and with) business people
(BA, Product Owner)
![Page 14: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/14.jpg)
DDD can help
![Page 15: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/15.jpg)
Model
Model is a system of abstractions that describes selected aspects of a domain and can be used to solve problems related to this domain
http://www.infoq.com/presentations/model-to-work-evans
![Page 16: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/16.jpg)
Ubiquitous Language
A language structured around the domain model and used by all team members to connect all the activities of the team with the software
http://www.infoq.com/presentations/model-to-work-evans
![Page 17: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/17.jpg)
Ubiquitous Language helps to find out model weaknesses
If domain expert does not understand you when you use ubiquitous language, then your model does not describe domain as needed and should be corrected.
![Page 18: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/18.jpg)
Multilayered Architecture
http://dddsample.sourceforge.net/architecture.html
![Page 19: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/19.jpg)
Layers in typical symfony2 project
● Infrastructure○ Doctrine○ SwiftMailer
● UI○ views○ JS
● Application○ controllers○ some services
● Domain○ entities
![Page 20: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/20.jpg)
Entities are not enough to create a powerful domain
model
![Page 21: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/21.jpg)
Domain Model
● Domain model is just Plain Old PHP Objects.● Framework-agnostic
![Page 22: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/22.jpg)
Domain Model
At its worst business logic can be very complex. Rules and logic describe many different cases and slants of behavior, and it's this complexity that objects were designed to work with. A Domain Model creates a web of interconnected objects, where each object represents some meaningful individual, whether as large as a corporation or as small as a single line on an order form.
http://martinfowler.com/eaaCatalog/domainModel.html
![Page 23: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/23.jpg)
Forget all you know about frameworks, controllers, forms, api etc. Just do
OOP.
![Page 24: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/24.jpg)
Behat can be used to drive Domain Model code
![Page 25: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/25.jpg)
Warning
This is real project code, so it is not ideal and can be possibly done better. But in real project we can not refactor our code infinitely because of obvious reasons.
![Page 26: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/26.jpg)
Sample user private messages functionality
● Registered user can send private message to any other registered user
● Messages should be combined to dialogs● There should be email notifications about
new messages
![Page 27: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/27.jpg)
![Page 28: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/28.jpg)
Scenario first
Do not think about implementation, think about business value, use-cases and user stories first.
![Page 29: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/29.jpg)
Tip: personalize your users in scenarios, remember their names and other private info and do not change them between scenarios
![Page 30: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/30.jpg)
Note: I forgot about last message text in dialogs, and it will be added later.
Note: “fetch messages” operation is part of our domain, because we want to know when to mark messages as read. So it should be explicitly added to the scenario
![Page 31: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/31.jpg)
![Page 32: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/32.jpg)
![Page 33: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/33.jpg)
Note: Maybe it would be better to split this scenario to 1) total number of unread messages should be decreased; 2) message should not be unread when I fetch messages second time.
![Page 34: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/34.jpg)
Note: Maybe it would be better to move last message text to dialog “properties” (see note to “I can send messages” scenario). Or we should remove “last message date” from dialog instead
![Page 35: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/35.jpg)
![Page 36: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/36.jpg)
![Page 37: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/37.jpg)
![Page 38: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/38.jpg)
Note: this is unusual situation. Doctrine Entities should be used as Domain
Entities when possible.
![Page 39: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/39.jpg)
![Page 40: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/40.jpg)
![Page 41: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/41.jpg)
Where is $em->flush()?
Avoid using $em->flush() at the domain model level. This is against of UnitOfWork ideology
http://martinfowler.com/eaaCatalog/unitOfWork.html
![Page 42: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/42.jpg)
![Page 43: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/43.jpg)
![Page 44: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/44.jpg)
![Page 45: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/45.jpg)
![Page 46: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/46.jpg)
![Page 47: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/47.jpg)
![Page 48: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/48.jpg)
![Page 49: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/49.jpg)
![Page 50: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/50.jpg)
Notifications
![Page 51: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/51.jpg)
![Page 52: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/52.jpg)
![Page 53: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/53.jpg)
![Page 54: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/54.jpg)
Simple and framework agnostic Domain Model
● Easy to write● Easy to read and understand● Can be guided by UnitTests if they have
complex logic● Easy to migrate between frameworks (if you
want to migrate, for example, from symfony2 to silex + AngularJS)
![Page 55: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/55.jpg)
Application and UI layers
![Page 56: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/56.jpg)
![Page 57: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/57.jpg)
![Page 58: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/58.jpg)
Domain scenarios steps can easily be reused in UI
tests
![Page 59: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/59.jpg)
![Page 60: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/60.jpg)
Profit● Everything needed by business can be done
without a single Controller line● Simple and clear integration tests can be
used as user stories by business people● Simple and thin controllers will not contain
business rules● Little amount of complex and heavy UI tests
(no need to test business expectations, only UI itself)
![Page 61: DDD with Behat](https://reader034.fdocuments.us/reader034/viewer/2022050905/54b740274a7959be4c8b48cf/html5/thumbnails/61.jpg)
Thanks!