<commit_before><?php

namespace LizardsAndPumpkins\MagentoConnector\XmlBuilder;

class ListingBuilderTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @param string $urlKey
     * @dataProvider provideInvalidUrlKey
     */
    public function testExceptionForInvalidUrlKeyIsThrown($urlKey)
    {
        $this->setExpectedExceptionRegExp(\InvalidArgumentException::class);
        $website = 'ma';
        $locale = 'en_DK';
        ListingBuilder::create($urlKey, $website, $locale);
    }

    /**
     * @return array[]
     */
    public function provideInvalidUrlKey()
    {
        return [
            ['@'],
            ['<EMAIL>'],
            ['german länguage'],
            ['category and products'],
            [''],
            [new \stdClass()],
            [12],
        ];
    }

    public function testUrlKeyHasNoLeadingSlash()
    {
        $urlKey = 'sneaker';
        $urlKeyWithLeadingSlash = '/' . $urlKey;

        $website = 'ma';
        $locale = 'en_DK';
        $listingBuilder = ListingBuilder::create($urlKeyWithLeadingSlash, $website, $locale);
        $xml = $listingBuilder->buildXml()->getXml();

        $this->assertContains($urlKeyWithLeadingSlash, $xml);
    }

    /**
     * @return array[]
     */
    public function provideValidUrlKey()
    {
        return [
            ['valid-url-key'],
            ['this"$()-lala-*!'],
            ['sneakershop/lala'],
        ];
    }

    /**
     * @param string $locale
     * @dataProvider provideInvalidLocale
     */
    public function testExceptionForInvalidLocaleIsThrown($locale)
    {
        $this->setExpectedException(\InvalidArgumentException::class);
        $website = 'ma';
        ListingBuilder::create('valid-url-key', $website, $locale);
    }

    /**
     * @return array[]
     */
    public function provideInvalidLocale()
    {
        return [
            ['german'],
            ['ISO-8859-1'],
            ['us'],
            [''],
            [new \stdClass()],
            [['asd']],
        ];
    }

    public function testUrlKeyAttributeIsAddedToListingNodes()
    {
        $website = '42';
        $locale = 'cz_CN';
        $listingBuilder = ListingBuilder::create('urlkey', $website, $locale);
        $xmlString = $listingBuilder->buildXml();
        $this->assertInstanceOf(XmlString::class, $xmlString);
        $xml = $xmlString->getXml();
        $this->assertRegExp(
            '#<listing .*url_key="urlkey".*?>#',
            $xml,
            'UrlKey as attribute is missing on listing node'
        );
    }

    /**
     * @depends testUrlKeyAttributeIsAddedToListingNodes
     */
    public function testLocaleAttributeIsAddedToListingNodes()
    {
        $website = 'fu';
        $locale = 'cs_CZ';
        $listingBuilder = ListingBuilder::create('urlkey', $website, $locale);
        $xmlString = $listingBuilder->buildXml();
        $this->assertInstanceOf(XmlString::class, $xmlString);
        $xml = $xmlString->getXml();
        $this->assertRegExp(
            '#<listing .*locale="cs_CZ".*?>#',
            $xml,
            'Locale as attribute is missing on listing node'
        );
    }

    /**
     * @depends testUrlKeyAttributeIsAddedToListingNodes
     */
    public function testWebsiteAttributeIsAddedToListingNodes()
    {
        $website = 'ru_de';
        $locale = 'en_DK';
        $listingBuilder = ListingBuilder::create('urlkey', $website, $locale);
        $xmlString = $listingBuilder->buildXml();
        $this->assertInstanceOf(XmlString::class, $xmlString);
        $xml = $xmlString->getXml();
        $this->assertRegExp(
            '#<listing .*website="ru_de".*?>#',
            $xml,
            'Website as attribute is missing on listing node'
        );
    }

    public function testExceptionOnInvalidAttributeIsThrown()
    {
        $this->setExpectedException(\InvalidArgumentException::class);
        $website = 'ru_de';
        $locale = 'en_DK';
        $listingBuilder = ListingBuilder::create('urlkey', $website, $locale);
        $listingBuilder->addFilterCriterion(new \stdClass(), 'Equal', 'sale');
    }

    public function testExceptionForInvalidOperationOnFilterIsThrown()
    {
        $this->setExpectedException(\InvalidArgumentException::class);
        $locale = 'en_DK';
        $website = 'ru_de';
        $listingBuilder = ListingBuilder::create('urlkey', $website, $locale);
        $listingBuilder->addFilterCriterion('category', new \stdClass(), 'sale');
    }

    public function testExceptionForInvalidAttributeOnFilterIsThrown()
    {
        $this->setExpectedException(\InvalidArgumentException::class);
        $website = 'ru_de';
        $locale = 'en_DK';
        $listingBuilder = ListingBuilder::create('urlkey', $website, $locale);
        $listingBuilder->addFilterCriterion(new \stdClass(), 'Equal', 'sale');
    }

    /**
     * @param string $validAttribute
     * @param string $validOperation
     * @param string $validValue
     * @dataProvider validFilterProvider
     */
    public function testOneFilterForListingIsCreated($validAttribute, $validOperation, $validValue)
    {
        $website = 'ru_de';
        $locale = 'en_DK';
        $listingBuilder = ListingBuilder::create('urlkey', $website, $locale);
        $listingBuilder->addFilterCriterion($validAttribute, $validOperation, $validValue);
        $result = $listingBuilder->buildXml()->getXml();

        $expectedXml = sprintf('<%1$s is="%2$s">%3$s</%1$s>', $validAttribute, $validOperation, $validValue);

        $this->assertContains($expectedXml, $result);
    }

    /**
     * @return string[]
     */
    public function validFilterProvider()
    {
        $validOperations = [
            'Equal',
            'GreaterOrEqualThan',
            'GreaterThan',
            'LessOrEqualThan',
            'LessThan',
            'Like',
            'NotEqual',
        ];

        return array_map(function ($operation) {
            return [
                'attribute' => 'category',
                'operation' => $operation,
                'value' => 'sale',
            ];
        }, $validOperations);
    }

    public function testFilterWithLeadingSlashIsCreated()
    {
        $website = 'ru_de';
        $locale = 'en_DK';
        $listingBuilder = ListingBuilder::create('urlkey', $website, $locale);
        $urlKey = 'sneaker';
        $urlKeyWithLeadingSlash = '/' . $urlKey;
        $listingBuilder->addFilterCriterion('category', 'Equal', $urlKeyWithLeadingSlash);
        $xml = $listingBuilder->buildXml()->getXml();
        $this->assertContains("<category is=\"Equal\">$urlKeyWithLeadingSlash</category>", $xml);
    }

    public function testXmlContainsOneCondition()
    {
        $xml = '<criteria type="and"><category is="Equal">accessoires</category></criteria>';

        $category = 'accessoires';
        $website = 'ru_de';
        $locale = 'en_DK';
        $listingBuilder = ListingBuilder::create($category, $website, $locale);
        $listingBuilder->addFilterCriterion('category', 'Equal', $category);
        $this->assertContains($xml, $listingBuilder->buildXml()->getXml());
    }

    public function testXmlContainsMultipleConditions()
    {
        $xml =
            '<criteria type="and"><category is="Equal">accessoires</category><stock_qty is="GreaterThan">0</stock_qty></criteria>';

        $category = 'accessoires';
        $website = 'ru_de';
        $locale = 'en_DK';
        $listingBuilder = ListingBuilder::create($category, $website, $locale);
        $listingBuilder->addFilterCriterion('category', 'Equal', $category);
        $listingBuilder->addFilterCriterion('stock_qty', 'GreaterThan', '0');
        $this->assertContains($xml, $listingBuilder->buildXml()->getXml());
    }
}
<commit_msg>Issue #214: Fix return type<commit_after><?php

