PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/pluf-v1.0/src/Pluf/Test/Client.php

#
PHP | 137 lines | 96 code | 9 blank | 32 comment | 10 complexity | 4775b4e749a361923d1efd6063297131 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  3. /*
  4. # ***** BEGIN LICENSE BLOCK *****
  5. # This file is part of Plume Framework, a simple PHP Application Framework.
  6. # Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
  7. #
  8. # Plume Framework is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # Plume Framework is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  21. #
  22. # ***** END LICENSE BLOCK ***** */
  23. /**
  24. * Emulates a client to call your views during unit testing.
  25. *
  26. * Usage:
  27. * <code>
  28. * $client = new Pluf_Test_Client('./path/to/app-views.php');
  29. * $response = $client->get('/the/page/', array('var'=>'toto'));
  30. * $response is now the Pluf_HTTP_Response
  31. * </code>
  32. *
  33. */
  34. class Pluf_Test_Client
  35. {
  36. public $views = '';
  37. public $dispatcher = '';
  38. public $cookies = array();
  39. public function __construct($views)
  40. {
  41. $this->views = $views;
  42. $this->dispatcher = new Pluf_Dispatcher();
  43. $this->dispatcher->loadControllers($this->views);
  44. $this->clean(false);
  45. }
  46. protected function clean($keepcookies=true)
  47. {
  48. $_REQUEST = array();
  49. if (!$keepcookies) {
  50. $_COOKIE = array();
  51. $this->cookies = array();
  52. }
  53. $_SERVER = array();
  54. $_GET = array();
  55. $_POST = array();
  56. $_FILES = array();
  57. $_SERVER['REQUEST_METHOD'] = '';
  58. $_SERVER['REQUEST_URI'] = '';
  59. $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  60. $_SERVER['HTTP_HOST'] = 'localhost';
  61. }
  62. protected function dispatch($page)
  63. {
  64. $GLOBALS['_PX_tests_templates'] = array();
  65. $_SERVER['REQUEST_URI'] = $page;
  66. foreach ($this->cookies as $cookie => $data) {
  67. $_COOKIE[$cookie] = $data;
  68. }
  69. ob_implicit_flush(False);
  70. list($request, $response) = $this->dispatcher->dispatch($page);
  71. ob_start();
  72. $response->render();
  73. $content = ob_get_contents();
  74. ob_end_clean();
  75. $response->content = $content;
  76. $response->request = $request;
  77. if (isset($GLOBALS['_PX_tests_templates'])) {
  78. if (count($GLOBALS['_PX_tests_templates']) == 1) {
  79. $response->template = $GLOBALS['_PX_tests_templates'][0];
  80. } else {
  81. $response->template = $GLOBALS['_PX_tests_templates'];
  82. }
  83. }
  84. foreach ($response->cookies as $cookie => $data) {
  85. $_COOKIE[$cookie] = $data;
  86. $this->cookies[$cookie] = $data;
  87. }
  88. return $response;
  89. }
  90. public function get($page, $params=array())
  91. {
  92. $this->clean();
  93. $_GET = $params;
  94. $_REQUEST = $params;
  95. $_SERVER['REQUEST_METHOD'] = 'GET';
  96. $response = $this->dispatch($page);
  97. $code = $response->status_code;
  98. if ($code == 302) {
  99. list($page, $params) = $this->parseRedirect($response->headers['Location']);
  100. $response = $this->get($page, $params);
  101. }
  102. return $response;
  103. }
  104. public function post($page, $params=array(), $files=array())
  105. {
  106. $this->clean();
  107. $_POST = $params;
  108. $_REQUEST = $params;
  109. $_FILES = $files; //FIXME need to match the correct array structure
  110. $_SERVER['REQUEST_METHOD'] = 'POST';
  111. $response = $this->dispatch($page);
  112. if ($response->status_code == 302) {
  113. list($page, $params) = $this->parseRedirect($response->headers['Location']);
  114. return $this->get($page, $params);
  115. }
  116. return $response;
  117. }
  118. public function parseRedirect($location)
  119. {
  120. $page = parse_url($location, PHP_URL_PATH);
  121. $query = parse_url($location, PHP_URL_QUERY);
  122. $params = array();
  123. if (strlen($query)) {
  124. parse_str($query, $params);
  125. }
  126. return array($page, $params);
  127. }
  128. }