<commit_before><?php

require_once 'MongoTripodTestBase.php';

class DiscoverImpactedSubjectsTest extends MongoTripodTestBase
{
    public function testFoo()
    {
        $this->assertTrue(true);
    }
}<commit_msg>Added tests for DiscoverImpactedSubjects job<commit_after><?php

require_once 'MongoTripodTestBase.php';

class DiscoverImpactedSubjectsTest extends MongoTripodTestBase
{
    protected $args = array();
    public function testMandatoryArgTripodConfig()
    {
        $this->setArgs();
        unset($this->args['tripodConfig']);
        $job = new DiscoverImpactedSubjects();
        $job->args = $this->args;
        $this->setExpectedException('Exception', "Argument tripodConfig was not present in supplied job args for job DiscoverImpactedSubjects");
        $job->perform();
    }

    public function testMandatoryArgStoreName()
    {
        $this->setArgs();
        unset($this->args['storeName']);
        $job = new DiscoverImpactedSubjects();
        $job->args = $this->args;
        $this->setExpectedException('Exception', "Argument storeName was not present in supplied job args for job DiscoverImpactedSubjects");
        $job->perform();
    }

    public function testMandatoryArgPodName()
    {
        $this->setArgs();
        unset($this->args['podName']);
        $job = new DiscoverImpactedSubjects();
        $job->args = $this->args;
        $this->setExpectedException('Exception', "Argument podName was not present in supplied job args for job DiscoverImpactedSubjects");
        $job->perform();
    }

    public function testMandatoryArgChanges()
    {
        $this->setArgs();
        unset($this->args['changes']);
        $job = new DiscoverImpactedSubjects();
        $job->args = $this->args;
        $this->setExpectedException('Exception', "Argument changes was not present in supplied job args for job DiscoverImpactedSubjects");
        $job->perform();
    }

    public function testMandatoryArgOperations()
    {
        $this->setArgs();
        unset($this->args['operations']);
        $job = new DiscoverImpactedSubjects();
        $job->args = $this->args;
        $this->setExpectedException('Exception', "Argument operations was not present in supplied job args for job DiscoverImpactedSubjects");
        $job->perform();
    }

    public function testMandatoryArgContextAlias()
    {
        $this->setArgs();
        unset($this->args['contextAlias']);
        $job = new DiscoverImpactedSubjects();
        $job->args = $this->args;
        $this->setExpectedException('Exception', "Argument contextAlias was not present in supplied job args for job DiscoverImpactedSubjects");
        $job->perform();
    }

    public function testSubmitApplyOperationsJob()
    {
        $this->setArgs();

        $tripod = $this->getMockBuilder('MongoTripod')
            ->setMethods(array('getComposite'))
            ->setConstructorArgs(array('CBD_testing', 'tripod_php_testing'))
            ->getMock();

        $views = $this->getMockBuilder('MongoTripodViews')
            ->setMethods(array('getImpactedSubjects'))
            ->setConstructorArgs(
                array(
                    'tripod_php_testing',
                    MongoTripodConfig::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'),
                    'http://talisaspire.com/'
                )
            )->getMock();

        $tables = $this->getMockBuilder('MongoTripodTables')
            ->setMethods(array('getImpactedSubjects'))
            ->setConstructorArgs(
                array(
                    'tripod_php_testing',
                    MongoTripodConfig::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'),
                    'http://talisaspire.com/'
                )
            )->getMock();

        $search = $this->getMockBuilder('MongoTripodSearchIndexer')
            ->setMethods(array('getImpactedSubjects'))
            ->setConstructorArgs(array($tripod))
            ->getMock();

        $tripod->expects($this->exactly(3))
            ->method('getComposite')
            ->will($this->returnValueMap(
                array(
                    array(OP_VIEWS, $views),
                    array(OP_TABLES, $tables),
                    array(OP_SEARCH, $search)
                )
            ));

        $discoverImpactedSubjects = $this->getMockBuilder('DiscoverImpactedSubjects')
            ->setMethods(array('getMongoTripod', 'submitJob'))
            ->getMock();

        $discoverImpactedSubjects->expects($this->once())
            ->method('getMongoTripod')
            ->will($this->returnValue($tripod));

        $discoverImpactedSubjects->args = $this->args;

        $viewSubject = new ImpactedSubject(
            array(
                _ID_RESOURCE=>'http://example.com/resources/foo',
                _ID_CONTEXT=>$this->args['contextAlias']
            ),
            OP_VIEWS,
            $this->args['storeName'],
            $this->args['podName']
        );

        $views->expects($this->once())
            ->method('getImpactedSubjects')
            ->with($this->args['changes'], $this->args['contextAlias'])
            ->will($this->returnValue(
                array($viewSubject)
            ));

        $tableSubject = new ImpactedSubject(
            array(
                _ID_RESOURCE=>'http://example.com/resources/foo2',
                _ID_CONTEXT=>$this->args['contextAlias']
            ),
            OP_TABLES,
            $this->args['storeName'],
            $this->args['podName'],
            array('t_foo_bar')
        );

        $tables->expects($this->once())
            ->method('getImpactedSubjects')
            ->with($this->args['changes'], $this->args['contextAlias'])
            ->will($this->returnValue(
                array($tableSubject)
            ));

        $search->expects($this->once())
            ->method('getImpactedSubjects')
            ->with($this->args['changes'], $this->args['contextAlias'])
            ->will($this->returnValue(array()));

        $discoverImpactedSubjects->expects($this->exactly(2))
            ->method('submitJob')
            ->withConsecutive(
                array(
                    MongoTripodConfig::getApplyQueueName(),
                    "ApplyOperation",
                    array(
                        "subject"=>$viewSubject->toArray(),
                        "tripodConfig"=>$this->args['tripodConfig']
                    )
                ),
                array(
                    MongoTripodConfig::getApplyQueueName(),
                    "ApplyOperation",
                    array(
                        "subject"=>$tableSubject->toArray(),
                        "tripodConfig"=>$this->args['tripodConfig']
                    )
                )
            );

        $discoverImpactedSubjects->perform();
    }

    protected function setArgs()
    {
        $this->args = array(
            'tripodConfig'=>MongoTripodConfig::getConfig(),
            'storeName'=>'tripod_php_testing',
            'podName'=>'CBD_testing',
            'changes'=>array('http://example.com/resources/foo'=>array('rdf:type','dct:title')),
            'operations'=>array(OP_VIEWS, OP_TABLES, OP_SEARCH),
            'contextAlias'=>'http://talisaspire.com/'
        );
    }
}
{'fraction_non_alphanumeric': 0.13979814511729405, 'fraction_numerical': 0.0004091653027823241, 'mean_word_length': 2.0888795282224093, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 129, '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': '3131500', 'n_tokens_mistral': 2041, 'n_tokens_neox': 1934, 'n_words': 289}