Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP front controller library with support for unit testing

I am looking for a (small) library that helps me cleanly implement a front controller for my pet project and dispatches requests to single controller classes. The front controller/dispatcher and controller classes need to be fully unittestable without sending HTTP requests.

Requirements

  • PSR-0 compatible
  • installable via its own PEAR channel
  • support for unit testing:
    • checking if the correct HTTP headers are sent
    • catches output to allow inspection in unit tests
    • perferably PHPUnit helper methods to help inspecting the output (for different output types, i.e. HTML, XML, JSON)
    • allows setting of incoming HTTP headers, GET and POST parameters and cookies without actually doing HTTP requests
  • needs to be usable standalone - without the db abstraction, templating and so that the fat frameworks all provide

Background

SemanticScuttle, the application that is bound to get proper "C" support, is an existing, working application. The library needs to blend in it and needs to work with the existing structure and classes. I won't rewrite it to match a framework's specific required directory layout.

The application already has unittests, but based on HTTP requests which make them slow. Also, the current old way of having several dozens of .php files in the www directory isn't the most managable solution, which is why proper controller classes need to be introduced. All in all, there will be about 20-30 controllers.

Previous experience

In general, I was pretty happy with Zend Framework for some previous projects but it has several drawbacks:

  • not pear-installable, so I cannot use it as dependency in my pear-installble applications
  • only available as one fat download, so I manually need to extract the required bits from it - for each single ZF update.
  • while unit test support exists for ZF controllers, it's lacking some advanced utility functionality like assertions for json, HTTP status code and content type checks.

While these points seem to be nit-picking, they are important for me. If I have to implement them myself, I do not need to use an external libary but write my own.

What I don't want

StackOverflow has a million "what's the best PHP framework" questions (1, 2, 3, 4, 5), but I'm not looking for those but for a specific library that helps with controllers. If it's part of a modular framework, fine.

I also know the PHP framework comparison website, but it doesn't help answer my question since my requirements are not listed there.

And I know that I can build this all on my own and invent another microframework. But why? There are so many of them already, and one just has to have all that I need.

Related questions

  • What's your 'no framework' PHP framework?
  • How do you convert a page-based PHP application to MVC?
like image 907
cweiske Avatar asked Jun 08 '11 06:06

cweiske


2 Answers

Knowing Symfony2 well, I can assure you it's definitely possible to use it just for the "C" in MVC. The models and templates are completely free and are typically executed from the Controllers anyway, so if you don't call Doctrine or Twig specifically, you can do what you want.

As for functional testing, which is really what you're talking about in your article, what you want to look at is the WebTestCase class, which is well complemented by the LiipFunctionalTestBundle bundle for more advanced cases.

That allows for some things like this example of testing a contact form that sends an email, where the entire HTTP request is done in process, since the framework is written to allow multiple requests per process and has no global state, this works very well and does not require a http server to be running or anything. As you can see I do assertions on the HTTP status code of the response too, and was able to capture the email without sending it since in test configuration sending of emails is disabled in the standard distro of Symfony2.

That being said, you could also just use the Request and Response classes from Symfony2's HttpFoundation component. It should allow you to test your code, but IMO you wouldn't get as many nice features as you could if you'd use the entire framework. Of course that's just my biased opinion ;)

like image 69
Seldaek Avatar answered Oct 22 '22 17:10

Seldaek


I would recommend downloading the Symfony 2 framework Routing component: https://github.com/symfony/Routing

Documentation is found here: http://symfony.com/doc/current/book/routing.html

Perhaps it does not satisfy all you requirements, but it's the closest.

like image 42
afilina Avatar answered Oct 22 '22 17:10

afilina