Skip to content

Commit

Permalink
Merge pull request #418 from halkyon/phpunit_fixes
Browse files Browse the repository at this point in the history
Fixing phpunit.xml support, remove PHPUnit 3.4 support
  • Loading branch information
Stig Lindqvist committed May 10, 2012
2 parents f546ab2 + a1d676d commit c05e80c
Show file tree
Hide file tree
Showing 25 changed files with 50 additions and 182 deletions.
6 changes: 3 additions & 3 deletions admin/tests/CMSMenuTest.php
Expand Up @@ -16,7 +16,7 @@ public function testBasicMenuHandling() {
CMSMenu::add_controller('CMSMenuTest_LeftAndMainController');
$menuItems = CMSMenu::get_menu_items();
$menuItem = $menuItems['CMSMenuTest_LeftAndMainController'];
$this->assertType('CMSMenuItem', $menuItem, 'Controller menu item is of class CMSMenuItem');
$this->assertInstanceOf('CMSMenuItem', $menuItem, 'Controller menu item is of class CMSMenuItem');
$this->assertEquals($menuItem->url, singleton('CMSMenuTest_LeftAndMainController')->Link(), 'Controller menu item has the correct link');
$this->assertEquals($menuItem->controller, 'CMSMenuTest_LeftAndMainController', 'Controller menu item has the correct controller class');
$this->assertEquals($menuItem->priority, singleton('CMSMenuTest_LeftAndMainController')->stat('menu_priority'), 'Controller menu item has the correct priority');
Expand All @@ -26,7 +26,7 @@ public function testBasicMenuHandling() {
CMSMenu::add_link('LinkCode', 'link title', 'http://www.example.com');
$menuItems = CMSMenu::get_menu_items();
$menuItem = $menuItems['LinkCode'];
$this->assertType('CMSMenuItem', $menuItem, 'Link menu item is of class CMSMenuItem');
$this->assertInstanceOf('CMSMenuItem', $menuItem, 'Link menu item is of class CMSMenuItem');
$this->assertEquals($menuItem->title, 'link title', 'Link menu item has the correct title');
$this->assertEquals($menuItem->url,'http://www.example.com', 'Link menu item has the correct link');
$this->assertNull($menuItem->controller, 'Link menu item has no controller class');
Expand All @@ -53,7 +53,7 @@ public function testAdvancedMenuHandling() {
CMSMenu::clear_menu();
CMSMenu::populate_menu();
$menuItem = CMSMenu::get_menu_item('SecurityAdmin');
$this->assertType('CMSMenuItem', $menuItem, 'SecurityAdmin menu item exists');
$this->assertInstanceOf('CMSMenuItem', $menuItem, 'SecurityAdmin menu item exists');
$this->assertEquals($menuItem->url, singleton('SecurityAdmin')->Link(), 'Menu item has the correct link');
$this->assertEquals($menuItem->controller, 'SecurityAdmin', 'Menu item has the correct controller class');
$this->assertEquals(
Expand Down
2 changes: 1 addition & 1 deletion admin/tests/LeftAndMainTest.php
Expand Up @@ -98,7 +98,7 @@ public function testLeftAndMainSubclasses() {

$response = $this->get($link);

$this->assertType('SS_HTTPResponse', $response, "$link should return a response object");
$this->assertInstanceOf('SS_HTTPResponse', $response, "$link should return a response object");
$this->assertEquals(200, $response->getStatusCode(), "$link should return 200 status code");
// Check that a HTML page has been returned
$this->assertRegExp('/<html[^>]*>/i', $response->getBody(), "$link should contain <html> tag");
Expand Down
55 changes: 0 additions & 55 deletions dev/SapphireTest.php
Expand Up @@ -652,61 +652,6 @@ function assertDOSAllMatch($match, $dataObjectSet) {
}
}

/**
* Backported from PHPUnit 3.4 in order to maintain backwards
* compatibility: assertType() is deprecated in PHPUnit 3.5 (with PHP 5.2.7+),
* but as SilverStripe 2.3 and 2.4 support PHP 5.1 we can't require it.
*/
public static function assertType($expected, $actual, $message = '') {
// PHPUnit_Util_DeprecatedFeature_Logger::log(
// 'assertType() will be removed in PHPUnit 3.6 and should no longer ' .
// 'be used. assertInternalType() should be used for asserting ' .
// 'internal types such as "integer" or "string" whereas ' .
// 'assertInstanceOf() should be used for asserting that an object is ' .
// 'an instance of a specified class or interface.'
// );

if (is_string($expected)) {
if (PHPUnit_Util_Type::isType($expected)) {
$constraint = new PHPUnit_Framework_Constraint_IsType(
$expected
);
}

else if (class_exists($expected) || interface_exists($expected)) {
$constraint = new PHPUnit_Framework_Constraint_IsInstanceOf(
$expected
);
}

else {
throw PHPUnit_Util_InvalidArgumentHelper::factory(
1, 'class or interface name'
);
}
} else {
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
}

self::assertThat($actual, $constraint, $message);
}

/**
* Provide assertEmpty() in PHPUnit <3.5.
* We want to support PHPUnit 3.4, as this is the most recent release available
* to environments running PHP <=5.2.6, such as Debian Lenny.
*/
public static function assertEmpty($item, $message = '') {
if(class_exists('PHPUnit_Framework_Constraint_IsEmpty')) {
parent::assertEmpty($item, $message);
} else {
if(!empty($item)) {
$message = $message ? $message : "Failed asserting that " . var_export($item, true) . " is empty.";
throw new PHPUnit_Framework_AssertionFailedError($message);
}
}
}

/**
* Helper function for the DOS matchers
*/
Expand Down
2 changes: 1 addition & 1 deletion main.php
Expand Up @@ -96,7 +96,7 @@
}

// Connect to database
require_once("model/DB.php");
require_once('model/DB.php');

// Redirect to the installer if no database is selected
if(!isset($databaseConfig) || !isset($databaseConfig['database']) || !$databaseConfig['database']) {
Expand Down
83 changes: 0 additions & 83 deletions tests/FullTestSuite.php

This file was deleted.

10 changes: 9 additions & 1 deletion tests/bootstrap.php
Expand Up @@ -5,6 +5,12 @@
// Make sure display_errors is on
ini_set('display_errors', 1);

// Check we're using at least PHPUnit 3.6
if(version_compare(PHPUnit_Runner_Version::id(), '3.6', '<')) {
echo 'PHPUnit 3.6 required to run tests using bootstrap.php';
die();
}

// Fake the script name and base
global $_SERVER;
if (!$_SERVER) $_SERVER = array();
Expand Down Expand Up @@ -37,7 +43,9 @@
$_GET['flush'] = 1;

// Connect to database
require_once($frameworkPath . "/core/Core.php");
require_once $frameworkPath . '/core/Core.php';
require_once $frameworkPath . '/tests/FakeController.php';

global $databaseConfig;
DB::connect($databaseConfig);

Expand Down
2 changes: 1 addition & 1 deletion tests/control/DirectorTest.php
Expand Up @@ -165,7 +165,7 @@ public function testTestRequestCarriesGlobals() {
$url = 'DirectorTestRequest_Controller/' . sprintf($testfunction, ucfirst($method)) . '?' . http_build_query($fixture);
$getresponse = Director::test($url, $fixture, null, strtoupper($method), null, null, $fixture);

$this->assertType('SS_HTTPResponse', $getresponse, 'Director::test() returns SS_HTTPResponse');
$this->assertInstanceOf('SS_HTTPResponse', $getresponse, 'Director::test() returns SS_HTTPResponse');
$this->assertEquals($fixture['somekey'], $getresponse->getBody(), 'Director::test() ' . $testfunction);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/control/RequestHandlingTest.php
Expand Up @@ -22,7 +22,7 @@ class RequestHandlingTest extends FunctionalTest {

function testConstructedWithNullRequest() {
$r = new RequestHandler();
$this->assertType('NullHTTPRequest', $r->getRequest());
$this->assertInstanceOf('NullHTTPRequest', $r->getRequest());
}

function testRequestHandlerChainingAllParams() {
Expand Down
8 changes: 4 additions & 4 deletions tests/core/ObjectTest.php
Expand Up @@ -165,19 +165,19 @@ public function testGetExtensions() {
$inst = new ObjectTest_ExtensionTest();
$extensions = $inst->getExtensionInstances();
$this->assertEquals(count($extensions), 2);
$this->assertType(
$this->assertInstanceOf(
'ObjectTest_ExtendTest1',
$extensions['ObjectTest_ExtendTest1']
);
$this->assertType(
$this->assertInstanceOf(
'ObjectTest_ExtendTest2',
$extensions['ObjectTest_ExtendTest2']
);
$this->assertType(
$this->assertInstanceOf(
'ObjectTest_ExtendTest1',
$inst->getExtensionInstance('ObjectTest_ExtendTest1')
);
$this->assertType(
$this->assertInstanceOf(
'ObjectTest_ExtendTest2',
$inst->getExtensionInstance('ObjectTest_ExtendTest2')
);
Expand Down
4 changes: 1 addition & 3 deletions tests/dev/LogTest.php
Expand Up @@ -26,7 +26,6 @@ function testExistingWriter() {
SS_Log::add_writer($testFileWriter, SS_Log::WARN);

$writers = SS_Log::get_writers();
$this->assertType('array', $writers);
$this->assertEquals(2, count($writers));
}

Expand All @@ -38,12 +37,11 @@ function testRemoveWriter() {

SS_Log::remove_writer($testEmailWriter);
$writers = SS_Log::get_writers();
$this->assertType('array', $writers);

$this->assertEquals(1, count($writers));

SS_Log::remove_writer($testFileWriter);
$writers = SS_Log::get_writers();
$this->assertType('array', $writers);
$this->assertEquals(0, count($writers));
}

Expand Down
4 changes: 2 additions & 2 deletions tests/filesystem/FileTest.php
Expand Up @@ -61,10 +61,10 @@ function testCreateWithFilenameWithSubfolder() {
$this->assertEquals($testfilePath, $file->Filename, '"Filename" property remains unchanged');

// TODO This should be auto-detected, see File->updateFilesystem()
// $this->assertType('Folder', $file->Parent(), 'Parent folder is created in database');
// $this->assertInstanceOf('Folder', $file->Parent(), 'Parent folder is created in database');
// $this->assertFileExists($file->Parent()->getFullPath(), 'Parent folder is created on filesystem');
// $this->assertEquals('FileTest', $file->Parent()->Name);
// $this->assertType('Folder', $file->Parent()->Parent(), 'Grandparent folder is created in database');
// $this->assertInstanceOf('Folder', $file->Parent()->Parent(), 'Grandparent folder is created in database');
// $this->assertFileExists($file->Parent()->Parent()->getFullPath(), 'Grandparent folder is created on filesystem');
// $this->assertEquals('assets', $file->Parent()->Parent()->Name);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/forms/FormTest.php
Expand Up @@ -241,12 +241,12 @@ function testGloballyDisabledSecurityTokenInheritsToNewForm() {
SecurityToken::enable();

$form1 = $this->getStubForm();
$this->assertType('SecurityToken', $form1->getSecurityToken());
$this->assertInstanceOf('SecurityToken', $form1->getSecurityToken());

SecurityToken::disable();

$form2 = $this->getStubForm();
$this->assertType('NullSecurityToken', $form2->getSecurityToken());
$this->assertInstanceOf('NullSecurityToken', $form2->getSecurityToken());

SecurityToken::enable();
}
Expand All @@ -255,7 +255,7 @@ function testDisableSecurityTokenDoesntAddTokenFormField() {
SecurityToken::enable();

$formWithToken = $this->getStubForm();
$this->assertType(
$this->assertInstanceOf(
'HiddenField',
$formWithToken->Fields()->fieldByName(SecurityToken::get_default_name()),
'Token field added by default'
Expand Down
2 changes: 1 addition & 1 deletion tests/forms/gridfield/GridFieldConfigTest.php
Expand Up @@ -8,7 +8,7 @@ class GridFieldConfigTest extends SapphireTest {

function testGetComponents() {
$config = GridFieldConfig::create();
$this->assertType('ArrayList', $config->getComponents());
$this->assertInstanceOf('ArrayList', $config->getComponents());
$this->assertEquals($config->getComponents()->Count(), 0);

$config
Expand Down
4 changes: 2 additions & 2 deletions tests/i18n/i18nTest.php
Expand Up @@ -441,8 +441,8 @@ function testRegisterTranslator() {
i18n::register_translator($translator, 'custom', 10);
$translators = i18n::get_translators();
$this->assertArrayHasKey('custom', $translators[10]);
$this->assertType('Zend_Translate', $translators[10]['custom']);
$this->assertType('i18nTest_CustomTranslatorAdapter', $translators[10]['custom']->getAdapter());
$this->assertInstanceOf('Zend_Translate', $translators[10]['custom']);
$this->assertInstanceOf('i18nTest_CustomTranslatorAdapter', $translators[10]['custom']->getAdapter());

i18n::unregister_translator('custom');
$translators = i18n::get_translators();
Expand Down
8 changes: 4 additions & 4 deletions tests/model/DataExtensionTest.php
Expand Up @@ -41,10 +41,10 @@ function testOneToManyAssociationWithExtension() {
$object = DataObject::get_one('DataExtensionTest_RelatedObject', "\"ContactID\" = {$contactID}");

$this->assertNotNull($object, 'Related object not null');
$this->assertType('DataExtensionTest_Member', $object->Contact(), 'Related contact is a member dataobject');
$this->assertType('DataExtensionTest_Member', $object->getComponent('Contact'), 'getComponent does the same thing as Contact()');
$this->assertInstanceOf('DataExtensionTest_Member', $object->Contact(), 'Related contact is a member dataobject');
$this->assertInstanceOf('DataExtensionTest_Member', $object->getComponent('Contact'), 'getComponent does the same thing as Contact()');

$this->assertType('DataExtensionTest_RelatedObject', $contact->RelatedObjects()->First());
$this->assertInstanceOf('DataExtensionTest_RelatedObject', $contact->RelatedObjects()->First());
$this->assertEquals("Lorem ipsum dolor", $contact->RelatedObjects()->First()->FieldOne);
$this->assertEquals("Random notes", $contact->RelatedObjects()->First()->FieldTwo);
$contact->delete();
Expand Down Expand Up @@ -143,7 +143,7 @@ function testPopulateDefaults() {
function testDbObjectOnExtendedFields() {
$member = $this->objFromFixture('DataExtensionTest_Member', 'member1');
$this->assertNotNull($member->dbObject('Website'));
$this->assertType('Varchar', $member->dbObject('Website'));
$this->assertInstanceOf('Varchar', $member->dbObject('Website'));
}

function testExtensionCanBeAppliedToDataObject() {
Expand Down
2 changes: 1 addition & 1 deletion tests/model/DataListTest.php
Expand Up @@ -161,7 +161,7 @@ function testByID() {
$team = DataList::create("DataObjectTest_Team")->byID($id);

// byID() returns a DataObject, rather than a DataList
$this->assertType('DataObjectTest_Team', $team);
$this->assertInstanceOf('DataObjectTest_Team', $team);
$this->assertEquals('Team 2', $team->Title);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/model/DataObjectLazyLoadingTest.php
Expand Up @@ -187,7 +187,7 @@ function testLazyLoadedFieldsHasOneRelation() {
$subteam1Lazy = $teams->find('ID', $subteam1->ID);

$parentTeamLazy = $subteam1Lazy->ParentTeam();
$this->assertType('DataObjectTest_Team', $parentTeamLazy);
$this->assertInstanceOf('DataObjectTest_Team', $parentTeamLazy);
$this->assertEquals($parentTeam->ID, $parentTeamLazy->ID);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/model/DataObjectTest.php
Expand Up @@ -719,15 +719,15 @@ function testNewClassInstance() {
$changedFields = $changedDO->getChangedFields();

// Don't write the record, it will reset changed fields
$this->assertType('DataObjectTest_SubTeam', $changedDO);
$this->assertInstanceOf('DataObjectTest_SubTeam', $changedDO);
$this->assertEquals($changedDO->ClassName, 'DataObjectTest_SubTeam');
$this->assertContains('ClassName', array_keys($changedFields));
$this->assertEquals($changedFields['ClassName']['before'], 'DataObjectTest_Team');
$this->assertEquals($changedFields['ClassName']['after'], 'DataObjectTest_SubTeam');

$changedDO->write();

$this->assertType('DataObjectTest_SubTeam', $changedDO);
$this->assertInstanceOf('DataObjectTest_SubTeam', $changedDO);
$this->assertEquals($changedDO->ClassName, 'DataObjectTest_SubTeam');
}

Expand Down

0 comments on commit c05e80c

Please sign in to comment.