Skip to content

Commit

Permalink
better filter method behavior, handling mismatching endblocks, improv…
Browse files Browse the repository at this point in the history
…ed makefile with automatic testing
  • Loading branch information
arshaw committed Mar 28, 2010
1 parent df2dac1 commit 486b0fa
Show file tree
Hide file tree
Showing 28 changed files with 285 additions and 36 deletions.
14 changes: 12 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,26 @@ DATE_SED = sed s/@DATE/"${DATE}"/

zip:
@rm -rf build/*

@echo "building..."
@mkdir -p build
@mkdir -p dist
@cat src/ti.php \
| ${VER_SED} \
| ${DATE_SED} \
| sed /require.*debug\.php/d \
> build/ti.php
@cp readme.mkd build/readme.txt
@cp license.txt build
@cd build; zip -q phpti.zip *
@mv build/phpti.zip dist/phpti-${VER}.zip

@echo "running tests..."
@if tests/run-tests -B; then \
echo "zipping..."; \
cd build; zip -q phpti.zip *; cd ..; \
mv build/phpti.zip dist/phpti-${VER}.zip; \
else \
echo "DID NOT PASS ALL TESTS."; \
fi

clean:
@rm -rf build/*
Expand Down
21 changes: 9 additions & 12 deletions src/ti.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
Released: @DATE
Website: http://phpti.com/
Author: Adam Shaw (http://arshaw.com/)
Licensed under the MIT License (license.txt)
Released under the MIT License (license.txt)
*/


//require_once dirname(__FILE__) . '/debug.php';
require_once dirname(__FILE__) . '/debug.php'; // line will be removed by Makefile


