<commit_before><?php

/*
 * Copyright (c) 2016 Refinery29, Inc.
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */
namespace Refinery29\Sitemap\Test\Unit\Component\News;

use InvalidArgumentException;
use Refinery29\Sitemap\Component\News\News;
use Refinery29\Sitemap\Component\News\NewsInterface;
use Refinery29\Sitemap\Component\News\PublicationInterface;
use Refinery29\Test\Util\Faker\GeneratorTrait;
use ReflectionClass;

class NewsTest extends \PHPUnit_Framework_TestCase
{
    use GeneratorTrait;

    public function testIsFinal()
    {
        $reflectionClass = new ReflectionClass(News::class);

        $this->assertTrue($reflectionClass->isFinal());
    }

    public function testImplementsNewsInterface()
    {
        $reflectionClass = new ReflectionClass(News::class);

        $this->assertTrue($reflectionClass->implementsInterface(NewsInterface::class));
    }

    public function testDefaults()
    {
        $faker = $this->getFaker();

        $news = new News(
            $this->getPublicationMock(),
            $faker->dateTime,
            $faker->sentence()
        );

        $this->assertNull($news->access());

        $this->assertInternalType('array', $news->genres());
        $this->assertCount(0, $news->genres());

        $this->assertInternalType('array', $news->keywords());
        $this->assertCount(0, $news->keywords());

        $this->assertInternalType('array', $news->stockTickers());
        $this->assertCount(0, $news->stockTickers());
    }

    public function testConstructorSetsValues()
    {
        $faker = $this->getFaker();

        $publication = $this->getPublicationMock();
        $publicationDate = $faker->dateTime;
        $title = $faker->sentence();

        $news = new News(
            $publication,
            $publicationDate,
            $title
        );

        $this->assertSame($publication, $news->publication());
        $this->assertNotSame($publicationDate, $news->publicationDate());
        $this->assertSame($title, $news->title());
    }

    public function testWithAccessRejectsInvalidValue()
    {
        $this->setExpectedException(InvalidArgumentException::class);

        $faker = $this->getFaker();

        $access = $faker->sentence();

        $news = new News(
            $this->getPublicationMock(),
            $faker->dateTime,
            $faker->sentence()
        );

        $news->withAccess($access);
    }

    public function testWithAccessClonesObjectAndSetsValue()
    {
        $faker = $this->getFaker();

        $access = $faker->randomElement([
            NewsInterface::ACCESS_REGISTRATION,
            NewsInterface::ACCESS_SUBSCRIPTION,
        ]);

        $news = new News(
            $this->getPublicationMock(),
            $faker->dateTime,
            $faker->sentence()
        );

        $instance = $news->withAccess($access);

        $this->assertInstanceOf(News::class, $instance);
        $this->assertNotSame($news, $instance);
        $this->assertSame($access, $instance->access());
    }

    public function testWithGenresRejectsInvalidValues()
    {
        $this->setExpectedException(InvalidArgumentException::class);

        $faker = $this->getFaker();

        $genres = [
            'foobarbaz',
        ];

        $news = new News(
            $this->getPublicationMock(),
            $faker->dateTime,
            $faker->sentence()
        );

        $news->withGenres($genres);
    }

    public function testWithGenresClonesObjectAndSetsValue()
    {
        $faker = $this->getFaker();

        $genres = $faker->randomElements([
            NewsInterface::GENRE_BLOG,
            NewsInterface::GENRE_OP_ED,
            NewsInterface::GENRE_OPINION,
            NewsInterface::GENRE_SATIRE,
            NewsInterface::GENRE_USER_GENERATED,
        ]);

        $news = new News(
            $this->getPublicationMock(),
            $faker->dateTime,
            $faker->sentence()
        );

        $instance = $news->withGenres($genres);

        $this->assertInstanceOf(News::class, $instance);
        $this->assertNotSame($news, $instance);
        $this->assertSame($genres, $instance->genres());
    }

