<commit_before><?php

/**
 * Created by PhpStorm.
 * User: talv
 * Date: 20/07/16
 * Time: 17:22.
 */
class PostFormFieldsTest extends TestCase
{
    /**
     * @var \Illuminate\Support\Collection
     */
    private $filesCreated;

    public function setUp()
    {
        parent::setUp();

        $this->filesCreated = collect();
    }

    public function tearDown()
    {
        $this->filesCreated->each(function ($file) {
            try {
                unlink($file);
            } catch (Exception $e) {
            }
        });

        parent::tearDown();
    }

    /**
     * Allow the private getPostLayouts method to become accessible / invokable.
     *
     * @return ReflectionMethod
     */
    private function getPostLayoutsAsPublic()
    {
        $reflection = new ReflectionClass(\Easel\Http\Jobs\PostFormFields::class);
        $getPostLayouts = $reflection->getMethod('getPostLayouts');
        $getPostLayouts->setAccessible(true);

        return $getPostLayouts;
    }

    /**
     * @param $fileName
     *
     * @return bool
     */
    private function createNewBlogTemplateFile($fileName)
    {
        $path = $this->createPathFromFileName($fileName);

        return $this->createFile($path);
    }

    /**
     * @param $fileName
     *
     * @return string
     */
    private function createPathFromFileName($fileName)
    {
        $path = resource_path(str_replace('.', DIRECTORY_SEPARATOR, '/views/'.config('easel.layouts.posts'))).'/'.$fileName;

        return $path;
    }

    /**
     * @param $path
     *
     * @return bool
     */
    private function createFile($path)
    {
        if (!file_exists(dirname($path))) {
            mkdir(dirname($path), 0777, true);
        }

        if (touch($path)) {
            $this->filesCreated->push($path);

            return true;
        }

        return false;
    }

    /**
     * @return \Illuminate\Support\Collection
     */
    private function getLayouts()
    {
        $getPostLayouts = $this->getPostLayoutsAsPublic();

        $postFormFields = new \Easel\Http\Jobs\PostFormFields();
        /** @var \Illuminate\Support\Collection $response */
        $response = $getPostLayouts->invoke($postFormFields);

        return $response;
    }

    public function test_default_layout_only()
    {
        $response = $this->getLayouts();

        // Is there only one layout returned
        $this->assertCount(1, $response);
        // Is the stored key in the collection
        $this->assertTrue($response->contains('default'));
        // Is the displayed value in the collection
        $this->assertTrue($response->has('vendor.easel.frontend.blog.post'));
    }

    public function test_additional_layout()
    {
        $this->createNewBlogTemplateFile('gallery.blade.php');
        $response = $this->getLayouts();

        // Are there 2 values layout returned, default + new layout
        $this->assertCount(2, $response);
        // Are these keys in the collection
        $this->assertTrue($response->contains('default'));
        $this->assertTrue($response->contains('gallery'));
        // Are these values in the collection
        $this->assertTrue($response->has('vendor.easel.frontend.blog.post'));
        $this->assertTrue($response->has(config('easel.layouts.posts').'.gallery'));
    }

    public function test_only_blade_php_files_are_considered_layouts()
    {
        $this->createNewBlogTemplateFile('readme.txt');
        $response = $this->getLayouts();

        // Is there only one layout returned
        $this->assertCount(1, $response);
        // Is the stored key in the collection
        $this->assertTrue($response->contains('default'));
        // Is the displayed value in the collection
        $this->assertTrue($response->has('vendor.easel.frontend.blog.post'));<commit_msg>fixed failing unit test<commit_after><?php

/**
 * Created by PhpStorm.
 * User: talv
 * Date: 20/07/16
 * Time: 17:22.
 */
class PostFormFieldsTest extends TestCase
{
    /**
     * @var \Illuminate\Support\Collection
     */
    private $filesCreated;

    public function setUp()
    {
        parent::setUp();

        $this->filesCreated = collect();
    }

    public function tearDown()
    {
        $this->filesCreated->each(function ($file) {
            try {
                unlink($file);
            } catch (Exception $e) {
            }
        });

        parent::tearDown();
    }

    /**
     * Allow the private getPostLayouts method to become accessible / invokable.
     *
     * @return ReflectionMethod
     */
    private function getPostLayoutsAsPublic()
    {
        $reflection = new ReflectionClass(\Easel\Http\Jobs\PostFormFields::class);
        $getPostLayouts = $reflection->getMethod('getPostLayouts');
        $getPostLayouts->setAccessible(true);

        return $getPostLayouts;
    }

    /**
     * @param $fileName
     *
     * @return bool
     */
    private function createNewBlogTemplateFile($fileName)
    {
        $path = $this->createPathFromFileName($fileName);

        return $this->createFile($path);
    }

    /**
     * @param $fileName
     *
     * @return string
     */
    private function createPathFromFileName($fileName)
    {
        $path = resource_path(str_replace('.', DIRECTORY_SEPARATOR, '/views/'.config('easel.layouts.posts.custom'))).'/'.$fileName;

        return $path;
    }

    /**
     * @param $path
     *
     * @return bool
     */
    private function createFile($path)
    {
        if (!file_exists(dirname($path))) {
            mkdir(dirname($path), 0777, true);
        }

        if (touch($path)) {
            $this->filesCreated->push($path);

            return true;
        }

        return false;
    }

    /**
     * @return \Illuminate\Support\Collection
     */
    private function getLayouts()
    {
        $getPostLayouts = $this->getPostLayoutsAsPublic();

        $postFormFields = new \Easel\Http\Jobs\PostFormFields();
        /** @var \Illuminate\Support\Collection $response */
        $response = $getPostLayouts->invoke($postFormFields);

        return $response;
    }

    public function test_default_layout_only()
    {
        $response = $this->getLayouts();

        // Is there only one layout returned
        $this->assertCount(1, $response);
        // Is the stored key in the collection
        $this->assertTrue($response->contains('default'));
        // Is the displayed value in the collection
        $this->assertTrue($response->has('vendor.easel.frontend.blog.post'));
    }

    public function test_additional_layout()
    {
        $this->createNewBlogTemplateFile('gallery.blade.php');
        $response = $this->getLayouts();

        // Are there 2 values layout returned, default + new layout
        $this->assertCount(2, $response);
        // Are these keys in the collection
        $this->assertTrue($response->contains('default'));
        $this->assertTrue($response->contains('gallery'));
        // Are these values in the collection
        $this->assertTrue($response->has('vendor.easel.frontend.blog.post'));
        $this->assertTrue($response->has(config('easel.layouts.posts.custom').'.gallery'));
    }

    public function test_only_blade_php_files_are_considered_layouts()
    {
        $this->createNewBlogTemplateFile('readme.txt');
        $response = $this->getLayouts();

        // Is there only one layout returned
        $this->assertCount(1, $response);
        // Is the stored key in the collection
        $this->assertTrue($response->contains('default'));
        // Is the displayed value in the collection
        $this->assertTrue($response->has('vendor.easel.frontend.blog.post'));
{'fraction_non_alphanumeric': 0.1386652817450013, 'fraction_numerical': 0.004674110620618021, 'mean_word_length': 2.3145438898450945, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 73, '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': '2542611', 'n_tokens_mistral': 2228, 'n_tokens_neox': 2167, 'n_words': 420}