Symfony2 best practices, business logic and unit test

I would like to create very convenient code with unit tests.

I read the best practices, so the directories are organized according to best practice, with only one package named AppBundle, and I use annotations.

I have a problem with business logic. I read that I should not put business logic inside controllers. Since the vast majority of my code was not intended to be reused, I put the logic inside the controllers. Yesterday, I read here that “logic should live in the controller if you don’t go to the unit test or until you need reuse” and it made me faint: really I won’t be unit test if I put my logic in the controllers ?

I understand that "controllers should be as thin as possible, with a few lines of code to be glued together." But what about forms? A form often has logic such as "display the form if it really does something and displays another page." Now, if I am going to put the creation of the form and the logic inside the service (or model?), I would have to insert a page render command into it, so basically the controller will be 1 line, and everything else inside the service and the actual decision about which page will be displayed, will be executed inside the service, not the controller itself ... so what is the point of the controller, just routing?

I really need to understand this before continuing (I have already developed 3 months on this and I will have to work hard, but better than never) ...

Thank!

EDIT: , .

, Symfony, "isValid()" .

, :

get the current customer from security context
queries the DB to get the field $user->getIsBillable()
if the user is not billable
    queries the DB to find if someone else is paying for him
else //the user is billable
    create and manage form1
if the user is not billable but there is someone who is paying for him
    check if the license is active (I have to call an external API)
    if the license is not ok
        redirect to a page to update credit card info
if the user is not billable
    create and manage form2
else
    create and manage form3
render the license management page with all the necessary forms created  

? , ( null, )? , "" . foreach ?

+4
4

, .

, - Symfony. , -.

, .

- Symfony2, : http://symfony.com/doc/current/book/forms.html

+7

php-, symfony2 .

, symfony2 - . symfony , "".

, : http://symfony.com/doc/current/cookbook/controller/service.html

, -. 3-5 . , .

unit test - , - .

, "Selenium" - , .

+2

, , , Unit Test Functional Test.

Unit Test

Unit Test, "" (, )

, "" (, , db, - ..)

Unit Test , -, ( db, , )

, - , , - : Unit Test ( , !) , () , , , .

, - , - - ( [ enaugh], ), ( ). Unit Test (s) , , , - (), , " ".

, - (). , Symfony2 ( MVC )

+1

Structuring your code to be clean and maintainable is very difficult, I suggest you start learning the principles SOLIDthan having a superficial understanding MVC:

0
source

All Articles