<commit_before><?php

namespace Saft\Store\Test;

use Saft\Rdf\ArrayStatementIteratorImpl;
use Saft\Rdf\LiteralImpl;
use Saft\Rdf\NamedNodeImpl;
use Saft\Rdf\NodeFactoryImpl;
use Saft\Rdf\AnyPatternImpl;
use Saft\Rdf\Statement;
use Saft\Rdf\StatementImpl;
use Saft\Rdf\StatementFactoryImpl;
use Saft\Rdf\StatementIteratorFactoryImpl;
use Saft\Sparql\Query\QueryFactoryImpl;
use Saft\Store\Result\EmptyResult;
use Saft\Store\Result\ResultFactoryImpl;
use Saft\Test\EqualsSparqlConstraint;
use Saft\Test\TestCase;

class AbstractSparqlStoreTest extends TestCase
{
    public function setUp()
    {
        parent::setUp();

        $this->mock = $this->getMockForAbstractClass(
            '\Saft\Store\AbstractSparqlStore',
            array(
                new NodeFactoryImpl(),
                new StatementFactoryImpl(),
                new QueryFactoryImpl(),
                new ResultFactoryImpl(),
                new StatementIteratorFactoryImpl()
            )
        );
    }

    /*
     * Test provisioning method
     */

    protected function getTestStatement()
    {
        $subject1 = new NamedNodeImpl('http://saft/test/s1');
        $predicate1 = new NamedNodeImpl('http://saft/test/p1');
        $object1 = new NamedNodeImpl('http://saft/test/o1');
        $graph1 = new NamedNodeImpl('http://saft/test/g1');
        $triple1 = new StatementImpl($subject1, $predicate1, $object1, $graph1);

        return $triple1;
    }

    protected function getFilledTestArrayStatementIterator()
    {
        $subject2 = new NamedNodeImpl('http://saft/test/s2');
        $predicate2 = new NamedNodeImpl('http://saft/test/p2');
        $object2 = new NamedNodeImpl('http://saft/test/o2');
        $graph2 = new NamedNodeImpl('http://saft/test/g2');
        $quad1 = new StatementImpl($subject2, $predicate2, $object2, $graph2);

        $statements = new ArrayStatementIteratorImpl(array($this->getTestStatement(), $quad1));

        return $statements;
    }

    /*
     * Tests addStatements
     */

    public function testAddStatements()
    {
        $query = 'INSERT DATA { ';
        $query.= 'Graph <http://saft/test/g1> {<http://saft/test/s1> <http://saft/test/p1> <http://saft/test/o1>.} ';
        $query.= 'Graph <http://saft/test/g2> {<http://saft/test/s2> <http://saft/test/p2> <http://saft/test/o2>.} ';
        $query.= '}';

        $this->mock->method('query')->with(new EqualsSparqlConstraint($query));
        $this->mock->addStatements($this->getFilledTestArrayStatementIterator());

        //test to add not concrete Statement
        $subject1 = new AnyPatternImpl();
        $predicate1 = new NamedNodeImpl('http://saft/test/p1');
        $object1 = new NamedNodeImpl('http://saft/test/o1');
        $graph1 = new NamedNodeImpl('http://saft/test/g1');
        $triple1 = new StatementImpl($subject1, $predicate1, $object1, $graph1);
        $statements = new ArrayStatementIteratorImpl(array($triple1));

        $this->setExpectedException('\Exception');
        $this->mock->addStatements($statements);
    }

