123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- <?php
- /**
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
- * @package Base
- * @subpackage Tests
- * @version //autogentag//
- * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
- */
- /**
- * @package Base
- * @subpackage Tests
- */
- class ezcBaseTest extends ezcTestCase
- {
- /*
- * For use with the method testInvalidClass().
- */
- private $errorMessage = null;
- public function testConfigExceptionUnknownSetting()
- {
- try
- {
- throw new ezcBaseSettingNotFoundException( 'broken' );
- }
- catch ( ezcBaseSettingNotFoundException $e )
- {
- $this->assertEquals( "The setting 'broken' is not a valid configuration setting.", $e->getMessage() );
- }
- }
- public function testConfigExceptionOutOfRange1()
- {
- try
- {
- throw new ezcBaseSettingValueException( 'broken', 42 );
- }
- catch ( ezcBaseSettingValueException $e )
- {
- $this->assertEquals( "The value '42' that you were trying to assign to setting 'broken' is invalid.", $e->getMessage() );
- }
- }
- public function testConfigExceptionOutOfRange2()
- {
- try
- {
- throw new ezcBaseSettingValueException( 'broken', 42, "int, 40 - 48" );
- }
- catch ( ezcBaseSettingValueException $e )
- {
- $this->assertEquals( "The value '42' that you were trying to assign to setting 'broken' is invalid. Allowed values are: int, 40 - 48", $e->getMessage() );
- }
- }
- public function testConfigExceptionOutOfRange3()
- {
- try
- {
- throw new ezcBaseSettingValueException( 'broken', array(1, 1, 3, 4, 5), 'int' );
- }
- catch ( ezcBaseSettingValueException $e )
- {
- $this->assertEquals( "The value 'a:5:{i:0;i:1;i:1;i:1;i:2;i:3;i:3;i:4;i:4;i:5;}' that you were trying to assign to setting 'broken' is invalid. Allowed values are: int", $e->getMessage() );
- }
- }
- public function testFileIoException1()
- {
- try
- {
- throw new ezcBaseFileIoException( 'testfile.php', ezcBaseFileException::READ );
- }
- catch ( ezcBaseFileIoException $e )
- {
- $this->assertEquals( "An error occurred while reading from 'testfile.php'.", $e->getMessage() );
- }
- }
- public function testFileIoException2()
- {
- try
- {
- throw new ezcBaseFileIoException( 'testfile.php', ezcBaseFileException::WRITE );
- }
- catch ( ezcBaseFileIoException $e )
- {
- $this->assertEquals( "An error occurred while writing to 'testfile.php'.", $e->getMessage() );
- }
- }
- public function testFileIoException3()
- {
- try
- {
- throw new ezcBaseFileIoException( 'testfile.php', ezcBaseFileException::WRITE, "Extra extra" );
- }
- catch ( ezcBaseFileIoException $e )
- {
- $this->assertEquals( "An error occurred while writing to 'testfile.php'. (Extra extra)", $e->getMessage() );
- }
- }
- public function testFileNotFoundException1()
- {
- try
- {
- throw new ezcBaseFileNotFoundException( 'testfile.php' );
- }
- catch ( ezcBaseFileNotFoundException $e )
- {
- $this->assertEquals( "The file 'testfile.php' could not be found.", $e->getMessage() );
- }
- }
- public function testFileNotFoundException2()
- {
- try
- {
- throw new ezcBaseFileNotFoundException( 'testfile.php', 'INI' );
- }
- catch ( ezcBaseFileNotFoundException $e )
- {
- $this->assertEquals( "The INI file 'testfile.php' could not be found.", $e->getMessage() );
- }
- }
- public function testFileNotFoundException3()
- {
- try
- {
- throw new ezcBaseFileNotFoundException( 'testfile.php', 'INI', "Extra extra" );
- }
- catch ( ezcBaseFileNotFoundException $e )
- {
- $this->assertEquals( "The INI file 'testfile.php' could not be found. (Extra extra)", $e->getMessage() );
- }
- }
- public function testFilePermissionException1()
- {
- try
- {
- throw new ezcBaseFilePermissionException( 'testfile.php', ezcBaseFileException::READ );
- }
- catch ( ezcBaseFilePermissionException $e )
- {
- $this->assertEquals( "The file 'testfile.php' can not be opened for reading.", $e->getMessage() );
- }
- }
- public function testFilePermissionException2()
- {
- try
- {
- throw new ezcBaseFilePermissionException( 'testfile.php', ezcBaseFileException::WRITE );
- }
- catch ( ezcBaseFileException $e )
- {
- $this->assertEquals( "The file 'testfile.php' can not be opened for writing.", $e->getMessage() );
- }
- }
- public function testFilePermissionException3()
- {
- try
- {
- throw new ezcBaseFilePermissionException( 'testfile.php', ezcBaseFileException::EXECUTE );
- }
- catch ( ezcBaseException $e )
- {
- $this->assertEquals( "The file 'testfile.php' can not be executed.", $e->getMessage() );
- }
- }
- public function testFilePermissionException4()
- {
- try
- {
- throw new ezcBaseFilePermissionException( 'testfile.php', ezcBaseFilePermissionException::CHANGE, "Extra extra" );
- }
- catch ( ezcBaseException $e )
- {
- $this->assertEquals( "The permissions for 'testfile.php' can not be changed. (Extra extra)", $e->getMessage() );
- }
- }
- public function testFilePermissionException5()
- {
- try
- {
- throw new ezcBaseFilePermissionException( 'testfile.php', ezcBaseFilePermissionException::READ | ezcBaseFilePermissionException::WRITE, "Extra extra" );
- }
- catch ( ezcBaseException $e )
- {
- $this->assertEquals( "The file 'testfile.php' can not be opened for reading and writing. (Extra extra)", $e->getMessage() );
- }
- }
- public function testFilePermissionException6()
- {
- try
- {
- throw new ezcBaseFilePermissionException( 'testfile.php', ezcBaseFilePermissionException::REMOVE, "Extra extra" );
- }
- catch ( ezcBaseException $e )
- {
- $this->assertEquals( "The file 'testfile.php' can not be removed. (Extra extra)", $e->getMessage() );
- }
- }
- public function testPropertyNotFoundException()
- {
- try
- {
- throw new ezcBasePropertyNotFoundException( 'broken' );
- }
- catch ( ezcBasePropertyNotFoundException $e )
- {
- $this->assertEquals( "No such property name 'broken'.", $e->getMessage() );
- }
- }
- public function testPropertyPermissionException1()
- {
- try
- {
- throw new ezcBasePropertyPermissionException( 'broken', ezcBasePropertyPermissionException::READ );
- }
- catch ( ezcBaseException $e )
- {
- $this->assertEquals( "The property 'broken' is read-only.", $e->getMessage() );
- }
- }
- public function testPropertyPermissionException2()
- {
- try
- {
- throw new ezcBasePropertyPermissionException( 'broken', ezcBasePropertyPermissionException::WRITE );
- }
- catch ( ezcBaseException $e )
- {
- $this->assertEquals( "The property 'broken' is write-only.", $e->getMessage() );
- }
- }
- public function testBaseValue1()
- {
- try
- {
- throw new ezcBaseValueException( 'broken', array( 42 ) );
- }
- catch ( ezcBaseValueException $e )
- {
- $this->assertEquals( "The value 'a:1:{i:0;i:42;}' that you were trying to assign to setting 'broken' is invalid.", $e->getMessage() );
- }
- }
- public function testBaseValue2()
- {
- try
- {
- throw new ezcBaseValueException( 'broken', "string", "strings" );
- }
- catch ( ezcBaseValueException $e )
- {
- $this->assertEquals( "The value 'string' that you were trying to assign to setting 'broken' is invalid. Allowed values are: strings.", $e->getMessage() );
- $this->assertEquals( "The value 'string' that you were trying to assign to setting 'broken' is invalid. Allowed values are: strings.", $e->originalMessage );
- }
- }
- public function testExtraDirNotFoundException()
- {
- try
- {
- ezcBase::addClassRepository( 'wrongDir' );
- }
- catch ( ezcBaseFileNotFoundException $e )
- {
- $this->assertEquals( "The base directory file 'wrongDir' could not be found.", $e->getMessage() );
- }
- }
- public function testExtraDirBaseNotFoundException()
- {
- try
- {
- ezcBase::addClassRepository( '.', './wrongAutoloadDir' );
- }
- catch ( ezcBaseFileNotFoundException $e )
- {
- $this->assertEquals( "The autoload directory file './wrongAutoloadDir' could not be found.", $e->getMessage() );
- }
- }
-
- public function testBaseAddAndGetAutoloadDirs1()
- {
- ezcBase::addClassRepository( __DIR__ );
- $resultArray = ezcBase::getRepositoryDirectories();
- if ( count( $resultArray ) != 2 )
- {
- $this->fail( "Duplicating or missing extra autoload dirs while adding." );
- }
- if ( !isset( $resultArray['ezc'] ) )
- {
- $this->fail( "No packageDir found in result of getRepositoryDirectories()" );
- }
- if ( !isset( $resultArray[0] ) || $resultArray[0]->basePath != __DIR__ )
- {
- $this->fail( "Extra base dir '{$resultArray[0]->basePath}' is added incorrectly" );
- }
- if ( !isset( $resultArray[0] ) || $resultArray[0]->autoloadPath != __DIR__ . '/autoload' )
- {
- $this->fail( "Extra autoload dir '{$resultArray[0]->autoloadPath}' is added incorrectly" );
- }
- }
- // this test is sorta obsolete, but we keep it around for good measure
- public function testBaseAddAndGetAutoloadDirs2()
- {
- ezcBase::addClassRepository( __DIR__, __DIR__ . '/autoload' );
- ezcBase::addClassRepository( __DIR__ . '/test_repository', __DIR__ . '/test_repository/autoload_files' );
- ezcBase::addClassRepository( __DIR__ . '/test_repository', __DIR__ . '/test_repository/autoload_files' );
- $resultArray = ezcBase::getRepositoryDirectories();
- if ( count( $resultArray ) != 5 )
- {
- $this->fail( "Duplicating or missing extra autoload dirs while adding." );
- }
- if ( !isset( $resultArray['ezc'] ) )
- {
- $this->fail( "No packageDir found in result of getRepositoryDirectories()" );
- }
- if ( !isset( $resultArray[2] ) || $resultArray[2]->autoloadPath != __DIR__ . '/test_repository/autoload_files' )
- {
- $this->fail( "Extra autoload dir '{$resultArray[2]->autoloadPath}' is added incorrectly" );
- }
- self::assertEquals( true, class_exists( 'trBasetestClass', true ) );
- self::assertEquals( true, class_exists( 'trBasetestClass2', true ) );
- try
- {
- self::assertEquals( false, class_exists( 'trBasetestClass3', true ) );
- self::fail( 'The expected exception was not thrown.' );
- }
- catch ( ezcBaseAutoloadException $e )
- {
- self::assertStringStartsWith( "Could not find a class to file mapping for 'trBasetestClass3'. Searched for basetest_class3_autoload.php, basetest_autoload.php, autoload.php in:", $e->getMessage());
- }
- self::assertEquals( true, class_exists( 'trBasetestLongClass', true ) );
- try
- {
- class_exists( 'trBasetestClass4', true );
- self::fail( 'The expected exception was not thrown.' );
- }
- catch ( ezcBaseFileNotFoundException $e )
- {
- self::assertEquals( "The file '" . __DIR__ . "/test_repository/TestClasses/base_test_class_number_four.php' could not be found.", $e->getMessage() );
- }
- }
- public function testBaseAddAndGetAutoloadDirs3()
- {
- ezcBase::addClassRepository( __DIR__ . '/extra_repository', null, 'ext' );
- $resultArray = ezcBase::getRepositoryDirectories();
- self::assertEquals( true, array_key_exists( 'ezc', $resultArray ) );
- self::assertEquals( true, array_key_exists( 'ext', $resultArray ) );
- //self::assertEquals( true, class_exists( 'extTranslationTest', true ) );
- //self::assertEquals( true, class_exists( 'ezcTranslationTsBackend', true ) );
- }
- public function testBaseAddAndGetAutoloadDirs4()
- {
- ezcBase::addClassRepository( __DIR__ . '/test_repository', __DIR__ . '/test_repository/autoload_files', 'tr' );
- try
- {
- ezcBase::addClassRepository( __DIR__ . '/test_repository', __DIR__ . '/test_repository/autoload_files', 'tr' );
- }
- catch ( ezcBaseDoubleClassRepositoryPrefixException $e )
- {
- self::assertEquals( "The class repository in '" . __DIR__ . "/test_repository' (with autoload dir '" . __DIR__ . "/test_repository/autoload_files') can not be added because another class repository already uses the prefix 'tr'.", $e->getMessage() );
- }
- $resultArray = ezcBase::getRepositoryDirectories();
- self::assertEquals( 7, count( $resultArray ) );
- self::assertEquals( true, array_key_exists( 'ezc', $resultArray ) );
- self::assertEquals( true, array_key_exists( 'tr', $resultArray ) );
- self::assertEquals( __DIR__ . '/test_repository', $resultArray['tr']->basePath );
- self::assertEquals( __DIR__ . '/test_repository/autoload_files', $resultArray['tr']->autoloadPath );
- }
- public function testNoPrefixAutoload()
- {
- ezcBase::addClassRepository( __DIR__ . '/test_repository', __DIR__ . '/test_repository/autoload_files' );
- ezc_autoload( 'Objet' );
- if ( !class_exists( 'Objet' ) )
- {
- $this->fail( "Autoload does not handle classes with no prefix" );
- }
- }
- public function testNoPrefixAutoload2()
- {
- ezcBase::addClassRepository( __DIR__ . '/issue15896' );
- ezc_autoload( 'ab' );
- }
- public function testCheckDependencyExtension()
- {
- ezcBase::checkDependency( 'Tester', ezcBase::DEP_PHP_EXTENSION, 'standard' );
- }
- public function testCheckDependencyVersion()
- {
- ezcBase::checkDependency( 'Tester', ezcBase::DEP_PHP_VERSION, '5.1.1' );
- }
- public function testInvalidClass()
- {
- $this->setExpectedException( 'ezcBaseAutoloadException', "Could not find a class to file mapping for 'ezcNoSuchClass'. Searched for no_such_autoload.php, no_autoload.php, autoload.php in:" );
- self::assertEquals( false, class_exists( 'ezcNoSuchClass', true ) );
- }
- public function testDebug()
- {
- try
- {
- class_exists( 'ezcTestingOne' );
- self::fail( "There should have been an exception" );
- }
- catch ( ezcBaseAutoloadException $e )
- {
- }
- }
- public function testNoDebug()
- {
- try
- {
- $options = new ezcBaseAutoloadOptions;
- $options->debug = false;
- ezcBase::setOptions( $options );
- class_exists( 'ezcTestingOne' );
- }
- catch ( Exception $e )
- {
- self::fail( "There should not have been an exception. Found one: " . $e->getMessage() );
- }
- }
- public function testGetInstallationPath()
- {
- $this->markTestSkipped( 'What should behavior be in a composer installed environment?' );
- $path = ezcBase::getInstallationPath();
- $pathParts = explode( DIRECTORY_SEPARATOR, $path );
- self::assertEquals( array( 'trunk', '' ), array_splice( $pathParts, -2 ) );
- self::assertEquals( DIRECTORY_SEPARATOR, substr( $path, -1 ) );
- }
- public function testSetInvalidRunMode()
- {
- try
- {
- ezcBase::setRunMode( -3 );
- self::fail( "Expected exception not thrown." );
- }
- catch ( ezcBaseValueException $e )
- {
- self::assertEquals( "The value '-3' that you were trying to assign to setting 'runMode' is invalid. Allowed values are: ezcBase::MODE_PRODUCTION or ezcBase::MODE_DEVELOPMENT.", $e->getMessage() );
- }
- }
- public function testSetGetRunMode()
- {
- self::assertEquals( ezcBase::MODE_DEVELOPMENT, ezcBase::getRunMode() );
- self::assertEquals( true, ezcBase::inDevMode() );
- ezcBase::setRunMode( ezcBase::MODE_PRODUCTION );
- self::assertEquals( ezcBase::MODE_PRODUCTION, ezcBase::getRunMode() );
- self::assertEquals( false, ezcBase::inDevMode() );
- ezcBase::setRunMode( ezcBase::MODE_DEVELOPMENT );
- self::assertEquals( ezcBase::MODE_DEVELOPMENT, ezcBase::getRunMode() );
- self::assertEquals( true, ezcBase::inDevMode() );
- }
- public function testGetInstallMethod()
- {
- self::assertEquals( 'custom', ezcBase::getInstallMethod() );
- }
- public function setup()
- {
- $options = new ezcBaseAutoloadOptions;
- $options->debug = true;
- ezcBase::setOptions( $options );
- }
- public function teardown()
- {
- $options = new ezcBaseAutoloadOptions;
- $options->debug = true;
- ezcBase::setOptions( $options );
- }
- public static function suite()
- {
- return new PHPUnit_Framework_TestSuite("ezcBaseTest");
- }
- }
- ?>
|