$GLOBALS['_ti_base'] = null;
Expand Down Expand Up @@ -44,10 +42,9 @@ function endblock($name=null) {
if ($stack) {
$block = array_pop($stack);
if ($name && $name != $block['name']) {
_ti_warning("orphan endblock('$name')", $trace);
}else{
_ti_insertBlock($block);
_ti_warning("startblock('{$block['name']}') does not match endblock('$name')", $trace);
}
_ti_insertBlock($block);
}else{
_ti_warning(
$name ? "orphan endblock('$name')" : "orphan endblock()",
Expand Down Expand Up @@ -114,9 +111,7 @@ function flushblocks() {


function blockbase() {
_ti_init(
_ti_callingTrace()
);
_ti_init(_ti_callingTrace());
}


Expand Down Expand Up @@ -170,9 +165,11 @@ function _ti_newBlock($name, $filters, $trace) {
$filters = array($filters);
}
foreach ($filters as $i => $f) {
if ($f && is_string($f) && !function_exists($f)) {
if ($f && !is_callable($f)) {
_ti_warning(
"filter '$f' is not defined",
is_array($f) ?
"filter " . implode('::', $f) . " is not defined":
"filter '$f' is not defined", // TODO: better messaging for methods
$trace
);
$filters[$i] = null;
Expand Down
5 changes: 0 additions & 5 deletions tests/TODO.txt

This file was deleted.

2 changes: 1 addition & 1 deletion tests/after_content.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Output outside of template, after template, gets sent to standard out

<?php

require_once '../src/ti.php';
require_once 'ti.php';

include 'templates/page1.php';

Expand Down
2 changes: 1 addition & 1 deletion tests/base_only.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Make sure a single base template file handles the buffer correctly

--FILE--

<? require_once '../src/ti.php' ?>
<? require_once 'ti.php' ?>
<html>
<head>
<? startblock('head') ?>
Expand Down
2 changes: 1 addition & 1 deletion tests/block_name_conflict.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Cannot have two blocks with same name in one file
--FILE--

<?
require_once '../src/ti.php';
require_once 'ti.php';
include 'templates/base.php';
?>

Expand Down
23 changes: 23 additions & 0 deletions tests/blockbase.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--

Make sure blockbase works on a base template that has no blocks

--FILE--

<? require_once 'ti.php' ?>
<? include 'templates/base_no_blocks.php' ?>

<? startblock('content') ?>
this is the content
<? endblock() ?>

--EXPECT--

<html>
<head>
<link rel='stylesheet' type='text/css' href='main.css' />
<script type='text/javascript' src='main.js'></script>
</head>
<body>
</body>
</html>
2 changes: 1 addition & 1 deletion tests/double_different.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ two consecutive different templates

--FILE--

<? require_once '../src/ti.php' ?>
<? require_once 'ti.php' ?>
<? include 'templates/page1.php' ?>
<? include 'templates/page2.php' ?>

Expand Down
2 changes: 1 addition & 1 deletion tests/double_same.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ two consecutive same templates

--FILE--

<? require_once '../src/ti.php' ?>
<? require_once 'ti.php' ?>
<? include 'templates/page1.php' ?>
<? include 'templates/page1.php' ?>

Expand Down
42 changes: 42 additions & 0 deletions tests/endblock.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--

Enblock with the optional name parameter

--FILE--

<? require_once 'ti.php' ?>
<? include 'templates/base.php' ?>

<? startblock('content') ?>
<p>
the page content
</p>
<? endblock('content') ?>

<? startblock('title') ?>
the page title
<? endblock('title') ?>

--EXPECT--

<html>
<head>
<link rel='stylesheet' type='text/css' href='main.css' />
<script type='text/javascript' src='main.js'></script>
</head>
<body>
<div id='header'>
<h1>
the page title
</h1>
</div>
<div id='content'>
<p>
the page content
</p>
</div>
<div id='footer'>
this is the default footer
</div>
</body>
</html>
44 changes: 44 additions & 0 deletions tests/endblock_mismatch.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--

Enblock with the optional name parameter

--FILE--

<? require_once 'ti.php' ?>
<? include 'templates/base.php' ?>

<? startblock('content') ?>
<p>
the page content
</p>
<? endblock('xxx') ?>

<? startblock('title') ?>
the page title
<? endblock('title') ?>

--EXPECTF--

<html>
<head>
<link rel='stylesheet' type='text/css' href='main.css' />
<script type='text/javascript' src='main.js'></script>
</head>
<body>
<div id='header'>
<h1>
the page title
</h1>
</div>
<div id='content'>
<p>
the page content
</p>
</div>
<div id='footer'>
this is the default footer
</div>
</body>
</html>

Warning: startblock('content') does not match endblock('xxx') in %s/endblock_mismatch.php on line 9
2 changes: 1 addition & 1 deletion tests/filters.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test the filter argument of startblock, with filters as a string and array
--FILE--

<?
require_once '../src/ti.php';
require_once 'ti.php';
include 'templates/base.php';

// TODO: test for commas and spaces in filter string
Expand Down
2 changes: 1 addition & 1 deletion tests/filters_bad.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Warns and does not execute filters that do not exist

<?php

require_once '../src/ti.php';
require_once 'ti.php';
include 'templates/base.php';

function filter1($s) {
Expand Down
2 changes: 1 addition & 1 deletion tests/filters_bad_base.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Warning for nonexistant filter happens where block is defined

--FILE--

<? require '../src/ti.php' ?>
<? require 'ti.php' ?>
<html>
<head>
<? startblock('head') ?>
Expand Down
44 changes: 44 additions & 0 deletions tests/filters_class.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--

Filters work with the array('class','method')

--FILE--

<?

require_once 'ti.php';
include 'templates/base.php';

class MyFilters {
function filter1($s) {
return '*' . trim($s) . '*';
}
}

?>

<? startblock('content', array(array('MyFilters','filter1'), array('Nothing','method'))) ?>
this is the content
<? endblock() ?>

--EXPECTF--

<html>
<head>
<link rel='stylesheet' type='text/css' href='main.css' />
<script type='text/javascript' src='main.js'></script>
</head>
<body>
<div id='header'>
<h1>
</h1>
</div>
<div id='content'>
*this is the content*</div>
<div id='footer'>
this is the default footer
</div>
</body>
</html>

Warning: filter Nothing::method is not defined in /home/adam/Projects/PHPTI/tests/filters_class.php on line 15
2 changes: 1 addition & 1 deletion tests/filters_closure.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if (PHP_MAJOR_VERSION < 5 || PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 3) {
--FILE--

<?php
require_once '../src/ti.php';
require_once 'ti.php';
include 'templates/base.php';

$filter1 = function($s) {
Expand Down
2 changes: 1 addition & 1 deletion tests/flushblocks_alone.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ flushblocks can be called when there are no blocks, just won't do anything
--FILE--

<?
require_once '../src/ti.php';
require_once 'ti.php';
echo "here1";
flushblocks();
echo "here2";
Expand Down
2 changes: 1 addition & 1 deletion tests/getsuperblock.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ getsuperblock function

--FILE--

<? require_once '../src/ti.php' ?>
<? require_once 'ti.php' ?>
<? include 'templates/base.php' ?>

<? startblock('footer') ?>
Expand Down
2 changes: 1 addition & 1 deletion tests/inheritance.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Basic inheritance. Notice block order doesn't matter.

--FILE--

<? require_once '../src/ti.php' ?>
<? require_once 'ti.php' ?>
<? include 'templates/base.php' ?>

<? startblock('content') ?>
Expand Down
2 changes: 1 addition & 1 deletion tests/inheritance_extra.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extra block

--FILE--

<? require_once '../src/ti.php' ?>
<? require_once 'ti.php' ?>
<? include 'templates/base.php' ?>

<? startblock('content') ?>
Expand Down
2 changes: 1 addition & 1 deletion tests/mismatch_base.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ orphan endblock and dangling startblock in a base template
--FILE--

<? error_reporting(E_ALL) ?>
<? require_once '../src/ti.php' ?>
<? require_once 'ti.php' ?>
<? include 'templates/base_broken.php' ?>

<? startblock('content') ?>
Expand Down
2 changes: 1 addition & 1 deletion tests/mismatch_child.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ throws a warning when omitted end tag in child template
--FILE--

<? error_reporting(E_ALL) ?>
<? require_once '../src/ti.php'?>
<? require_once 'ti.php'?>
<? include 'templates/base.php' ?>

<? endblock() ?>
Expand Down
2 changes: 1 addition & 1 deletion tests/nested.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ nested blocks

--FILE--

<? require_once '../src/ti.php' ?>
<? require_once 'ti.php' ?>
<? include 'templates/3col.php' ?>

<? startblock('left') ?>
Expand Down
Loading

0 comments on commit 486b0fa

Please sign in to comment.