Sağlıksız Dünyamız

Page 113

The MockObject Pattern

Like application code, tests can also be refactored. In this case, you can see the output buffering trick is going to be required multiple times, so use the “Extract Method” refactoring to simplify the tests. (Recall that methods that begin with the word “test” are the ones that the test suite runs automatically; any other methods can be created to make your testing easier.) The next code block shows the result of the output buffering being refactored to the runPage method, as well as another test for the output generated when the user is logged in.

class PageDirectorTestCase extends UnitTestCase { // ... function TestLoggedOutContent() { $session =& new MockSession($this); $session->setReturnValue(‘get’, null, array(‘user_name’)); $session->expectOnce(‘get’, array(‘user_name’)); $page =& new PageDirector($session, new Response); $result = $this-> >runPage($page); $this->assertNoUnwantedPattern(‘/secret.*content/i’, $result); $this->assertWantedPattern(‘/<form.*<input[^>]*text[^>]*’ .’name.*<input[^>]*password[^>]*passwd/ims’ ,$result); $session->tally(); } function TestLoggedInContent() { $session =& new MockSession($this); $session->setReturnValue(‘get’, ‘admin’, array(‘user_name’)); $session->expectAtLeastOnce(‘get’); $page =& new PageDirector($session, new Response); $result = $this-> >runPage($page); $this->assertWantedPattern(‘/secret.*content/i’, $result); $this->assertNoUnwantedPattern(‘/<form.*<input[^>]*text[^>]*’ .’name.*<input[^>]*password[^>]*passwd/ims’ ,$result); $session->tally(); } function runPage(&$page) { ob_start(); $page-> >run(); return ob_get_clean(); } }

Next, add a conditional check to the PageDirector::run() method to see if the user has logged in and decide what template to display based on the result:

115


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.