<commit_before><?php

namespace Tienvx\Bundle\MbtBundle\Tests\Command;

use Symfony\Component\Console\Tester\CommandTester;
use Tienvx\Bundle\MbtBundle\Generator\RandomGenerator;
use Tienvx\Bundle\MbtBundle\Graph\Path;

class GeneratePathCommandTest extends CommandTestCase
{
    public function coverageData()
    {
        return [
            ['shopping_cart', 'random', 100, 100, 24, 5],
            ['shopping_cart', 'random', 60, 80, 15, 4],
            ['shopping_cart', 'random', 75, 60, 18, 3],
            ['shopping_cart', 'all-places', null, null, 0, 5],
            ['shopping_cart', 'all-transitions', null, null, 24, 0],
            ['checkout', 'random', 100, 100, 65, 31],
            ['checkout', 'random', 60, 80, 39, 25],
            ['checkout', 'random', 40, 65, 26, 21],
        ];
    }

    /**
     * @dataProvider coverageData
     * @param $model
     * @param $generator
     * @param $transitionCoverage
     * @param $placeCoverage
     * @param $transitionCount
     * @param $placeCount
     */
    public function testExecute($model, $generator, $transitionCoverage, $placeCoverage, $transitionCount, $placeCount)
    {
        $command = $this->application->find('mbt:generate-path');
        if ($generator === 'random') {
            /** @var RandomGenerator $randomGenerator */
            $randomGenerator = self::$container->get(RandomGenerator::class);
            $randomGenerator->setTransitionCoverage($transitionCoverage);
            $randomGenerator->setPlaceCoverage($placeCoverage);
        }

        $commandTester = new CommandTester($command);
        $commandTester->execute([
            'command'     => $command->getName(),
            'model'       => $model,
            '--generator' => $generator,
        ]);

        $output = $commandTester->getDisplay();
        $path = unserialize($output);
        $this->assertInstanceOf(Path::class, $path);

        if ($path instanceof Path) {
            $placeInPathCount = count(array_unique(call_user_func_array('array_merge', $path->getAllPlaces())));
            $transitionInPathCount = count(array_unique($path->getAllTransitions()));
            $allPlaces = $path->getAllPlaces();
            if ($generator === 'all-transitions' && array_diff(end($allPlaces), ['home'])) {
                // Sometime, we can't get the path through all transitions, so ignore it.
            } elseif ($generator === 'all-places' && $transitionInPathCount === 1) {
                // Sometime, we can't get the path through all places, so ignore it.
            } elseif ($generator === 'random' && $path->countTransitions() === 300) {
                // Sometime we reach the path length limit, so ignore it.
                $this->assertGreaterThanOrEqual(1, $transitionInPathCount);
                $this->assertGreaterThanOrEqual(1, $placeInPathCount);
            } else {
                $this->assertGreaterThanOrEqual($transitionCount, $transitionInPathCount);
                $this->assertGreaterThanOrEqual($placeCount, $placeInPathCount);<commit_msg>Fix wrong transition count in path<commit_after><?php

namespace Tienvx\Bundle\MbtBundle\Tests\Command;

use Symfony\Component\Console\Tester\CommandTester;
use Tienvx\Bundle\MbtBundle\Generator\RandomGenerator;
use Tienvx\Bundle\MbtBundle\Graph\Path;

class GeneratePathCommandTest extends CommandTestCase
{
    public function coverageData()
    {
        return [
            ['shopping_cart', 'random', 100, 100, 24, 5],
            ['shopping_cart', 'random', 60, 80, 15, 4],
            ['shopping_cart', 'random', 75, 60, 18, 3],
            ['shopping_cart', 'all-places', null, null, 0, 5],
            ['shopping_cart', 'all-transitions', null, null, 24, 0],
            ['checkout', 'random', 100, 100, 65, 31],
            ['checkout', 'random', 60, 80, 39, 25],
            ['checkout', 'random', 40, 65, 26, 21],
        ];
    }

    /**
     * @dataProvider coverageData
     * @param $model
     * @param $generator
     * @param $transitionCoverage
     * @param $placeCoverage
     * @param $transitionCount
     * @param $placeCount
     */
    public function testExecute($model, $generator, $transitionCoverage, $placeCoverage, $transitionCount, $placeCount)
    {
        $command = $this->application->find('mbt:generate-path');
        if ($generator === 'random') {
            /** @var RandomGenerator $randomGenerator */
            $randomGenerator = self::$container->get(RandomGenerator::class);
            $randomGenerator->setTransitionCoverage($transitionCoverage);
            $randomGenerator->setPlaceCoverage($placeCoverage);
        }

        $commandTester = new CommandTester($command);
        $commandTester->execute([
            'command'     => $command->getName(),
            'model'       => $model,
            '--generator' => $generator,
        ]);

        $output = $commandTester->getDisplay();
        $path = unserialize($output);
        $this->assertInstanceOf(Path::class, $path);

        if ($path instanceof Path) {
            $placeInPathCount = count(array_unique(call_user_func_array('array_merge', $path->getAllPlaces())));
            $transitionInPathCount = count(array_unique(array_filter($path->getAllTransitions())));
            $allPlaces = $path->getAllPlaces();
            if ($generator === 'all-transitions' && array_diff(end($allPlaces), ['home'])) {
                // Sometime, we can't get the path through all transitions, so ignore it.
            } elseif ($generator === 'all-places' && $transitionInPathCount === 1) {
                // Sometime, we can't get the path through all places, so ignore it.
            } elseif ($generator === 'random' && $path->countTransitions() === 300) {
                // Sometime we reach the path length limit, so ignore it.
                $this->assertGreaterThanOrEqual(1, $transitionInPathCount);
                $this->assertGreaterThanOrEqual(1, $placeInPathCount);
            } else {
                $this->assertGreaterThanOrEqual($transitionCount, $transitionInPathCount);
                $this->assertGreaterThanOrEqual($placeCount, $placeInPathCount);
{'fraction_non_alphanumeric': 0.14362307067424857, 'fraction_numerical': 0.01949634443541836, 'mean_word_length': 2.6512455516014235, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6190086', 'n_tokens_mistral': 1855, 'n_tokens_neox': 1704, 'n_words': 372}