Ejemplo n.º 1
0
    /**
     * Test dumping a tree to a string.
     *
     * A tree of terms can be dumped to an ASCII tree for debugging purposes.
     * We will construct a tree of BaseTerms, and expect the resulting ASCII
     * representation to be accurate.
     */
    public function testAsciiTreeDump()
    {
        // Prepare the tree.
        $a = new BaseTerm('a', 'a');
        $b = new BaseTerm('b', 'b');
        $c = new BaseTerm('c', 'c');
        $d = new BaseTerm('d', 'd');
        $e = new BaseTerm('e', 'e');
        $f = new BaseTerm('f', 'f');
        $g = new BaseTerm('g', 'g');
        $h = new BaseTerm('h', 'h');
        $i = new BaseTerm('i', 'i');
        $a->addChild($b)->addChild($c)->addChild($h)->addChild($i);
        $c->addChild($d)->addChild($e);
        $e->addChild($f);
        $f->addChild($g);
        // Prepare the expected ASCII tree.
        $expectedAsciiTree = <<<'EOF'
--- a:a
    +-- b:b
    +-- c:c
        +-- d:d
        +-- e:e
            +-- f:f
                +-- g:g
    +-- h:h
    +-- i:i
EOF;
        // Prepare a mocked class, because BaseCurriculum is an abstract class.
        // Pass the root element of the tree to the constructor.
        $stub = $this->getMockForAbstractClass('Educa\\DSB\\Client\\Curriculum\\BaseCurriculum', [$a]);
        $this->assertEquals($a, $stub->getTree(), "Fetching the tree returns the root element.");
        $this->assertEquals(trim($expectedAsciiTree), $stub->asciiDump(), "The ASCII representation of the tree is correct.");
        // If there is no root, we return an empty string.
        $stub = $this->getMockForAbstractClass('Educa\\DSB\\Client\\Curriculum\\BaseCurriculum');
        $this->assertEquals('', $stub->asciiDump(), "The ASCII representation of an empty tree is an empty string.");
    }
Ejemplo n.º 2
0
 /**
  * Test searching for a child term.
  */
 public function testSearchChildTerm()
 {
     $terms = array();
     $root = new BaseTerm('root type', 'uuid0');
     for ($i = 5; $i > 0; $i--) {
         $term = new BaseTerm('parent type', "uuid{$i}", "Child {$i}");
         $root->addChild($term);
         $terms["uuid{$i}"] = $term;
         for ($j = 5; $j > 0; $j--) {
             $childTerm = new BaseTerm('child type', "uuid{$i}.{$j}", "Child {$i}.{$j}", "{$i}.{$j}");
             $term->addChild($childTerm);
             $terms["uuid{$i}.{$j}"] = $childTerm;
         }
     }
     $this->assertEquals([$terms['uuid3']], $root->findChildrenByName("Child 3"), "Searching by name works one level.");
     $this->assertEquals(null, $root->findChildrenByName("Child 4.2"), "Searching by name one level for a child that's located deeper returns null.");
     $this->assertEquals([$terms['uuid4.2']], $root->findChildrenByNameRecursive("Child 4.2"), "Recursively searching by name works.");
     $this->assertEquals($terms['uuid3'], $root->findChildByIdentifier('uuid3'), "Searching by ID works one level.");
     $this->assertEquals(null, $root->findChildByIdentifier('uuid4.2'), "Searching by ID one level for a child that's located deeper returns null.");
     $this->assertEquals($terms['uuid4.2'], $root->findChildByIdentifierRecursive('uuid4.2'), "Recursively searching by ID works.");
     $this->assertEquals([$terms['uuid5'], $terms['uuid4'], $terms['uuid3'], $terms['uuid2'], $terms['uuid1']], $root->findChildrenByType("parent type"), "Searching by type works.");
     $this->assertEquals(25, count($root->findChildrenByTypeRecursive("child type")), "Recursively searching by type works.");
 }