    public function testWithKeywordsClonesObjectAndSetsValue()
    {
        $faker = $this->getFaker();

        $keywords = $faker->words;

        $news = new News(
            $this->getPublicationMock(),
            $faker->dateTime,
            $faker->sentence()
        );

        $instance = $news->withKeywords($keywords);

        $this->assertInstanceOf(News::class, $instance);
        $this->assertNotSame($news, $instance);
        $this->assertSame($keywords, $instance->keywords());
    }

    public function testWithStockTickersRejectsTooManyValues()
    {
        $this->setExpectedException(InvalidArgumentException::class);

        $faker = $this->getFaker();

        $stockTickers = $faker->words(NewsInterface::STOCK_TICKERS_MAX_COUNT + 1);

        $news = new News(
            $this->getPublicationMock(),
            $faker->dateTime,
            $faker->sentence()
        );

        $news->withStockTickers($stockTickers);
    }

    public function testWithStockTickersClonesObjectAndSetsValue()
    {
        $faker = $this->getFaker();

        $stockTickers = $faker->words(NewsInterface::STOCK_TICKERS_MAX_COUNT);
<commit_msg>Enhancement: Assert that non-string values are rejected<commit_after><?php

/*
 * Copyright (c) 2016 Refinery29, Inc.
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */
namespace Refinery29\Sitemap\Test\Unit\Component\News;

use InvalidArgumentException;
use Refinery29\Sitemap\Component\News\News;
use Refinery29\Sitemap\Component\News\NewsInterface;
use Refinery29\Sitemap\Component\News\PublicationInterface;
use Refinery29\Test\Util\Faker\GeneratorTrait;
use ReflectionClass;

class NewsTest extends \PHPUnit_Framework_TestCase
{
    use GeneratorTrait;

    public function testIsFinal()
    {
        $reflectionClass = new ReflectionClass(News::class);

        $this->assertTrue($reflectionClass->isFinal());
    }

    public function testImplementsNewsInterface()
    {
        $reflectionClass = new ReflectionClass(News::class);

        $this->assertTrue($reflectionClass->implementsInterface(NewsInterface::class));
    }

    public function testDefaults()
    {
        $faker = $this->getFaker();

        $news = new News(
            $this->getPublicationMock(),
            $faker->dateTime,
            $faker->sentence()
        );

        $this->assertNull($news->access());

        $this->assertInternalType('array', $news->genres());
        $this->assertCount(0, $news->genres());

        $this->assertInternalType('array', $news->keywords());
        $this->assertCount(0, $news->keywords());

        $this->assertInternalType('array', $news->stockTickers());
        $this->assertCount(0, $news->stockTickers());
    }

    /**
     * @dataProvider providerInvalidString
     *
     * @param mixed $title
     */
    public function testConstructorRejectsInvalidTitle($title)
    {
        $this->setExpectedException(InvalidArgumentException::class);

        $faker = $this->getFaker();

        new News(
            $this->getPublicationMock(),
            $faker->dateTime,
            $title
        );
    }

    /**
     * @return \Generator
     */
    public function providerInvalidString()
    {
        $faker = $this->getFaker();

        $values = [
            null,
            $faker->boolean(),
            $faker->words,
            $faker->randomNumber(),
            $faker->randomFloat(),
            new \stdClass(),
        ];

        foreach ($values as $value) {
            yield [
                $value,
            ];
        }
    }

    public function testConstructorSetsValues()
    {
        $faker = $this->getFaker();

        $publication = $this->getPublicationMock();
        $publicationDate = $faker->dateTime;
        $title = $faker->sentence();

        $news = new News(
            $publication,
            $publicationDate,
            $title
        );

        $this->assertSame($publication, $news->publication());
        $this->assertNotSame($publicationDate, $news->publicationDate());
        $this->assertSame($title, $news->title());
    }

    public function testWithAccessRejectsInvalidValue()
    {
        $this->setExpectedException(InvalidArgumentException::class);

        $faker = $this->getFaker();

        $access = $faker->sentence();

        $news = new News(
            $this->getPublicationMock(),
            $faker->dateTime,
            $faker->sentence()
        );

        $news->withAccess($access);
    }

    public function testWithAccessClonesObjectAndSetsValue()
    {
        $faker = $this->getFaker();

        $access = $faker->randomElement([
            NewsInterface::ACCESS_REGISTRATION,
            NewsInterface::ACCESS_SUBSCRIPTION,
        ]);

        $news = new News(
            $this->getPublicationMock(),
            $faker->dateTime,
            $faker->sentence()
        );

        $instance = $news->withAccess($access);

        $this->assertInstanceOf(News::class, $instance);
        $this->assertNotSame($news, $instance);
        $this->assertSame($access, $instance->access());
    }

    public function testWithGenresRejectsInvalidValues()
    {
        $this->setExpectedException(InvalidArgumentException::class);

        $faker = $this->getFaker();

        $genres = [
            'foobarbaz',
        ];

        $news = new News(
            $this->getPublicationMock(),
            $faker->dateTime,
            $faker->sentence()
        );

        $news->withGenres($genres);
    }

    public function testWithGenresClonesObjectAndSetsValue()
    {
        $faker = $this->getFaker();

        $genres = $faker->randomElements([
            NewsInterface::GENRE_BLOG,
            NewsInterface::GENRE_OP_ED,
            NewsInterface::GENRE_OPINION,
            NewsInterface::GENRE_SATIRE,
            NewsInterface::GENRE_USER_GENERATED,
        ]);

        $news = new News(
            $this->getPublicationMock(),
            $faker->dateTime,
            $faker->sentence()
        );

        $instance = $news->withGenres($genres);

        $this->assertInstanceOf(News::class, $instance);
        $this->assertNotSame($news, $instance);
        $this->assertSame($genres, $instance->genres());
    }

    /**
     * @dataProvider providerInvalidString
     *
     * @param mixed $keyword
     */
    public function testWithKeywordsRejectsInvalidValues($keyword)
    {
        $this->setExpectedException(InvalidArgumentException::class);

        $faker = $this->getFaker();

        $keywords = [
            $faker->word,
            $faker->word,
            $keyword,
        ];

        $news = new News(
            $this->getPublicationMock(),
            $faker->dateTime,
            $faker->sentence()
        );

        $news->withKeywords($keywords);
    }

    public function testWithKeywordsClonesObjectAndSetsValue()
    {
        $faker = $this->getFaker();

        $keywords = $faker->words;

        $news = new News(
            $this->getPublicationMock(),
            $faker->dateTime,
            $faker->sentence()
        );

        $instance = $news->withKeywords($keywords);

        $this->assertInstanceOf(News::class, $instance);
        $this->assertNotSame($news, $instance);
        $this->assertSame($keywords, $instance->keywords());
    }

    /**
     * @dataProvider providerInvalidString
     *
     * @param mixed $stockTicker
     */
    public function testWithStockTickersRejectsInvalidValues($stockTicker)
    {
        $this->setExpectedException(InvalidArgumentException::class);

        $faker = $this->getFaker();

        $stockTickers = [
            $faker->word,
            $faker->word,
            $stockTicker,
        ];

        $news = new News(
            $this->getPublicationMock(),
            $faker->dateTime,
            $faker->sentence()
        );

        $news->withStockTickers($stockTickers);
    }

    public function testWithStockTickersRejectsTooManyValues()
    {
        $this->setExpectedException(InvalidArgumentException::class);

        $faker = $this->getFaker();

        $stockTickers = $faker->words(NewsInterface::STOCK_TICKERS_MAX_COUNT + 1);

        $news = new News(
            $this->getPublicationMock(),
            $faker->dateTime,
            $faker->sentence()
        );

        $news->withStockTickers($stockTickers);
    }

    public function testWithStockTickersClonesObjectAndSetsValue()
    {
        $faker = $this->getFaker();

        $stockTickers = $faker->words(NewsInterface::STOCK_TICKERS_MAX_COUNT);

{'fraction_non_alphanumeric': 0.1370361763111008, 'fraction_numerical': 0.0030986133705166938, 'mean_word_length': 2.3256053580628544, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 196, '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': '234210', 'n_tokens_mistral': 3790, 'n_tokens_neox': 3600, 'n_words': 545}