Permalink
Browse files

improved unit tests

  • Loading branch information...
1 parent 29cb005 commit 1f757cbf53993324af20141c0fb7ec640a91d2f4 alexander.makarow committed Feb 18, 2011
Showing with 10,416 additions and 0 deletions.
  1. +63 −0 tests/TestApplication.php
  2. +19 −0 tests/bootstrap.php
  3. +13 −0 tests/framework/base/CApplicationComponentTest.php
  4. +51 −0 tests/framework/base/CBehaviorTest.php
  5. +223 −0 tests/framework/base/CComponentTest.php
  6. +54 −0 tests/framework/base/CLogRouterTest.php
  7. +49 −0 tests/framework/base/CLoggerTest.php
  8. +122 −0 tests/framework/base/CModelTest.php
  9. +95 −0 tests/framework/base/CModuleTest.php
  10. +138 −0 tests/framework/base/CPropertyValueTest.php
  11. +79 −0 tests/framework/base/CSecurityManagerTest.php
  12. +33 −0 tests/framework/base/CStatePersisterTest.php
  13. +13 −0 tests/framework/base/NewApplicationComponent.php
  14. +8 −0 tests/framework/base/NewBeforeValidateBehavior.php
  15. +9 −0 tests/framework/base/NewBehavior.php
  16. +41 −0 tests/framework/base/NewComponent.php
  17. +10 −0 tests/framework/base/NewFormModel.php
  18. +23 −0 tests/framework/base/NewModel.php
  19. +4 −0 tests/framework/base/NewModule.php
  20. +178 −0 tests/framework/caching/CDbCacheTest.php
  21. +61 −0 tests/framework/caching/CDirectoryCacheDependencyTest.php
  22. +53 −0 tests/framework/caching/CFileCacheDependencyTest.php
  23. +141 −0 tests/framework/caching/CMemCacheTest.php
  24. +106 −0 tests/framework/collections/CAttributeCollectionTest.php
  25. +97 −0 tests/framework/collections/CConfigurationTest.php
  26. +208 −0 tests/framework/collections/CListTest.php
  27. +204 −0 tests/framework/collections/CMapTest.php
  28. +119 −0 tests/framework/collections/CQueueTest.php
  29. +119 −0 tests/framework/collections/CStackTest.php
  30. +22 −0 tests/framework/collections/CTypedListTest.php
  31. +17 −0 tests/framework/collections/data/config.php
  32. +388 −0 tests/framework/db/CDbCommand2Test.php
  33. +239 −0 tests/framework/db/CDbCommandTest.php
  34. +116 −0 tests/framework/db/CDbConnectionTest.php
  35. +162 −0 tests/framework/db/CDbDataReaderTest.php
  36. +63 −0 tests/framework/db/CDbTransactionTest.php
  37. +596 −0 tests/framework/db/ar/CActiveRecord2Test.php
  38. +156 −0 tests/framework/db/ar/CActiveRecordEventWrappersTest.php
  39. +54 −0 tests/framework/db/ar/CActiveRecordHasManyThroughModels.php
  40. +77 −0 tests/framework/db/ar/CActiveRecordHasManyThroughTest.php
  41. +29 −0 tests/framework/db/ar/CActiveRecordHasManyThroughTest.sql
  42. +878 −0 tests/framework/db/ar/CActiveRecordTest.php
  43. +596 −0 tests/framework/db/data/models.php
  44. +185 −0 tests/framework/db/data/models2.php
  45. +306 −0 tests/framework/db/data/mssql.sql
  46. +150 −0 tests/framework/db/data/mysql.sql
  47. +165 −0 tests/framework/db/data/postgres.sql
  48. +215 −0 tests/framework/db/data/sqlite.sql
  49. +507 −0 tests/framework/db/schema/CDbCriteriaTest.php
  50. +310 −0 tests/framework/db/schema/CMssqlTest.php
  51. +128 −0 tests/framework/db/schema/CMysql2Test.php
  52. +275 −0 tests/framework/db/schema/CMysqlTest.php
  53. +126 −0 tests/framework/db/schema/CPostgres2Test.php
  54. +271 −0 tests/framework/db/schema/CPostgresTest.php
  55. +258 −0 tests/framework/db/schema/CSqliteTest.php
  56. +49 −0 tests/framework/db/schema/sqlite.sql
  57. +98 −0 tests/framework/i18n/CLocaleTest.php
  58. +70 −0 tests/framework/i18n/CNumberFormatterTest.php
  59. +186 −0 tests/framework/i18n/YiiTTest.php
  60. +4 −0 tests/framework/i18n/data/en/test.php
  61. +15 −0 tests/framework/i18n/data/ru/test.php
  62. +4 −0 tests/framework/i18n/data/zh_cn/test.php
  63. +11 −0 tests/framework/utils/CDateTimeParserTest.php
  64. +12 −0 tests/framework/validators/CEmailValidatorTest.php
  65. +12 −0 tests/framework/validators/ValidatorTestModel.php
  66. +42 −0 tests/framework/web/CAssetManagerTest.php
  67. +187 −0 tests/framework/web/CControllerTest.php
  68. +84 −0 tests/framework/web/CSortTest.php
  69. +12 −0 tests/framework/web/CSortTest.sql
  70. +559 −0 tests/framework/web/CUrlManagerTest.php
  71. +243 −0 tests/framework/web/auth/AuthManagerTestBase.php
  72. +45 −0 tests/framework/web/auth/CDbAuthManagerTest.php
  73. +29 −0 tests/framework/web/auth/CPhpAuthManagerTest.php
  74. +32 −0 tests/framework/web/auth/schema.sql
  75. 0 tests/framework/web/controllers/admin/index.php
  76. 0 tests/framework/web/controllers/article.php
  77. 0 tests/framework/web/controllers/home2.php
  78. 0 tests/framework/web/controllers/post.php
  79. +79 −0 tests/framework/web/helpers/CJSONTest.php
  80. +7 −0 tests/phpunit.xml
  81. +13 −0 tests/readme.txt
  82. +1 −0 tests/rununit.bat
