<commit_before><?php

namespace JimLind\Pie7o\Tests;

use JimLind\Pie7o\TweetFactory;
use phpmock\spy\Spy;

class TweetFactoryTest extends \PHPUnit_Framework_TestCase
{
    public function testEmptyBuildTweet()
    {
        $tweet = TweetFactory::buildTweet();

        $messageStream = $tweet->getMessage();
        $mediaStream   = $tweet->getMedia();

        $this->assertSame('', $messageStream->getContents());
        $this->assertNull($mediaStream);
    }

    public function testBuildTweetWithMessage()
    {
        $message = (string) rand();
        $tweet   = TweetFactory::buildTweet($message);

        $messageStream = $tweet->getMessage();
        $mediaStream   = $tweet->getMedia();

        $this->assertSame($message, $messageStream->getContents());
        $this->assertNull($mediaStream);
    }

    // TODO: test for file doesn't exists.

    public function testBuildTweetWithFile()
    {
        $filePath = (string) rand();

        $fileExistsSpy = new Spy('JimLind\Pie7o', 'file_exists', [$this, 'returnTrue']);
        $fileExistsSpy->enable();

        $fileOpenSpy = new Spy('JimLind\Pie7o', 'fopen', [$this, 'returnFileHandle']);
        $fileOpenSpy->enable();

        $tweet = TweetFactory::buildTweet('', $filePath);
        $media = $tweet->getMedia();
        $this->assertEquals('fileStart '.$filePath.' fileEnd', $media->getContents());

        $existsInvocationList = $fileExistsSpy->getInvocations();
        $existsArgumentList   = $existsInvocationList[0]->getArguments();
        $this->assertEquals($filePath, $existsArgumentList[0]);

        $openInvocationList = $fileOpenSpy->getInvocations();
        $openArgumentList   = $openInvocationList[0]->getArguments();
        $this->assertEquals($filePath, $openArgumentList[0]);
    }

    public function returnTrue()
    {
        return true;
    }

    public function returnFileHandle($input)
    {
        $handler =  fopen('php://temp', 'r+');
        fputs($handler, 'fileStart '.$input.' fileEnd');
        rewind($handler);

        return $handler;
    }
}<commit_msg>Finish tests for TweetFactory.<commit_after><?php

namespace JimLind\Pie7o\Tests;

use JimLind\Pie7o\TweetFactory;
use phpmock\spy\Spy;

class TweetFactoryTest extends \PHPUnit_Framework_TestCase
{
    public function testEmptyBuildTweet()
    {
        $tweet = TweetFactory::buildTweet();

        $messageStream = $tweet->getMessage();
        $mediaStream   = $tweet->getMedia();

        $this->assertSame('', $messageStream->getContents());
        $this->assertNull($mediaStream);
    }

    public function testBuildTweetWithMessage()
    {
        $message = (string) rand();
        $tweet   = TweetFactory::buildTweet($message);

        $messageStream = $tweet->getMessage();
        $mediaStream   = $tweet->getMedia();

        $this->assertSame($message, $messageStream->getContents());
        $this->assertNull($mediaStream);
    }

    public function testBuildTweetWithoutFile()
    {
        $filePath = (string) rand();

        $fileExistsSpy = new Spy('JimLind\Pie7o', 'file_exists', [$this, 'returnFalse']);
        $fileExistsSpy->enable();

        $tweet = TweetFactory::buildTweet('', $filePath);
        $mediaStream = $tweet->getMedia();
        $this->assertNull($mediaStream);

        $existsInvocationList = $fileExistsSpy->getInvocations();
        $existsArgumentList   = $existsInvocationList[0]->getArguments();
        $this->assertEquals($filePath, $existsArgumentList[0]);

        $fileExistsSpy->disable();
    }

    public function testBuildTweetWithFile()
    {
        $filePath = (string) rand();

        $fileExistsSpy = new Spy('JimLind\Pie7o', 'file_exists', [$this, 'returnTrue']);
        $fileExistsSpy->enable();

        $fileOpenSpy = new Spy('JimLind\Pie7o', 'fopen', [$this, 'returnFileHandle']);
        $fileOpenSpy->enable();

        $tweet = TweetFactory::buildTweet('', $filePath);
        $mediaStream = $tweet->getMedia();
        $this->assertEquals('fileStart '.$filePath.' fileEnd', $mediaStream->getContents());

        $existsInvocationList = $fileExistsSpy->getInvocations();
        $existsArgumentList   = $existsInvocationList[0]->getArguments();
        $this->assertEquals($filePath, $existsArgumentList[0]);

        $openInvocationList = $fileOpenSpy->getInvocations();
        $openArgumentList   = $openInvocationList[0]->getArguments();
        $this->assertEquals($filePath, $openArgumentList[0]);

        $fileExistsSpy->disable();
        $fileOpenSpy->disable();
    }

    public function returnTrue()
    {
        return true;
    }

    public function returnFalse()
    {
        return false;
    }

    public function returnFileHandle($input)
    {
        $handler =  fopen('php://temp', 'r+');
        fputs($handler, 'fileStart '.$input.' fileEnd');
        rewind($handler);

        return $handler;
    }
}
{'fraction_non_alphanumeric': 0.1539877300613497, 'fraction_numerical': 0.003885480572597137, 'mean_word_length': 3.117003367003367, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 54, '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': '3486161', 'n_tokens_mistral': 1507, 'n_tokens_neox': 1439, 'n_words': 236}