<commit_before><?php
namespace Metabor\Statemachine\Factory;

use Metabor\Statemachine\Condition\Tautology;
use Metabor\Statemachine\Factory\TransitionSelector\ScoreTransition;
use Metabor\Statemachine\Process;
use Metabor\Statemachine\State;
use Metabor\Statemachine\Transition;
use MetaborStd\Statemachine\Factory\FactoryInterfaceTest;

/**
 * 
 * @author <NAME>
 *
 */
class ScoreTransitionTest extends \PHPUnit_Framework_TestCase
{
	/**
	 * 
	 * @return multitype:\Metabor\Statemachine\Transition
	 */
    public function testIfThereIsOnlyOneTransitionWithoutEventAndTransitionUseThis()
    {
        $targetState = new State('TargetState');

        $transitions = array();
        $transitionAllwaysActive = new Transition($targetState);
        $transitions[] = $transitionAllwaysActive;

        array_rand($transitions);

        $selector = new ScoreTransition();
        $result = $selector->selectTransition(new \ArrayIterator($transitions));

        $this->assertEquals($transitionAllwaysActive, $result);

        return $transitions;
    }

    /**
     * @depends testIfThereIsOnlyOneTransitionWithoutEventAndTransitionUseThis
     * @param array $transitions
     * @return \Metabor\Statemachine\Transition
     */
    public function testPrefereTransitionWithCondition(array $transitions)
    {
        $targetState = new State('TargetState');
        $condition = new Tautology('Allways True');

        $transitionWithConditon = new Transition($targetState, null, $condition);
        $transitions[] = $transitionWithConditon;

        array_rand($transitions);

        $selector = new ScoreTransition();
        $result = $selector->selectTransition(new \ArrayIterator($transitions));

        $this->assertEquals($transitionWithConditon, $result);

        return $transitions;
    }

    /**
     * @depends testIfThereIsOnlyOneTransitionWithoutEventAndTransitionUseThis
     * @param array $transitions
     * @return \Metabor\Statemachine\Transition
     */
    public function testPrefereTransitionWithEvent(array $transitions)
    {
        $targetState = new State('TargetState');
        $eventName = 'testEvent';

        $transitionWithEvent = new Transition($targetState, $eventName);
        $transitions[] = $transitionWithEvent;

        array_rand($transitions);

        $selector = new ScoreTransition();
        $result = $selector->selectTransition(new \ArrayIterator($transitions));

        $this->assertEquals($transitionWithEvent, $result);

        return $transitions;
    }

    /**
     * @depends testPrefereTransitionWithEvent
     * @param array $transitions
     * @return \Metabor\Statemachine\Transition
     */
    public function testPrefereTransitionWithEventAndCondition(array $transitions)
    {
        $targetState = new State('TargetState');
        $eventName = 'testEvent';
        $condition = new Tautology('Allways True');

        $transitionWithEventAndConditon = new Transition($targetState, $eventName, $condition);
        $transitions[] = $transitionWithEventAndConditon;

        array_rand($transitions);

        $selector = new ScoreTransition();
        $result = $selector->selectTransition(new \ArrayIterator($transitions));

        $this->assertEquals($transitionWithEventAndConditon, $result);
    }
}<commit_msg>Add UnitTest<commit_after><?php
namespace Metabor\Statemachine\Factory;

use Metabor\Statemachine\Condition\Tautology;
use Metabor\Statemachine\Factory\TransitionSelector\ScoreTransition;
use Metabor\Statemachine\Process;
use Metabor\Statemachine\State;
use Metabor\Statemachine\Transition;
use MetaborStd\Statemachine\Factory\FactoryInterfaceTest;

/**
 * 
 * @author <NAME>
 *
 */
class ScoreTransitionTest extends \PHPUnit_Framework_TestCase
{
    /**
     * 
     * @return multitype:\Metabor\Statemachine\Transition
     */
    public function testIfThereIsOnlyOneTransitionWithoutEventAndTransitionUseThis()
    {
        $targetState = new State('TargetState');

        $transitions = array();
        $transitionAllwaysActive = new Transition($targetState);
        $transitions[] = $transitionAllwaysActive;

        array_rand($transitions);

        $selector = new ScoreTransition();
        $result = $selector->selectTransition(new \ArrayIterator($transitions));

        $this->assertEquals($transitionAllwaysActive, $result);

        return $transitions;
    }

    /**
     * @depends testIfThereIsOnlyOneTransitionWithoutEventAndTransitionUseThis
     * @param array $transitions
     * @return \Metabor\Statemachine\Transition
     */
    public function testPrefereTransitionWithCondition(array $transitions)
    {
        $targetState = new State('TargetState');
        $condition = new Tautology('Allways True');

        $transitionWithConditon = new Transition($targetState, null, $condition);
        $transitions[] = $transitionWithConditon;

        array_rand($transitions);

        $selector = new ScoreTransition();
        $result = $selector->selectTransition(new \ArrayIterator($transitions));

        $this->assertEquals($transitionWithConditon, $result);

        return $transitions;
    }

    /**
     * @depends testIfThereIsOnlyOneTransitionWithoutEventAndTransitionUseThis
     * @param array $transitions
     * @return \Metabor\Statemachine\Transition
     */
    public function testPrefereTransitionWithEvent(array $transitions)
    {
        $targetState = new State('TargetState');
        $eventName = 'testEvent';

        $transitionWithEvent = new Transition($targetState, $eventName);
        $transitions[] = $transitionWithEvent;

        array_rand($transitions);

        $selector = new ScoreTransition();
        $result = $selector->selectTransition(new \ArrayIterator($transitions));

        $this->assertEquals($transitionWithEvent, $result);

        return $transitions;
    }

    /**
     * @depends testPrefereTransitionWithEvent
     * @param array $transitions
     * @return \Metabor\Statemachine\Transition
     */
    public function testPrefereTransitionWithEventAndCondition(array $transitions)
    {
        $targetState = new State('TargetState');
        $eventName = 'testEvent';
        $condition = new Tautology('Allways True');

        $transitionWithEventAndConditon = new Transition($targetState, $eventName, $condition);
        $transitions[] = $transitionWithEventAndConditon;

        array_rand($transitions);

        $selector = new ScoreTransition();
        $result = $selector->selectTransition(new \ArrayIterator($transitions));

        $this->assertEquals($transitionWithEventAndConditon, $result);
    }

    /**
     * 
     */
    public function testThrowsAnExceptionIfMoreThanOneTransitionIsInTheHighestLevel()
    {
        $targetState = new State('TargetState');

        $transitions = array();
        $transitions[] = new Transition($targetState);
        $transitions[] = new Transition($targetState);

        $selector = new ScoreTransition();

        $this->setExpectedException('\RuntimeException');
        $selector->selectTransition(new \ArrayIterator($transitions));
    }
}
{'fraction_non_alphanumeric': 0.11020293122886134, 'fraction_numerical': 0.0, 'mean_word_length': 3.569864777849324, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6126731', 'n_tokens_mistral': 1898, 'n_tokens_neox': 1820, 'n_words': 325}