namespace LizardsAndPumpkins\MagentoConnector\XmlBuilder;

class ListingBuilderTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @param string $urlKey
     * @dataProvider provideInvalidUrlKey
     */
    public function testExceptionForInvalidUrlKeyIsThrown($urlKey)
    {
        $this->setExpectedExceptionRegExp(\InvalidArgumentException::class);
        $website = 'ma';
        $locale = 'en_DK';
        ListingBuilder::create($urlKey, $website, $locale);
    }

    /**
     * @return array[]
     */
    public function provideInvalidUrlKey()
    {
        return [
            ['@'],
            ['<EMAIL>'],
            ['german länguage'],
            ['category and products'],
            [''],
            [new \stdClass()],
            [12],
        ];
    }

    public function testUrlKeyHasNoLeadingSlash()
    {
        $urlKey = 'sneaker';
        $urlKeyWithLeadingSlash = '/' . $urlKey;

        $website = 'ma';
        $locale = 'en_DK';
        $listingBuilder = ListingBuilder::create($urlKeyWithLeadingSlash, $website, $locale);
        $xml = $listingBuilder->buildXml()->getXml();

        $this->assertContains($urlKeyWithLeadingSlash, $xml);
    }

    /**
     * @return array[]
     */
    public function provideValidUrlKey()
    {
        return [
            ['valid-url-key'],
            ['this"$()-lala-*!'],
            ['sneakershop/lala'],
        ];
    }

    /**
     * @param string $locale
     * @dataProvider provideInvalidLocale
     */
    public function testExceptionForInvalidLocaleIsThrown($locale)
    {
        $this->setExpectedException(\InvalidArgumentException::class);
        $website = 'ma';
        ListingBuilder::create('valid-url-key', $website, $locale);
    }

    /**
     * @return array[]
     */
    public function provideInvalidLocale()
    {
        return [
            ['german'],
            ['ISO-8859-1'],
            ['us'],
            [''],
            [new \stdClass()],
            [['asd']],
        ];
    }

    public function testUrlKeyAttributeIsAddedToListingNodes()
    {
        $website = '42';
        $locale = 'cz_CN';
        $listingBuilder = ListingBuilder::create('urlkey', $website, $locale);
        $xmlString = $listingBuilder->buildXml();
        $this->assertInstanceOf(XmlString::class, $xmlString);
        $xml = $xmlString->getXml();
        $this->assertRegExp(
            '#<listing .*url_key="urlkey".*?>#',
            $xml,
            'UrlKey as attribute is missing on listing node'
        );
    }

    /**
     * @depends testUrlKeyAttributeIsAddedToListingNodes
     */
    public function testLocaleAttributeIsAddedToListingNodes()
    {
        $website = 'fu';
        $locale = 'cs_CZ';
        $listingBuilder = ListingBuilder::create('urlkey', $website, $locale);
        $xmlString = $listingBuilder->buildXml();
        $this->assertInstanceOf(XmlString::class, $xmlString);
        $xml = $xmlString->getXml();
        $this->assertRegExp(
            '#<listing .*locale="cs_CZ".*?>#',
            $xml,
            'Locale as attribute is missing on listing node'
        );
    }

    /**
     * @depends testUrlKeyAttributeIsAddedToListingNodes
     */
    public function testWebsiteAttributeIsAddedToListingNodes()
    {
        $website = 'ru_de';
        $locale = 'en_DK';
        $listingBuilder = ListingBuilder::create('urlkey', $website, $locale);
        $xmlString = $listingBuilder->buildXml();
        $this->assertInstanceOf(XmlString::class, $xmlString);
        $xml = $xmlString->getXml();
        $this->assertRegExp(
            '#<listing .*website="ru_de".*?>#',
            $xml,
            'Website as attribute is missing on listing node'
        );
    }

    public function testExceptionOnInvalidAttributeIsThrown()
    {
        $this->setExpectedException(\InvalidArgumentException::class);
        $website = 'ru_de';
        $locale = 'en_DK';
        $listingBuilder = ListingBuilder::create('urlkey', $website, $locale);
        $listingBuilder->addFilterCriterion(new \stdClass(), 'Equal', 'sale');
    }

    public function testExceptionForInvalidOperationOnFilterIsThrown()
    {
        $this->setExpectedException(\InvalidArgumentException::class);
        $locale = 'en_DK';
        $website = 'ru_de';
        $listingBuilder = ListingBuilder::create('urlkey', $website, $locale);
        $listingBuilder->addFilterCriterion('category', new \stdClass(), 'sale');
    }

    public function testExceptionForInvalidAttributeOnFilterIsThrown()
    {
        $this->setExpectedException(\InvalidArgumentException::class);
        $website = 'ru_de';
        $locale = 'en_DK';
        $listingBuilder = ListingBuilder::create('urlkey', $website, $locale);
        $listingBuilder->addFilterCriterion(new \stdClass(), 'Equal', 'sale');
    }

    /**
     * @param string $validAttribute
     * @param string $validOperation
     * @param string $validValue
     * @dataProvider validFilterProvider
     */
    public function testOneFilterForListingIsCreated($validAttribute, $validOperation, $validValue)
    {
        $website = 'ru_de';
        $locale = 'en_DK';
        $listingBuilder = ListingBuilder::create('urlkey', $website, $locale);
        $listingBuilder->addFilterCriterion($validAttribute, $validOperation, $validValue);
        $result = $listingBuilder->buildXml()->getXml();

        $expectedXml = sprintf('<%1$s is="%2$s">%3$s</%1$s>', $validAttribute, $validOperation, $validValue);

        $this->assertContains($expectedXml, $result);
    }

    /**
     * @return array[]
     */
    public function validFilterProvider()
    {
        $validOperations = [
            'Equal',
            'GreaterOrEqualThan',
            'GreaterThan',
            'LessOrEqualThan',
            'LessThan',
            'Like',
            'NotEqual',
        ];

        return array_map(function ($operation) {
            return [
                'attribute' => 'category',
                'operation' => $operation,
                'value' => 'sale',
            ];
        }, $validOperations);
    }

    public function testFilterWithLeadingSlashIsCreated()
    {
        $website = 'ru_de';
        $locale = 'en_DK';
        $listingBuilder = ListingBuilder::create('urlkey', $website, $locale);
        $urlKey = 'sneaker';
        $urlKeyWithLeadingSlash = '/' . $urlKey;
        $listingBuilder->addFilterCriterion('category', 'Equal', $urlKeyWithLeadingSlash);
        $xml = $listingBuilder->buildXml()->getXml();
        $this->assertContains("<category is=\"Equal\">$urlKeyWithLeadingSlash</category>", $xml);
    }

    public function testXmlContainsOneCondition()
    {
        $xml = '<criteria type="and"><category is="Equal">accessoires</category></criteria>';

        $category = 'accessoires';
        $website = 'ru_de';
        $locale = 'en_DK';
        $listingBuilder = ListingBuilder::create($category, $website, $locale);
        $listingBuilder->addFilterCriterion('category', 'Equal', $category);
        $this->assertContains($xml, $listingBuilder->buildXml()->getXml());
    }

    public function testXmlContainsMultipleConditions()
    {
        $xml =
            '<criteria type="and"><category is="Equal">accessoires</category><stock_qty is="GreaterThan">0</stock_qty></criteria>';

        $category = 'accessoires';
        $website = 'ru_de';
        $locale = 'en_DK';
        $listingBuilder = ListingBuilder::create($category, $website, $locale);
        $listingBuilder->addFilterCriterion('category', 'Equal', $category);
        $listingBuilder->addFilterCriterion('stock_qty', 'GreaterThan', '0');
        $this->assertContains($xml, $listingBuilder->buildXml()->getXml());
    }
}

{'fraction_non_alphanumeric': 0.1489676464912845, 'fraction_numerical': 0.002122595999228147, 'mean_word_length': 2.5264232252211385, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 125, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 46}, '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': '2885870', 'n_tokens_mistral': 4566, 'n_tokens_neox': 4378, 'n_words': 730}