@@ -0,0 +1,63 @@
+<?php
+
+class TestApplication extends CWebApplication
+{
+ public function __construct($config=null)
+ {
+ Yii::setApplication(null);
+ clearstatcache();
+ parent::__construct($config);
+ }
+
+ public function reset()
+ {
+ $this->removeDirectory($this->getRuntimePath());
+ $this->removeDirectory($this->getAssetPath());
+ }
+
+ protected function removeDirectory($path)
+ {
+ if(is_dir($path) && ($folder=@opendir($path))!==false)
+ {
+ while($entry=@readdir($folder))
+ {
+ if($entry[0]==='.')
+ continue;
+ $p=$path.DIRECTORY_SEPARATOR.$entry;
+ if(is_dir($p))
+ $this->removeDirectory($p);
+ @unlink($p);
+ }
+ @closedir($folder);
+ }
+ }
+
+ public function getAssetPath()
+ {
+ return dirname(__FILE__).DIRECTORY_SEPARATOR.'assets';
+ }
+
+ public function getRuntimePath()
+ {
+ return dirname(__FILE__).DIRECTORY_SEPARATOR.'runtime';
+ }
+
+ public function getBasePath()
+ {
+ return dirname(__FILE__);
+ }
+
+ public function setBasePath($value)
+ {
+ }
+
+ public function loadGlobalState()
+ {
+ parent::loadGlobalState();
+ }
+
+ public function saveGlobalState()
+ {
+ parent::saveGlobalState();
+ }
+}
View
@@ -0,0 +1,19 @@
+<?php
+
+define('YII_ENABLE_EXCEPTION_HANDLER',false);
+define('YII_ENABLE_ERROR_HANDLER',false);
+$_SERVER['SCRIPT_NAME']='/'.basename(__FILE__);
+$_SERVER['SCRIPT_FILENAME']=__FILE__;
+
+require_once(dirname(__FILE__).'/../framework/yii.php');
+require_once(dirname(__FILE__).'/TestApplication.php');
+require_once('PHPUnit/Framework/TestCase.php');
+
+class CTestCase extends PHPUnit_Framework_TestCase
+{
+}
+
+
+class CActiveRecordTestCase extends CTestCase
+{
+}
@@ -0,0 +1,13 @@
+<?php
+
+class CApplicationComponentTest extends CTestCase {
+ public function testInitialization() {
+ $c = $this->getMockForAbstractClass('CApplicationComponent',array('init','getIsInitialized'),'',NULL);
+ $c->expects($this->any())
+ ->method('getIsInitialized')
+ ->will($this->returnValue(FALSE));
+ $this->assertFalse($c->getIsInitialized());
+ $c->init();
+ $this->assertTrue($c->getIsInitialized());
+ }
+}
@@ -0,0 +1,51 @@
+<?php
+require_once dirname(__FILE__) . '/NewComponent.php';
+require_once dirname(__FILE__) . '/NewBehavior.php';
+
+require_once dirname(__FILE__) . '/NewBeforeValidateBehavior.php';
+require_once dirname(__FILE__) . '/NewFormModel.php';
+
+class CBehaviorTest extends CTestCase {
+
+ public function testAttachBehavior() {
+ $component=new NewComponent;
+ $component->attachBehavior('a',new NewBehavior);
+ $this->assertFalse($component->behaviorCalled);
+ $this->assertFalse(method_exists($component,'test'));
+ $this->assertEquals(2,$component->test());
+ $this->assertTrue($component->behaviorCalled);
+ $this->setExpectedException('CException');
+ $component->test2();
+ }
+
+ public function testDisableBehaviors(){
+ $component=new NewComponent;
+ $component->attachBehavior('a',new NewBehavior);
+ $component->disableBehaviors();
+ $this->setExpectedException('CException');
+ // test should not be called since behavior is disabled
+ echo $component->test();
+ }
+
+ /**
+ * Since disableBehaviors() was called, validate() should not call beforeValidate() from behavior.
+ * @return void
+ */
+ public function testDisableBehaviorsAndModels(){
+ $model = new NewFormModel();
+ $model->disableBehaviors();
+ $model->validate();
+ }
+
+ /**
+ * enableBehaviors() should work after disableBehaviors().
+ * @return void
+ */
+ public function testDisableAndEnableBehaviorsAndModels(){
+ $this->setExpectedException('NewBeforeValidateBehaviorException');
+ $model = new NewFormModel();
+ $model->disableBehaviors();
+ $model->enableBehaviors();
+ $model->validate();
+ }
+}
@@ -0,0 +1,223 @@
+<?php
+require_once dirname(__FILE__) . '/NewComponent.php';
+require_once dirname(__FILE__) . '/NewBehavior.php';
+
+function globalEventHandler($event)
+{
+ $event->sender->eventHandled=true;
+}
+
+function globalEventHandler2($event)
+{
+ $event->sender->eventHandled=true;
+ $event->handled=true;
+}
+
+class CComponentTest extends CTestCase
+{
+ protected $component;
+
+ public function setUp()
+ {
+ $this->component = new NewComponent();
+ }
+
+ public function tearDown()
+ {
+ $this->component = null;
+ }
+
+ public function testHasProperty()
+ {
+ $this->assertTrue($this->component->hasProperty('Text'), "Component hasn't property Text");
+ $this->assertTrue($this->component->hasProperty('text'), "Component hasn't property text");
+ $this->assertFalse($this->component->hasProperty('Caption'), "Component as property Caption");
+ }
+
+ public function testCanGetProperty()
+ {
+ $this->assertTrue($this->component->canGetProperty('Text'));
+ $this->assertTrue($this->component->canGetProperty('text'));
+ $this->assertFalse($this->component->canGetProperty('Caption'));
+ }
+
+ public function testCanSetProperty()
+ {
+ $this->assertTrue($this->component->canSetProperty('Text'));
+ $this->assertTrue($this->component->canSetProperty('text'));
+ $this->assertFalse($this->component->canSetProperty('Caption'));
+ }
+
+ public function testGetProperty()
+ {
+ $this->assertTrue('default'===$this->component->Text);
+ $this->setExpectedException('CException');
+ $value2=$this->component->Caption;
+ }
+
+ public function testSetProperty()
+ {
+ $value='new value';
+ $this->component->Text=$value;
+ $text=$this->component->Text;
+ $this->assertTrue($value===$this->component->Text);
+ $this->setExpectedException('CException');
+ $this->component->NewMember=$value;
+ }
+
+ public function testIsset()
+ {
+ $this->assertTrue(isset($this->component->Text));
+ $this->assertTrue(!empty($this->component->Text));
+
+ unset($this->component->Text);
+ $this->assertFalse(isset($this->component->Text));
+ $this->assertFalse(!empty($this->component->Text));
+
+ $this->component->Text='';
+ $this->assertTrue(isset($this->component->Text));
+ $this->assertTrue(empty($this->component->Text));
+ }
+
+ public function testHasEvent()
+ {
+ $this->assertTrue($this->component->hasEvent('OnMyEvent'));
+ $this->assertTrue($this->component->hasEvent('onmyevent'));
+ $this->assertFalse($this->component->hasEvent('onYourEvent'));
+ }
+
+ public function testHasEventHandler()
+ {
+ $this->assertFalse($this->component->hasEventHandler('OnMyEvent'));
+ $this->component->attachEventHandler('OnMyEvent','foo');
+ $this->assertTrue($this->component->hasEventHandler('OnMyEvent'));
+ }
+
+ public function testGetEventHandlers()
+ {
+ $list=$this->component->getEventHandlers('OnMyEvent');
+ $this->assertEquals($list->getCount(),0);
+ $this->component->attachEventHandler('OnMyEvent','foo');
+ $this->assertEquals($list->getCount(),1);
+ $this->setExpectedException('CException');
+ $list=$this->component->getEventHandlers('YourEvent');
+ }
+
+ public function testAttachEventHandler()
+ {
+ $this->component->attachEventHandler('OnMyEvent','foo');
+ $this->assertTrue($this->component->getEventHandlers('OnMyEvent')->getCount()===1);
+ $this->setExpectedException('CException');
+ $this->component->attachEventHandler('YourEvent','foo');
+ }
+
+ public function testDettachEventHandler()
+ {
+ $this->component->attachEventHandler('OnMyEvent','foo');
+ $this->component->attachEventHandler('OnMyEvent',array($this->component,'myEventHandler'));
+ $this->assertEquals($this->component->getEventHandlers('OnMyEvent')->getCount(),2);
+
+ $this->assertTrue($this->component->detachEventHandler('OnMyEvent','foo'));
+ $this->assertEquals($this->component->getEventHandlers('OnMyEvent')->getCount(),1);
+
+ $this->assertFalse($this->component->detachEventHandler('OnMyEvent','foo'));
+ $this->assertEquals($this->component->getEventHandlers('OnMyEvent')->getCount(),1);
+
+ $this->assertTrue($this->component->detachEventHandler('OnMyEvent',array($this->component,'myEventHandler')));
+ $this->assertEquals($this->component->getEventHandlers('OnMyEvent')->getCount(),0);
+
+ $this->assertFalse($this->component->detachEventHandler('OnMyEvent','foo'));
+ }
+
+ public function testRaiseEvent()
+ {
+ $this->component->attachEventHandler('OnMyEvent',array($this->component,'myEventHandler'));
+ $this->assertFalse($this->component->eventHandled);
+ $this->component->raiseEvent('OnMyEvent',new CEvent($this));
+ $this->assertTrue($this->component->eventHandled);
+
+ //$this->setExpectedException('CException');
+ //$this->component->raiseEvent('OnUnknown',new CEvent($this));
+ }
+
+ public function testEventAccessor()
+ {
+ $component=new NewComponent;
+ $this->assertEquals($component->onMyEvent->getCount(),0);
+ $component->onMyEvent='globalEventHandler';
+ $component->onMyEvent=array($this->component,'myEventHandler');
+ $this->assertEquals($component->onMyEvent->getCount(),2);
+ $this->assertFalse($component->eventHandled);
+ $this->assertFalse($this->component->eventHandled);
+ $component->onMyEvent();
+ $this->assertTrue($component->eventHandled);
+ $this->assertTrue($this->component->eventHandled);
+ }
+
+ public function testStopEvent()
+ {
+ $component=new NewComponent;
+ $component->onMyEvent='globalEventHandler2';
+ $component->onMyEvent=array($this->component,'myEventHandler');
+ $component->onMyEvent();
+ $this->assertTrue($component->eventHandled);
+ $this->assertFalse($this->component->eventHandled);
+ }
+
+ public function testInvalidHandler1()
+ {
+ $this->component->onMyEvent=array(1,2,3);
+ $this->setExpectedException('CException');
+ $this->component->onMyEvent();
+ }
+
+ public function testInvalidHandler2()
+ {
+ $this->component->onMyEvent=array($this->component,'nullHandler');
+ $this->setExpectedException('CException');
+ $this->component->onMyEvent();
+ }
+ public function testDetachBehavior() {
+ $component=new NewComponent;
+ $behavior = new NewBehavior;
+ $component->attachBehavior('a',$behavior);
+ $this->assertSame($behavior,$component->detachBehavior('a'));
+ }
+ public function testDetachingBehaviors() {
+ $component=new NewComponent;
+ $behavior = new NewBehavior;
+ $component->attachBehavior('a',$behavior);
+ $component->detachBehaviors();
+ $this->setExpectedException('CException');
+ $component->test();
+ }
+ public function testEnablingBehavior() {
+ $component=new NewComponent;
+ $behavior = new NewBehavior;
+ $component->attachBehavior('a',$behavior);
+ $component->disableBehavior('a');
+ $this->assertFalse($behavior->getEnabled());
+ $component->enableBehavior('a');
+ $this->assertTrue($behavior->getEnabled());
+ }
+ public function testEnablingBehaviors() {
+ $component=new NewComponent;
+ $behavior = new NewBehavior;
+ $component->attachBehavior('a',$behavior);
+ $component->disableBehaviors();
+ $this->assertFalse($behavior->getEnabled());
+ $component->enableBehaviors();
+ $this->assertTrue($behavior->getEnabled());
+ }
+ public function testAsa() {
+ $component=new NewComponent;
+ $behavior = new NewBehavior;
+ $component->attachBehavior('a',$behavior);
+ $this->assertSame($behavior,$component->asa('a'));
+ }
+ public function testEvaluateExpression() {
+ $component = new NewComponent;
+ $this->assertEquals('Hello world',$component->evaluateExpression('"Hello $who"',array('who' => 'world')));
+ $this->assertEquals('Hello world',$component->evaluateExpression(array($component,'exprEvaluator'),array('who' => 'world')));
+ }
+}
Oops, something went wrong.

0 comments on commit 1f757cb

Please sign in to comment.