Initial commit
This commit is contained in:
137
pluf/src/Pluf/Test/Client.php
Normal file
137
pluf/src/Pluf/Test/Client.php
Normal file
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* Emulates a client to call your views during unit testing.
|
||||
*
|
||||
* Usage:
|
||||
* <code>
|
||||
* $client = new Pluf_Test_Client('./path/to/app-views.php');
|
||||
* $response = $client->get('/the/page/', array('var'=>'toto'));
|
||||
* $response is now the Pluf_HTTP_Response
|
||||
* </code>
|
||||
*
|
||||
*/
|
||||
class Pluf_Test_Client
|
||||
{
|
||||
public $views = '';
|
||||
public $dispatcher = '';
|
||||
public $cookies = array();
|
||||
|
||||
public function __construct($views)
|
||||
{
|
||||
$this->views = $views;
|
||||
$this->dispatcher = new Pluf_Dispatcher();
|
||||
$this->dispatcher->loadControllers($this->views);
|
||||
$this->clean(false);
|
||||
}
|
||||
|
||||
protected function clean($keepcookies=true)
|
||||
{
|
||||
$_REQUEST = array();
|
||||
if (!$keepcookies) {
|
||||
$_COOKIE = array();
|
||||
$this->cookies = array();
|
||||
}
|
||||
$_SERVER = array();
|
||||
$_GET = array();
|
||||
$_POST = array();
|
||||
$_FILES = array();
|
||||
$_SERVER['REQUEST_METHOD'] = '';
|
||||
$_SERVER['REQUEST_URI'] = '';
|
||||
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
||||
$_SERVER['HTTP_HOST'] = 'localhost';
|
||||
}
|
||||
|
||||
protected function dispatch($page)
|
||||
{
|
||||
$GLOBALS['_PX_tests_templates'] = array();
|
||||
$_SERVER['REQUEST_URI'] = $page;
|
||||
foreach ($this->cookies as $cookie => $data) {
|
||||
$_COOKIE[$cookie] = $data;
|
||||
}
|
||||
ob_implicit_flush(False);
|
||||
list($request, $response) = $this->dispatcher->dispatch($page);
|
||||
ob_start();
|
||||
$response->render();
|
||||
$content = ob_get_contents();
|
||||
ob_end_clean();
|
||||
$response->content = $content;
|
||||
$response->request = $request;
|
||||
if (isset($GLOBALS['_PX_tests_templates'])) {
|
||||
if (count($GLOBALS['_PX_tests_templates']) == 1) {
|
||||
$response->template = $GLOBALS['_PX_tests_templates'][0];
|
||||
} else {
|
||||
$response->template = $GLOBALS['_PX_tests_templates'];
|
||||
}
|
||||
}
|
||||
foreach ($response->cookies as $cookie => $data) {
|
||||
$_COOKIE[$cookie] = $data;
|
||||
$this->cookies[$cookie] = $data;
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function get($page, $params=array())
|
||||
{
|
||||
$this->clean();
|
||||
$_GET = $params;
|
||||
$_REQUEST = $params;
|
||||
$_SERVER['REQUEST_METHOD'] = 'GET';
|
||||
$response = $this->dispatch($page);
|
||||
$code = $response->status_code;
|
||||
if ($code == 302) {
|
||||
list($page, $params) = $this->parseRedirect($response->headers['Location']);
|
||||
$response = $this->get($page, $params);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
public function post($page, $params=array(), $files=array())
|
||||
{
|
||||
$this->clean();
|
||||
$_POST = $params;
|
||||
$_REQUEST = $params;
|
||||
$_FILES = $files; //FIXME need to match the correct array structure
|
||||
$_SERVER['REQUEST_METHOD'] = 'POST';
|
||||
$response = $this->dispatch($page);
|
||||
if ($response->status_code == 302) {
|
||||
list($page, $params) = $this->parseRedirect($response->headers['Location']);
|
||||
return $this->get($page, $params);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function parseRedirect($location)
|
||||
{
|
||||
$page = parse_url($location, PHP_URL_PATH);
|
||||
$query = parse_url($location, PHP_URL_QUERY);
|
||||
$params = array();
|
||||
if (strlen($query)) {
|
||||
parse_str($query, $params);
|
||||
}
|
||||
return array($page, $params);
|
||||
}
|
||||
}
|
||||
|
113
pluf/src/Pluf/Test/Fixture.php
Normal file
113
pluf/src/Pluf/Test/Fixture.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
|
||||
/**
|
||||
* The fixture class is used to prepopulate your database with data.
|
||||
*
|
||||
* Load a fixture file in the database:
|
||||
* array = Pluf_Test_Fixture::load('fixturefile.json');
|
||||
*
|
||||
* Dump the data of a model as a fixture string:
|
||||
* $json = Pluf_Test_Fixture::dump('YourApp_Model'); // the full table
|
||||
* $json = Pluf_Test_Fixture::dump($model); // one given model
|
||||
*
|
||||
*/
|
||||
class Pluf_Test_Fixture
|
||||
{
|
||||
public static function loadFile($file)
|
||||
{
|
||||
if (false === ($ffile=Pluf::fileExists($file))) {
|
||||
throw new Exception(sprintf(__('Fixture file not found: %s.'), $file));
|
||||
}
|
||||
return self::load(file_get_contents($ffile));
|
||||
}
|
||||
|
||||
|
||||
public static function load($json, $deserialize=true)
|
||||
{
|
||||
$created = array();
|
||||
$data = ($deserialize) ? json_decode($json, true) : $json;
|
||||
unset($json);
|
||||
foreach ($data as $model) {
|
||||
if ((int)$model['pk'] > 0) {
|
||||
$item = new $model['model']($model['pk']);
|
||||
if ($item->id == $model['pk']) {
|
||||
throw new Exception(sprintf(__('Cannot load existing model <%1$s(%2$s)>.'), $model['model'], $model['pk']));
|
||||
}
|
||||
}
|
||||
$m = new $model['model']();
|
||||
$m->setFromFormData($model['fields']);
|
||||
$m->create(true); // we load in raw mode
|
||||
$created[] = array($model['model'], $model['pk']);
|
||||
}
|
||||
return $created;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a model or model name, dump the content.
|
||||
*
|
||||
* If the object is given, only this single object is dumped else
|
||||
* the complete table.
|
||||
*
|
||||
* @param mixed Model object or model name
|
||||
* @param bool Serialize as JSON (true)
|
||||
* @return mixed Array or JSON string
|
||||
*/
|
||||
public static function dump($model, $serialize=true)
|
||||
{
|
||||
if (is_object($model)) {
|
||||
return ($serialize) ?
|
||||
json_encode(array(self::prepare($model))) :
|
||||
array(self::prepare($model));
|
||||
}
|
||||
$out = array();
|
||||
foreach (Pluf::factory($model)->getList(array('order' =>'id ASC')) as $item) {
|
||||
$out[] = self::prepare($item);
|
||||
}
|
||||
return ($serialize) ? json_encode($out) : $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array, ready to be serialized as json.
|
||||
*/
|
||||
public static function prepare($model)
|
||||
{
|
||||
$out = array('model' => $model->_a['model'],
|
||||
'pk' => $model->id,
|
||||
'fields' => array());
|
||||
foreach ($model->_a['cols'] as $col=>$val) {
|
||||
$field = new $val['type']();
|
||||
if ($field->type != 'manytomany') {
|
||||
$out['fields'][$col] = $model->$col;
|
||||
} else {
|
||||
$func = 'get_'.$col.'_list';
|
||||
$out['fields'][$col] = array();
|
||||
foreach ($model->$func() as $item) {
|
||||
$out['fields'][$col][] = $item->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
}
|
91
pluf/src/Pluf/Test/TemplatetagsUnitTestCase.php
Normal file
91
pluf/src/Pluf/Test/TemplatetagsUnitTestCase.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2010 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
class Pluf_Test_TemplatetagsUnitTestCase extends UnitTestCase
|
||||
{
|
||||
/**
|
||||
* Tag class name like 'Pluf_Template_Tag_Cfg'.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $tag_class = null;
|
||||
|
||||
/**
|
||||
* Tag identifier like 'cfg'.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $tag_name = null;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected $tpl_folders;
|
||||
|
||||
function __construct($label = false)
|
||||
{
|
||||
$label = ($label) ? $label : 'Test the `'.$this->tag_name.'` template tag.';
|
||||
parent::__construct($label);
|
||||
|
||||
if (null === $this->tag_name) {
|
||||
throw new LogicException('You must initialize the `$tag_name` property.');
|
||||
}
|
||||
if (null === $this->tag_class) {
|
||||
throw new LogicException('You must initialize the `$tag_class` property.');
|
||||
}
|
||||
|
||||
$folder = Pluf::f('tmp_folder').'/templatetags';
|
||||
if (!file_exists($folder)) {
|
||||
mkdir($folder, 0777, true);
|
||||
}
|
||||
$this->tpl_folders = array($folder);
|
||||
|
||||
Pluf_Signal::connect('Pluf_Template_Compiler::construct_template_tags_modifiers',
|
||||
array($this, 'addTemplatetag'));
|
||||
}
|
||||
|
||||
public function addTemplatetag($signal, &$params)
|
||||
{
|
||||
$params['tags'] = array_merge($params['tags'],
|
||||
array($this->tag_name => $this->tag_class));
|
||||
}
|
||||
|
||||
protected function writeTemplateFile($tpl_name, $content)
|
||||
{
|
||||
$file = $this->tpl_folders[0].'/'.$tpl_name;
|
||||
if (file_exists($file)) {
|
||||
unlink($file);
|
||||
}
|
||||
file_put_contents($file, $content);
|
||||
}
|
||||
|
||||
protected function getNewTemplate($content = '')
|
||||
{
|
||||
$tpl_name = sprintf('%s-%s.html',
|
||||
get_class($this),
|
||||
md5($content.microtime(true)));
|
||||
$this->writeTemplateFile($tpl_name, $content);
|
||||
|
||||
return new Pluf_Template($tpl_name, $this->tpl_folders);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user