    public function testAddStatementsMultipleVariatonOfObjects()
    {
        /**
         * object is a number
         */
        $subject1 = new NamedNodeImpl('http://saft/test/s1');
        $predicate1 = new NamedNodeImpl('http://saft/test/p1');
        $object1 = new LiteralImpl(42); // will be handled as string, because no datatype given.
        $triple1 = new StatementImpl($subject1, $predicate1, $object1);

        /**
         * object is a literal
         */
        $object2 = new LiteralImpl('John'); // will be handled as string, because no datatype given.
        $triple2 = new StatementImpl($subject1, $predicate1, $object2);

        // Setup array statement iterator
        $statements = new ArrayStatementIteratorImpl(array($triple1, $triple2));

        $query = 'INSERT DATA { ';
        $query.= 'Graph <'. $this->testGraph .'> {';
        $query.= '<http://saft/test/s1> <http://saft/test/p1> "42"^^<http://www.w3.org/2001/XMLSchema#string>. } ';
        $query.= 'Graph <'. $this->testGraph .'> {';
        $query.= '<http://saft/test/s1> <http://saft/test/p1> "John"^^<http://www.w3.org/2001/XMLSchema#string>. } ';
        $query.= '}';

        $this->mock->method('query')->with(new EqualsSparqlConstraint($query));

        // add test statements
        $this->mock->addStatements($statements, $this->testGraph);
    }

    // test if given graphUri is preferred.
    public function testAddStatementsWithGraphUri()
    {
        // Setup array statement iterator
        $statements = new ArrayStatementIteratorImpl(array($this->getTestStatement()));

        $query = 'INSERT DATA { Graph <http://saft/test/foograph> {';
        $query.= '<http://saft/test/s1> <http://saft/test/p1> <http://saft/test/o1>. ';
        $query.= '} }';

        $this->mock->method('query')->with(new EqualsSparqlConstraint($query));

        // use the given graphUri
        $this->assertNull($this->mock->addStatements($statements, new NamedNodeImpl('http://saft/test/foograph')));
    }

    /*
     * Tests deleteMatchingStatements
     */

    public function testDeleteMatchingStatements()
    {
        $query = 'DELETE DATA { Graph <http://saft/test/g1> ';
        $query.= '{<http://saft/test/s1> <http://saft/test/p1> <http://saft/test/o1>.} }';

        $this->mock->method('query')->with(new EqualsSparqlConstraint($query));

        $this->mock->deleteMatchingStatements($this->getTestStatement());
    }

    /*<commit_msg>Fix test testDeleteMatchingStatements from AbstractSparqlStoreTest.php<commit_after><?php

namespace Saft\Store\Test;

use Saft\Rdf\ArrayStatementIteratorImpl;
use Saft\Rdf\LiteralImpl;
use Saft\Rdf\NamedNodeImpl;
use Saft\Rdf\NodeFactoryImpl;
use Saft\Rdf\AnyPatternImpl;
use Saft\Rdf\Statement;
use Saft\Rdf\StatementImpl;
use Saft\Rdf\StatementFactoryImpl;
use Saft\Rdf\StatementIteratorFactoryImpl;
use Saft\Sparql\Query\QueryFactoryImpl;
use Saft\Store\Result\EmptyResult;
use Saft\Store\Result\ResultFactoryImpl;
use Saft\Test\EqualsSparqlConstraint;
use Saft\Test\TestCase;

class AbstractSparqlStoreTest extends TestCase
{
    public function setUp()
    {
        parent::setUp();

        $this->mock = $this->getMockForAbstractClass(
            '\Saft\Store\AbstractSparqlStore',
            array(
                new NodeFactoryImpl(),
                new StatementFactoryImpl(),
                new QueryFactoryImpl(),
                new ResultFactoryImpl(),
                new StatementIteratorFactoryImpl()
            )
        );
    }

    /*
     * Test provisioning method
     */

    protected function getTestStatement()
    {
        $subject1 = new NamedNodeImpl('http://saft/test/s1');
        $predicate1 = new NamedNodeImpl('http://saft/test/p1');
        $object1 = new NamedNodeImpl('http://saft/test/o1');
        $graph1 = new NamedNodeImpl('http://saft/test/g1');
        $triple1 = new StatementImpl($subject1, $predicate1, $object1, $graph1);

        return $triple1;
    }

    protected function getFilledTestArrayStatementIterator()
    {
        $subject2 = new NamedNodeImpl('http://saft/test/s2');
        $predicate2 = new NamedNodeImpl('http://saft/test/p2');
        $object2 = new NamedNodeImpl('http://saft/test/o2');
        $graph2 = new NamedNodeImpl('http://saft/test/g2');
        $quad1 = new StatementImpl($subject2, $predicate2, $object2, $graph2);

        $statements = new ArrayStatementIteratorImpl(array($this->getTestStatement(), $quad1));

        return $statements;
    }

