Skip to content

Instantly share code, notes, and snippets.

@ch3ric
Created May 16, 2013 18:56
Show Gist options
  • Save ch3ric/5594135 to your computer and use it in GitHub Desktop.
Save ch3ric/5594135 to your computer and use it in GitHub Desktop.
Test a symfony1.4 project with PHPUnit and Symfony2 components. These Symfony2 components are needed: Symfony/Component/DomCrawler & Symfony/Component/CssSelector
<?php
// test/bootstrap/autoload.php
namespace Composer\Autoload;
class ClassLoader
{
private $prefixes = array();
private $fallbackDirs = array();
public function getPrefixes()
{
return $this->prefixes;
}
public function getFallbackDirs()
{
return $this->fallbackDirs;
}
public function add($prefix, $paths)
{
if (!$prefix) {
$this->fallbackDirs = (array) $paths;
return;
}
if (isset($this->prefixes[$prefix])) {
$this->prefixes[$prefix] = array_merge($this->prefixes[$prefix], (array) $paths);
} else {
$this->prefixes[$prefix] = (array) $paths;
}
}
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}
public function loadClass($class)
{
if ($file = $this->findFile($class)) {
require $file;
return true;
}
}
public function findFile($class)
{
if ('\\' == $class[0]) {
$class = substr($class, 1);
}
if (false !== $pos = strrpos($class, '\\')) {
$classPath = DIRECTORY_SEPARATOR
. str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos));
$className = substr($class, $pos + 1);
} else {
$classPath = null;
$className = $class;
}
$classPath .= DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
foreach ($this->prefixes as $prefix => $dirs) {
foreach ($dirs as $dir) {
if (0 === strpos($class, $prefix)) {
if (file_exists($dir . $classPath)) {
return $dir . $classPath;
}
}
}
}
foreach ($this->fallbackDirs as $dir) {
if (file_exists($dir . $classPath)) {
return $dir . $classPath;
}
}
}
}
$__composer_autoload_init = function ()
{
$loader = new \Composer\Autoload\ClassLoader();
$map = array(
'Symfony\\Component\\DomCrawler' => __DIR__ . '/../../lib/vendor/',
'Symfony\\Component\\CssSelector' => __DIR__ . '/../../lib/vendor/',
);
foreach ($map as $namespace => $path) {
$loader->add($namespace, $path);
}
$loader->register();
return $loader;
};
return $__composer_autoload_init();
<?php
// test/phpunit/ExampleTest.php
include(__DIR__ . "/../../lib/test/sfWebTestCase.class.php");
use Symfony\Component\DomCrawler\Crawler;
class ExampleTest extends sfWebTestCase
{
public function testCreate()
{
$client = $this->createClient();
$crawler = $client->get('/example/url');
$this->assertEquals("Blablabla", $crawler->filter('.title')->text());
}
}
<?php
// test/boostrap/functional.php
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
// guess current application
if (!isset($app))
{
$traces = debug_backtrace();
$caller = $traces[0];
$dirPieces = explode(DIRECTORY_SEPARATOR, dirname($caller['file']));
$app = array_pop($dirPieces);
}
require_once dirname(__FILE__).'/../../config/ProjectConfiguration.class.php';
$configuration = ProjectConfiguration::getApplicationConfiguration($app, 'test', isset($debug) ? $debug : true);
sfContext::createInstance($configuration);
// remove all cache
sfToolkit::clearDirectory(sfConfig::get('sf_app_cache_dir'));
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="true"
bootstrap="test/bootstrap/autoload.php"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
forceCoversAnnotation="false"
mapTestClassNameToCoveredClassName="false"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
syntaxCheck="false"
testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader"
strict="false"
verbose="false">
<testsuites>
<testsuite name="Main tests">
<directory >test/phpunit</directory>
</testsuite>
</testsuites>
<filter>
<blacklist>
<directory suffix=".php">test</directory>
<directory suffix=".php">src</directory>
</blacklist>
</filter>
</phpunit>
<?php
// lib/test/sfPHPUnitBrowser.php
use Symfony\Component\DomCrawler\Crawler,
Symfony\Component\DomCrawler\Form;
class sfPHPUnitBrowser extends sfBrowser
{
/**
* {@inheritdoc}
* @see sfBrowserBase::call()
*/
public function call($uri, $method = 'get', $parameters = array(), $changeStack = true)
{
$browser = parent::call($uri, $method, $parameters, $changeStack);
$uri = $this->fixUri($uri);
$uriPrefix = 'http://' . sfContext::getInstance()->getRequest()->getHost() . '/';
$crawler = new Crawler(null, $uriPrefix . $uri);
$crawler->addContent($browser->getResponse()->getContent(), $browser->getResponse()->getContentType());
return $crawler;
}
/**
* Post the form values
*
* @param Form $form
*/
public function submit(Form $form)
{
return $this->post($form->getUri(), $form->getPhpValues());
}
}
<?php
// lib/test/sfWebTestCase.php
abstract class sfWebTestCase extends PHPUnit_Framework_TestCase
{
/**
* @return sfPHPUnitBrowser
*/
protected function createClient()
{
$app = $this->getApplication();
include(dirname(__FILE__).'/../../test/bootstrap/functional.php');
return new sfPHPUnitBrowser();
}
/**
* @return string
*/
protected function getApplication()
{
return 'frontend';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment