-
Notifications
You must be signed in to change notification settings - Fork 4
/
HumbleObjectTest.php
42 lines (41 loc) · 1.46 KB
/
HumbleObjectTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
/**
* How do we test this controller? We have to instantiate it, using a
* constructor designed for the framework, and inject a lot of things like a
* bootstrap object, which is a bit complex to build. Or we can mock it,
* but it has lots of methods.
* In fact, we can only do so with other goodies from the
* framework, in our case Zend_Test. But the test will be slow since it will
* be and end-to-end one.
*/
class PostController // extends Zend_Controller_Action
{
public function indexAction()
{
$connection = $this->bootstrap->getResource('connection');
$stmt = $connection->query('SELECT * FROM posts')->execute();
$posts = array();
foreach ($stmt->fetchAll() as $row) {
$posts[] = $row;
}
// pass $posts to the view...
}
}
/**
* We separate the logic in an Humble Object (the controller) and the real
* object which performs the work.
* We can now test PostsDao in isolation, while the Humble Object short code
* will be tested by very few end-to-end tests.
* I don't show PostsDao implementation here for brevity reasons and because
* it's really simple to grasp what goes inside: the PDO usage.
*/
class PostController // extends Zend_Controller_Action
{
public function indexAction()
{
$connection = $this->bootstrap->getResource('connection');
$postsDao = new PostsDao($connection);
$posts = $postsDao->findAll();
// pass $posts to the view...
}
}