简体   繁体   中英

PHP: Test driven development with an MVC application

I'm already writing some unit tests for the critical business logic of my projects and I am aware of the concept and the pros and cons of TDD - I just never really went "all the way TDD" with writing tests first and stuff. I am currently working on a medium-sized project that I didn't develop myself and this is quite horrible to work with: No tests at all, tightly coupled architecture, no dependency injection, legacy MVC framework, and so on - less than ideal. I am considering just starting from scratch using Laravel or Symfony and applying TDD to really achieve loose coupled and testable code.

I know that it's probably not a good idea just to dive head first into TDD with such a "large" project, so I was thinking that I'd do a couple of test projects to see how TDD affects my code design and quality. For the sake of it, let's assume it's just some kind of "movie-rental-application-thing" with user registration and a function to rent a movie if that is available. Let's also assume that I already did some UML diagrams and I have some ideas about the required objects, their relations and the required business logic.

So: Where to start? When talking to people who do TDD on MVC frameworks, some people tend to "isolate" a function of the business logic and start up with an acceptance test, the function being "Show list of available movies" and the test being like "Navigate to /movies and assert against some HTML" or whatever. To me, this doesn't feel like a good start.

Personally, I like to start with features like user login or user management - pretty much stuff that is rather supporting the application than being business logic itself. If I was to use that approach, wouldn't I be deliberately ignoring functions that I already know I will require? Let's pretend the movie-list test works, so I'd add another test like "OnlyLoggedInUserCanSeeMovieList", see it fail and add the logic to the code - the logic I KNEW I would need even before I wrote the first test at all. I find it hard to believe that this would lead to a better code as I'm deliberately not implementing functions that I'm already aware of.

Is this just up to personal preference or is there something like a best practice to get me started? How do you guys start with TDD in a framework like Symfony or Laravel? Is there even a point in doing pure TDD in such cases, seeing that a whole lot of application logic is already handled and tested by the framework itself? Don't get me wrong: I don't want to start another war about pros and cons of TDD - I'm sure there are pros I can benefit from once I get my head around this completely. Nonetheless, right now this feels like a ridiculous amount of micro-progress on rather simple functionality and I feel I'd be better of just unit testing important parts of application logic instead of doing all the "Write test, see it fail, write code, refactor" iterations.

Thanks for some input,

Chris

I would say first you need to do some research to find the bundles you will be using.For example you say that you'd rather start unit testing your user management, except in fSymfony you already have great user management bundles that already come with their test series. So i would say the first step when starting youre project is to find what already exists and would fit your needs. After abstracting the most you can it should be simpler to see where to start (usually writing tests to make sure all the bundles you are using work properly together).

Hope that helps you get started

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM