當前位置: 首頁>>代碼示例>>PHP>>正文


PHP UnitTester::assertNotNull方法代碼示例

本文整理匯總了PHP中UnitTester::assertNotNull方法的典型用法代碼示例。如果您正苦於以下問題:PHP UnitTester::assertNotNull方法的具體用法?PHP UnitTester::assertNotNull怎麽用?PHP UnitTester::assertNotNull使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在UnitTester的用法示例。


在下文中一共展示了UnitTester::assertNotNull方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: testCategories

 public function testCategories()
 {
     // 1. Add category taxonomy
     $taxonomy = new TaxonomyDef();
     $taxonomy->name = 'test_categories';
     $taxonomy->class = CategoryTerm::className();
     $taxonomy->data_table = 'sample_categories';
     $taxonomy->ref_table = SampleTable::className();
     // 2. Create data table
     $categoryTerm = Yii::createObject($taxonomy->attributes);
     $migration = $categoryTerm->install();
     $this->runMigration($migration);
     $categoryTerm = $this->getTaxonomy()->getTerm($taxonomy->name);
     $this->tester->assertTrue($categoryTerm->isInstalled(), 'The taxonomy must be installed.');
     // ***************CategoryTerm::createCategory() tests start*****************
     // 3. Add a root category of type array
     $rootTermName = 'root';
     $this->assertExceptionThrown(function () use($categoryTerm, $rootTermName) {
         $categoryTerm->createCategory([$rootTermName]);
     });
     // 3a. Create a root term
     $rootTerm = $categoryTerm->createCategory($rootTermName);
     $this->tester->assertNotNull($rootTerm);
     $this->tester->assertEquals($rootTermName, $rootTerm->term);
     // 4. Add a child of wrong type
     $this->assertExceptionThrown(function () use($categoryTerm, $rootTerm) {
         $categoryTerm->createCategory((int) $rootTerm->id, 321);
     });
     // 5. Adding an array of children where the child name is not a string
     $this->assertExceptionThrown(function () use($categoryTerm, $rootTerm) {
         $categoryTerm->createCategory((int) $rootTerm->id, [321]);
     });
     // 6. Attatching children to a non-existing parent
     $this->assertExceptionThrown(function () use($categoryTerm) {
         $categoryTerm->createCategory(500, ['Test', 'Test2']);
     });
     // 7. Adding a child to a parent
     $childName1 = 'child1';
     $result = $categoryTerm->createCategory((int) $rootTerm->id, [$childName1]);
     $this->tester->assertEquals(1, count($result));
     $this->tester->assertEquals($childName1, $result[0]->term);
     $this->tester->assertTrue($categoryTerm->hasParent($result[0]->id));
     $this->tester->assertTrue($categoryTerm->hasChildren($rootTerm->id));
     $categoryTerm = $this->getTaxonomy()->getTerm($taxonomy->name);
     $terms = $categoryTerm->getTerms();
     $this->tester->assertEquals(2, count($terms));
     $this->tester->assertContains($rootTermName, $terms);
     $this->tester->assertContains($childName1, $terms);
     $childTerm1 = $result[0];
     // ***************CategoryTerm::createCategory() tests end*****************
     // ***************CategoryTerm::addTerm() tests start*****************
     // 1. Assigning one existing term to an object
     $terms = $categoryTerm->addTerm(1, $rootTerm->id);
     $this->tester->assertEquals(1, count($terms));
     $this->tester->assertEquals($rootTermName, $terms[0]->term);
     $categoryTerm = $this->getTaxonomy()->getTerm($taxonomy->name, true);
     $rootTerm = $categoryTerm->getTaxonomyTerm($rootTermName);
     $terms = $categoryTerm->getTerms(1);
     $this->tester->assertEquals(1, $categoryTerm->total_count);
     $this->tester->assertEquals(1, $rootTerm->total_count);
     $this->tester->assertEquals(1, count($terms));
     $this->tester->assertContains($rootTermName, $terms);
     // 2. Assigning one existing term to an object as an array
     $terms = $categoryTerm->addTerm(1, [$childTerm1->id]);
     $this->tester->assertEquals(1, count($terms));
     $this->tester->assertEquals($childName1, $terms[0]->term);
     $categoryTerm = $this->getTaxonomy()->getTerm($taxonomy->name, true);
     $childTerm = $categoryTerm->getTaxonomyTerm($childName1);
     $terms = $categoryTerm->getTerms(1);
     $this->tester->assertEquals(2, $categoryTerm->total_count);
     $this->tester->assertEquals(1, $childTerm->total_count);
     $this->tester->assertEquals(2, count($terms));
     $this->tester->assertContains($rootTermName, $terms);
     $this->tester->assertContains($childName1, $terms);
     // ***************CategoryTerm::addTerm() tests end*****************
     // ***************CategoryTerm::setTerms() tests start*****************
     $childName2 = 'child2';
     $childName3 = 'child3';
     $childName4 = 'child4';
     $createdTerms = $categoryTerm->createCategory((int) $rootTerm->id, [$childName2, $childName3, $childName4]);
     $terms = $categoryTerm->getTerms(1);
     $this->tester->assertEquals(2, count($terms));
     // 1. Change the terms where the object is assigned
     $result = $categoryTerm->setTerms(1, [$rootTerm->id => [$createdTerms[0]->id, $createdTerms[1]->id]]);
     $terms = $categoryTerm->getTerms(1);
     $this->tester->assertEquals(2, count($result));
     $this->tester->assertEquals(3, count($terms));
     $this->tester->assertContains($childName2, $terms);
     $this->tester->assertContains($childName3, $terms);
     // ***************CategoryTerm::setTerms() tests end*****************
     // ***************getChildren(), hasChildren(), getParent(), hasParent() tests start*****************
     $this->tester->assertTrue($categoryTerm->hasChildren($rootTerm->id));
     $children = $categoryTerm->getChildren($rootTerm->id);
     $this->tester->assertEquals(4, count($children));
     $this->tester->assertTrue($categoryTerm->hasParent($childTerm->id));
     $parent = $categoryTerm->getParent($childTerm->id);
     $this->tester->assertNotNull($parent);
     $this->tester->assertEquals($rootTermName, $parent->term);
     // ***************getChildren(), hasChildren(), getParent(), hasParent() tests end*****************
 }
開發者ID:nkostadinov,項目名稱:yii2-taxonomy,代碼行數:100,代碼來源:TaxonomyTest.php


注:本文中的UnitTester::assertNotNull方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。