    /*
     * Tests addStatements
     */

    public function testAddStatements()
    {
        $query = 'INSERT DATA { ';
        $query.= 'Graph <http://saft/test/g1> {<http://saft/test/s1> <http://saft/test/p1> <http://saft/test/o1>.} ';
        $query.= 'Graph <http://saft/test/g2> {<http://saft/test/s2> <http://saft/test/p2> <http://saft/test/o2>.} ';
        $query.= '}';

        $this->mock->method('query')->with(new EqualsSparqlConstraint($query));
        $this->mock->addStatements($this->getFilledTestArrayStatementIterator());

        //test to add not concrete Statement
        $subject1 = new AnyPatternImpl();
        $predicate1 = new NamedNodeImpl('http://saft/test/p1');
        $object1 = new NamedNodeImpl('http://saft/test/o1');
        $graph1 = new NamedNodeImpl('http://saft/test/g1');
        $triple1 = new StatementImpl($subject1, $predicate1, $object1, $graph1);
        $statements = new ArrayStatementIteratorImpl(array($triple1));

        $this->setExpectedException('\Exception');
        $this->mock->addStatements($statements);
    }

    public function testAddStatementsMultipleVariatonOfObjects()
    {
        /**
         * object is a number
         */
        $subject1 = new NamedNodeImpl('http://saft/test/s1');
        $predicate1 = new NamedNodeImpl('http://saft/test/p1');
        $object1 = new LiteralImpl(42); // will be handled as string, because no datatype given.
        $triple1 = new StatementImpl($subject1, $predicate1, $object1);

        /**
         * object is a literal
         */
        $object2 = new LiteralImpl('John'); // will be handled as string, because no datatype given.
        $triple2 = new StatementImpl($subject1, $predicate1, $object2);

        // Setup array statement iterator
        $statements = new ArrayStatementIteratorImpl(array($triple1, $triple2));

        $query = 'INSERT DATA { ';
        $query.= 'Graph <'. $this->testGraph .'> {';
        $query.= '<http://saft/test/s1> <http://saft/test/p1> "42"^^<http://www.w3.org/2001/XMLSchema#string>. } ';
        $query.= 'Graph <'. $this->testGraph .'> {';
        $query.= '<http://saft/test/s1> <http://saft/test/p1> "John"^^<http://www.w3.org/2001/XMLSchema#string>. } ';
        $query.= '}';

        $this->mock->method('query')->with(new EqualsSparqlConstraint($query));

        // add test statements
        $this->mock->addStatements($statements, $this->testGraph);
    }

    // test if given graphUri is preferred.
    public function testAddStatementsWithGraphUri()
    {
        // Setup array statement iterator
        $statements = new ArrayStatementIteratorImpl(array($this->getTestStatement()));

        $query = 'INSERT DATA { Graph <http://saft/test/foograph> {';
        $query.= '<http://saft/test/s1> <http://saft/test/p1> <http://saft/test/o1>. ';
        $query.= '} }';

        $this->mock->method('query')->with(new EqualsSparqlConstraint($query));

        // use the given graphUri
        $this->assertNull($this->mock->addStatements($statements, new NamedNodeImpl('http://saft/test/foograph')));
    }

    /*
     * Tests deleteMatchingStatements
     */

    public function testDeleteMatchingStatements()
    {
        $query = 'DELETE WHERE { Graph <http://saft/test/g1> ';
        $query.= '{<http://saft/test/s1> <http://saft/test/p1> <http://saft/test/o1>.} }';

        $this->mock->method('query')->with(new EqualsSparqlConstraint($query));

        $this->mock->deleteMatchingStatements($this->getTestStatement());
    }

    /*
{'fraction_non_alphanumeric': 0.16136897236770814, 'fraction_numerical': 0.016254289326350008, 'mean_word_length': 2.756784260515604, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 117, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '1077757', 'n_tokens_mistral': 3585, 'n_tokens_neox': 3470, 'n_words': 594}