CommitOrderCalculatorTest.php 1.57 KB
Newer Older
1
<?php
2 3 4

namespace Doctrine\Tests\ORM;

5
require_once __DIR__ . '/../TestInit.php';
6 7 8 9 10 11 12 13

/**
 * Tests of the commit order calculation.
 *
 * IMPORTANT: When writing tests here consider that a lot of graph constellations
 * can have many valid orderings, so you may want to build a graph that has only
 * 1 valid order to simplify your tests.
 */
14
class CommitOrderCalculatorTest extends \Doctrine\Tests\OrmTestCase
15 16 17 18 19
{
    private $_calc;
    
    protected function setUp()
    {
20
        $this->_calc = new \Doctrine\ORM\Internal\CommitOrderCalculator();
21
    }
romanb's avatar
romanb committed
22 23

    /** Helper to create an array of nodes */
24 25 26 27
    private function _createNodes(array $names)
    {
        $nodes = array();
        foreach ($names as $name) {
28
            $node = new \Doctrine\ORM\Internal\CommitOrderNode($name, $this->_calc);
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
            $nodes[$name] = $node;
            $this->_calc->addNode($node->getClass(), $node);
        }
        return $nodes;
    }
    
    public function testCommitOrdering1()
    {
        $nodes = $this->_createNodes(array("node1", "node2", "node3", "node4", "node5"));
        
        $nodes['node1']->before($nodes['node2']);
        $nodes['node2']->before($nodes['node3']);
        $nodes['node3']->before($nodes['node4']);
        $nodes['node5']->before($nodes['node1']);
        
        shuffle($nodes); // some randomness

        $sorted = $this->_calc->getCommitOrder();
        
        // There is only 1 valid ordering for this constellation
        $correctOrder = array("node5", "node1", "node2", "node3", "node4");
        $this->assertSame($correctOrder, $sorted);
        
    }
}