<commit_before><?php

namespace Hodor\Daemon;

use PHPUnit_Framework_TestCase;
use Hodor\JobQueue\Config;

class SupervisordManagerTest extends PHPUnit_Framework_TestCase
{
    public function testDaemonConfigContainsExpectedProcesses()
    {
        $expected = [
            'hodor-superqueuer-default',
            'hodor-bufferer-default',
            'hodor-bufferer-special',
            'hodor-worker-default',
            'hodor-worker-intense',
        ];

        $this->assertEquals(
            $expected,
            array_keys($this->getSupervisordManager()->getDaemonConfig())
        );
    }

    /**
     * @dataProvider provideDaemonProcesses
     */
    public function testProcessesContainExpectedKeys()
    {
        $expected = [
            'program_name',
            'command',
            'process_name',
            'numprocs',
            'numprocs_start',
            'autorestart',
            'autostart',
            'startsecs',
            'startretries',
            'user',
            'stopsignal',
            'stderr_logfile',
            'stderr_logfile_maxbytes',
            'stderr_logfile_backups',
            'stdout_logfile',
            'stdout_logfile_maxbytes',
            'stdout_logfile_backups',
        ];

        foreach ($this->getSupervisordManager()->getDaemonConfig() as $process) {
            $this->assertEquals($expected, array_keys($process));
        }
    }

    public function testDaemonConfigIsGeneratedAsExpected()
    {
        $this->assertEquals(
            require __DIR__ . '/ExpectedSupervisordConfig.php',
            $this->getSupervisordManager()->getDaemonConfig()
        );
    }

    /**
     * @return array
     */
    public function provideDaemonProcesses()
    {
        $rows = [];
        foreach ($this->getSupervisordManager()->getDaemonConfig() as $process) {
            $rows[] = [$process];
        }

        return $rows;
    }

    public function testSetupDaemonGeneratesSupervisordConfig()
    {
        $hodor_base_path = dirname(dirname(dirname(dirname(__DIR__))));

        $supervisord_config_path = __DIR__ . '/../../../../tests/tmp/supervisord.' . uniqid() . '.conf';
        $manager = $this->getSupervisordManager($supervisord_config_path);

        $config_dir = dirname($supervisord_config_path);
        if (!is_dir($config_dir) && !mkdir($config_dir)) {
            throw new Exception("Could not create directory '{$config_dir}'.");
        }

        $manager->setupDaemon();

        $expected_supervisord_config = str_replace(
            '{{HODOR_BASE_PATH}}',
            $hodor_base_path,
            file_get_contents(__DIR__ . '/ExpectedSupervisordConfig.conf')
        );<commit_msg>Add test to verify setupDaemon() will throw an exception<commit_after><?php

namespace Hodor\Daemon;

use PHPUnit_Framework_TestCase;
use Hodor\JobQueue\Config;

class SupervisordManagerTest extends PHPUnit_Framework_TestCase
{
    public function testDaemonConfigContainsExpectedProcesses()
    {
        $expected = [
            'hodor-superqueuer-default',
            'hodor-bufferer-default',
            'hodor-bufferer-special',
            'hodor-worker-default',
            'hodor-worker-intense',
        ];

        $this->assertEquals(
            $expected,
            array_keys($this->getSupervisordManager()->getDaemonConfig())
        );
    }

    /**
     * @dataProvider provideDaemonProcesses
     */
    public function testProcessesContainExpectedKeys()
    {
        $expected = [
            'program_name',
            'command',
            'process_name',
            'numprocs',
            'numprocs_start',
            'autorestart',
            'autostart',
            'startsecs',
            'startretries',
            'user',
            'stopsignal',
            'stderr_logfile',
            'stderr_logfile_maxbytes',
            'stderr_logfile_backups',
            'stdout_logfile',
            'stdout_logfile_maxbytes',
            'stdout_logfile_backups',
        ];

        foreach ($this->getSupervisordManager()->getDaemonConfig() as $process) {
            $this->assertEquals($expected, array_keys($process));
        }
    }

    public function testDaemonConfigIsGeneratedAsExpected()
    {
        $this->assertEquals(
            require __DIR__ . '/ExpectedSupervisordConfig.php',
            $this->getSupervisordManager()->getDaemonConfig()
        );
    }

    /**
     * @return array
     */
    public function provideDaemonProcesses()
    {
        $rows = [];
        foreach ($this->getSupervisordManager()->getDaemonConfig() as $process) {
            $rows[] = [$process];
        }

        return $rows;
    }

    /**
     * @expectedException \Exception
     */
    public function testSetupDaemonThrowsAnExceptionIfConfigFileIsNotWritable()
    {
        $hodor_base_path = dirname(dirname(dirname(dirname(__DIR__))));

        $supervisord_config_path = __DIR__ . '/../../../../tests/non-existent/supervisord.' . uniqid() . '.conf';
        $manager = $this->getSupervisordManager($supervisord_config_path);

        $manager->setupDaemon();
    }

    public function testSetupDaemonGeneratesSupervisordConfig()
    {
        $hodor_base_path = dirname(dirname(dirname(dirname(__DIR__))));

        $supervisord_config_path = __DIR__ . '/../../../../tests/tmp/supervisord.' . uniqid() . '.conf';
        $manager = $this->getSupervisordManager($supervisord_config_path);

        $config_dir = dirname($supervisord_config_path);
        if (!is_dir($config_dir) && !mkdir($config_dir)) {
            throw new Exception("Could not create directory '{$config_dir}'.");
        }

        $manager->setupDaemon();

        $expected_supervisord_config = str_replace(
            '{{HODOR_BASE_PATH}}',
            $hodor_base_path,
            file_get_contents(__DIR__ . '/ExpectedSupervisordConfig.conf')
        );
{'fraction_non_alphanumeric': 0.12430143945808637, 'fraction_numerical': 0.0, 'mean_word_length': 2.165058949624866, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '5520300', 'n_tokens_mistral': 1700, 'n_tokens_neox': 1672, 'n_words': 233}