<commit_before><?php

namespace Tienvx\Bundle\MbtBundle\Tests\Command;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Workflow\Registry;
use Tienvx\Bundle\MbtBundle\Command\RunStepsCommand;
use Tienvx\Bundle\MbtBundle\Service\GraphBuilder;
use Tienvx\Bundle\MbtBundle\Service\ModelRegistry;
use Tienvx\Bundle\MbtBundle\Service\PathReducerManager;
use Tienvx\Bundle\MbtBundle\Service\PathRunner;

class RunStepsCommandTest extends CommandTestCase
{
    public function testRun()
    {
        $kernel = static::bootKernel();

        $modelRegistry = self::$container->get(ModelRegistry::class);
        $graphBuilder = self::$container->get(GraphBuilder::class);
        $pathRunner = self::$container->get(PathRunner::class);
        $pathReducerManager = self::$container->get(PathReducerManager::class);
        $workflows = self::$container->get(Registry::class);

        $application = new Application($kernel);
        $application->add(new RunStepsCommand($modelRegistry, $graphBuilder, $pathRunner, $pathReducerManager, $workflows));

        $command = $application->find('mbt:run-steps');
        $output = $this->runCommand($command, 'home viewProductFromHome(product=49) product addFromProduct() product viewCartFromProduct() cart');
        $this->assertEquals('', $output);
        $output = $this->runCommand($command, 'home viewAnyCategoryFromHome(category=24) category addFromCategory(product=29) category viewProductFromCategory(product=40) product checkoutFromProduct() checkout');
        $this->assertEquals('', $output);
        $output = $this->runCommand($command, 'home addFromHome(product=49) home viewAnyCategoryFromHome(category=33) category addFromCategory(product=31) category viewCartFromCategory() cart update(product=31) cart remove(product=31) cart checkoutFromCart() checkout backToHomeFromCheckout() home');
        $this->assertContains('Found a bug: You added an out-of-stock product into cart! Can not checkout', $output);
        $output = $this->runCommand($command, 'home addFromHome(product=40) home viewAnyCategoryFromHome(category=57) category addFromCategory(product=49) category checkoutFromCategory() checkout', 'greedy');
        $this->assertEquals('Found a bug: You added an out-of-stock product into cart! Can not checkout
+--------+----------------------------------------------------------------+-------------------+
| Steps to reproduce                                                                          |
+--------+----------------------------------------------------------------+-------------------+
| Step   | Label                                                          | Data Input        |
+--------+----------------------------------------------------------------+-------------------+
| 1      | From home page, choose a random category and open it           | {"category":"57"} |
| 2      | From category page, choose a random product and add it to cart | {"product":"49"}  |
| 3      | From category page, open checkout page                         | []                |
+--------+----------------------------------------------------------------+-------------------+
', $output);
        $output = $this->runCommand($command, 'home viewAnyCategoryFromHome(category=33) category addFromCategory(product=31) category viewCartFromCategory() cart backToHomeFromCart() home viewAnyCategoryFromHome(category=57) category viewProductFromCategory(product=49) product addFromProduct() product checkoutFromProduct() checkout', 'binary');
        $this->assertEquals('Found a bug: You added an out-of-stock product into cart! Can not checkout
+--------+---------------------------------------------------------+-------------------+
| Steps to reproduce                                                                   |
+--------+---------------------------------------------------------+-------------------+
| Step   | Label                                                   | Data Input        |
+--------+---------------------------------------------------------+-------------------+
| 1      | From home page, choose a random category and open it    | {"category":"57"} |
| 2      | From category page, choose a random product and open it | {"product":"49"}  |
| 3      | From product page, add it to cart                       | []                |
| 4      | From product page, open checkout page                   | []                |
+--------+---------------------------------------------------------+-------------------+
', $output);
        $output = $this->runCommand($command, 'home viewAnyCategoryFromHome(category=34) category viewOtherCategory(category=57) category addFromCategory(product=49) category viewOtherCategory(category=34) category viewProductFromCategory(product=48) product backToHomeFromProduct() home checkoutFromHome() checkout', 'binary');
        if (strpos($output, '{"category":"34"}')) {
            $this->assertEquals('Found a bug: You added an out-of-stock product into cart! Can not checkout
+--------+----------------------------------------------------------------+-------------------+
| Steps to reproduce                                                                          |
+--------+----------------------------------------------------------------+-------------------+
| Step   | Label                                                          | Data Input        |
+--------+----------------------------------------------------------------+-------------------+
| 1      | From home page, choose a random category and open it           | {"category":"34"} |
| 2      | From category page, choose a random category and open it       | {"category":"57"} |
| 3      | From category page, choose a random product and add it to cart | {"product":"49"}  |
| 4      | From category page, open checkout page                         | []                |
+--------+----------------------------------------------------------------+-------------------+
', $output);
        }
        else {
            $this->assertEquals('Found a bug: You added an out-of-stock product into cart! Can not checkout
Steps to reproduce:
+------+----------------------------------------------------------------+-------------------+
| Step | Label                                                          | Data Input        |
+------+----------------------------------------------------------------+-------------------+
| 1    | From home page, choose a random category and open it           | {"category":"57"} |
| 2    | From category page, choose a random product and add it to cart | {"product":"49"}  |
| 3    | From category page, open checkout page                         | []                |
+------+----------------------------------------------------------------+-------------------+
', $output);
        }
        $output = $this->runCommand($command, 'home viewCartFromHome() cart backToHomeFromCart() home viewAnyCategoryFromHome(category=57) category addFromCategory(product=49) category viewOtherCategory(category=25_28) category viewOtherCategory(category=20) category checkoutFromCategory() checkout', 'greedy');
        $this->assertEquals('Found a bug: You added an out-of-stock product into cart! Can not checkout
+--------+----------------------------------------------------------------+-------------------+
| Steps to reproduce                                                                          |
+--------+----------------------------------------------------------------+-------------------+
| Step   | Label                                                          | Data Input        |
+--------+----------------------------------------------------------------+-------------------+
| 1      | From home page, choose a random category and open it           | {"category":"57"} |
| 2      | From category page, choose a random product and add it to cart | {"product":"49"}  |
| 3      | From category page, open checkout page                         | []                |
+--------+----------------------------------------------------------------+-------------------+
', $output);
        $output = $this->runCommand($command, 'home viewAnyCategoryFromHome(category=34) category viewOtherCategory(category=57) category addFromCategory(product=49) category viewOtherCategory(category=34) category viewProductFromCategory(product=48) product backToHomeFromProduct() home checkoutFromHome() checkout', 'greedy');
        if (strpos($output, '{"category":"34"}')) {
            $this->assertEquals('Found a bug: You added an out-of-stock product into cart! Can not checkout
+--------+----------------------------------------------------------------+-------------------+
| Steps to reproduce                                                                          |
+--------+----------------------------------------------------------------+-------------------+
| Step   | Label                                                          | Data Input        |
+--------+----------------------------------------------------------------+-------------------+
| 1      | From home page, choose a random category and open it           | {"category":"34"} |
| 2      | From category page, choose a random category and open it       | {"category":"57"} |
| 3      | From category page, choose a random product and add it to cart | {"product":"49"}  |
| 4      | From category page, open checkout page                         | []                |
+--------+----------------------------------------------------------------+-------------------+
', $output);
        }
        else {
            $this->assertEquals('Found a bug: You added an out-of-stock product into cart! Can not checkout
Steps to reproduce:
+------+----------------------------------------------------------------+-------------------+
| Step | Label                                                          | Data Input        |
+------+----------------------------------------------------------------+-------------------+
| 1    | From home page, choose a random category and open it           | {"category":"57"} |
| 2    | From category page, choose a random product and add it to cart | {"product":"49"}  |
| 3    | From category page, open checkout page                         | []                |
+------+----------------------------------------------------------------+-------------------+
', $output);
        }
    }

    public function runCommand(Command $command, $steps, $reducer = null)
    {
        $commandTester = new CommandTester($command);
        $input = [
            'command'        => $command->getName(),
            'model'          => 'shopping_cart',<commit_msg>Remove condition that rarely happen<commit_after><?php

namespace Tienvx\Bundle\MbtBundle\Tests\Command;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Workflow\Registry;
use Tienvx\Bundle\MbtBundle\Command\RunStepsCommand;
use Tienvx\Bundle\MbtBundle\Service\GraphBuilder;
use Tienvx\Bundle\MbtBundle\Service\ModelRegistry;
use Tienvx\Bundle\MbtBundle\Service\PathReducerManager;
use Tienvx\Bundle\MbtBundle\Service\PathRunner;

class RunStepsCommandTest extends CommandTestCase
{
    public function testRun()
    {
        $kernel = static::bootKernel();

        $modelRegistry = self::$container->get(ModelRegistry::class);
        $graphBuilder = self::$container->get(GraphBuilder::class);
        $pathRunner = self::$container->get(PathRunner::class);
        $pathReducerManager = self::$container->get(PathReducerManager::class);
        $workflows = self::$container->get(Registry::class);

        $application = new Application($kernel);
        $application->add(new RunStepsCommand($modelRegistry, $graphBuilder, $pathRunner, $pathReducerManager, $workflows));

        $command = $application->find('mbt:run-steps');
        $output = $this->runCommand($command, 'home viewProductFromHome(product=49) product addFromProduct() product viewCartFromProduct() cart');
        $this->assertEquals('', $output);
        $output = $this->runCommand($command, 'home viewAnyCategoryFromHome(category=24) category addFromCategory(product=29) category viewProductFromCategory(product=40) product checkoutFromProduct() checkout');
        $this->assertEquals('', $output);
        $output = $this->runCommand($command, 'home addFromHome(product=49) home viewAnyCategoryFromHome(category=33) category addFromCategory(product=31) category viewCartFromCategory() cart update(product=31) cart remove(product=31) cart checkoutFromCart() checkout backToHomeFromCheckout() home');
        $this->assertContains('Found a bug: You added an out-of-stock product into cart! Can not checkout', $output);
        $output = $this->runCommand($command, 'home addFromHome(product=40) home viewAnyCategoryFromHome(category=57) category addFromCategory(product=49) category checkoutFromCategory() checkout', 'greedy');
        $this->assertEquals('Found a bug: You added an out-of-stock product into cart! Can not checkout
+--------+----------------------------------------------------------------+-------------------+
| Steps to reproduce                                                                          |
+--------+----------------------------------------------------------------+-------------------+
| Step   | Label                                                          | Data Input        |
+--------+----------------------------------------------------------------+-------------------+
| 1      | From home page, choose a random category and open it           | {"category":"57"} |
| 2      | From category page, choose a random product and add it to cart | {"product":"49"}  |
| 3      | From category page, open checkout page                         | []                |
+--------+----------------------------------------------------------------+-------------------+
', $output);
        $output = $this->runCommand($command, 'home viewAnyCategoryFromHome(category=33) category addFromCategory(product=31) category viewCartFromCategory() cart backToHomeFromCart() home viewAnyCategoryFromHome(category=57) category viewProductFromCategory(product=49) product addFromProduct() product checkoutFromProduct() checkout', 'binary');
        $this->assertEquals('Found a bug: You added an out-of-stock product into cart! Can not checkout
+--------+---------------------------------------------------------+-------------------+
| Steps to reproduce                                                                   |
+--------+---------------------------------------------------------+-------------------+
| Step   | Label                                                   | Data Input        |
+--------+---------------------------------------------------------+-------------------+
| 1      | From home page, choose a random category and open it    | {"category":"57"} |
| 2      | From category page, choose a random product and open it | {"product":"49"}  |
| 3      | From product page, add it to cart                       | []                |
| 4      | From product page, open checkout page                   | []                |
+--------+---------------------------------------------------------+-------------------+
', $output);
        $output = $this->runCommand($command, 'home viewAnyCategoryFromHome(category=34) category viewOtherCategory(category=57) category addFromCategory(product=49) category viewOtherCategory(category=34) category viewProductFromCategory(product=48) product backToHomeFromProduct() home checkoutFromHome() checkout', 'binary');
        $this->assertEquals('Found a bug: You added an out-of-stock product into cart! Can not checkout
+--------+----------------------------------------------------------------+-------------------+
| Steps to reproduce                                                                          |
+--------+----------------------------------------------------------------+-------------------+
| Step   | Label                                                          | Data Input        |
+--------+----------------------------------------------------------------+-------------------+
| 1      | From home page, choose a random category and open it           | {"category":"34"} |
| 2      | From category page, choose a random category and open it       | {"category":"57"} |
| 3      | From category page, choose a random product and add it to cart | {"product":"49"}  |
| 4      | From category page, open checkout page                         | []                |
+--------+----------------------------------------------------------------+-------------------+
', $output);
        $output = $this->runCommand($command, 'home viewCartFromHome() cart backToHomeFromCart() home viewAnyCategoryFromHome(category=57) category addFromCategory(product=49) category viewOtherCategory(category=25_28) category viewOtherCategory(category=20) category checkoutFromCategory() checkout', 'greedy');
        $this->assertEquals('Found a bug: You added an out-of-stock product into cart! Can not checkout
+--------+----------------------------------------------------------------+-------------------+
| Steps to reproduce                                                                          |
+--------+----------------------------------------------------------------+-------------------+
| Step   | Label                                                          | Data Input        |
+--------+----------------------------------------------------------------+-------------------+
| 1      | From home page, choose a random category and open it           | {"category":"57"} |
| 2      | From category page, choose a random product and add it to cart | {"product":"49"}  |
| 3      | From category page, open checkout page                         | []                |
+--------+----------------------------------------------------------------+-------------------+
', $output);
        $output = $this->runCommand($command, 'home viewAnyCategoryFromHome(category=34) category viewOtherCategory(category=57) category addFromCategory(product=49) category viewOtherCategory(category=34) category viewProductFromCategory(product=48) product backToHomeFromProduct() home checkoutFromHome() checkout', 'greedy');
        $this->assertEquals('Found a bug: You added an out-of-stock product into cart! Can not checkout
+--------+----------------------------------------------------------------+-------------------+
| Steps to reproduce                                                                          |
+--------+----------------------------------------------------------------+-------------------+
| Step   | Label                                                          | Data Input        |
+--------+----------------------------------------------------------------+-------------------+
| 1      | From home page, choose a random category and open it           | {"category":"34"} |
| 2      | From category page, choose a random category and open it       | {"category":"57"} |
| 3      | From category page, choose a random product and add it to cart | {"product":"49"}  |
| 4      | From category page, open checkout page                         | []                |
+--------+----------------------------------------------------------------+-------------------+
', $output);
    }

    public function runCommand(Command $command, $steps, $reducer = null)
    {
        $commandTester = new CommandTester($command);
        $input = [
            'command'        => $command->getName(),
            'model'          => 'shopping_cart',
{'fraction_non_alphanumeric': 0.2997277951406392, 'fraction_numerical': 0.011392277447323318, 'mean_word_length': 1.253663523798705, 'pattern_counts': {'":': 30, '<': 5, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 78, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7573537', 'n_tokens_mistral': 4139, 'n_tokens_neox': 3842, 'n_words': 1137}