<commit_before><?php

namespace GraphAware\Neo4j\OGM\Tests\Integration;

use GraphAware\Neo4j\OGM\Query;
use GraphAware\Neo4j\OGM\Tests\Integration\Models\MoviesDemo\Movie;
use GraphAware\Neo4j\OGM\Tests\Integration\Models\MoviesDemo\Person;

/**
 * @group complex-query
 */
class ComplexQueryResultTest extends IntegrationTestCase
{
    public function setUp()
    {
        parent::setUp();
        $this->clearDb();
        $this->playMovies();
    }

    public function testQueryReturningMap()
    {
        $q = $this->em->createQuery('MATCH (n:Person)-[r:ACTED_IN]->(m)
        RETURN n, {roles: r.roles, movie: m} AS actInfo LIMIT 2');

        $q->addEntityMapping('n', Person::class);
        $q->addEntityMapping('actInfo', null, Query::HYDRATE_MAP);
        $q->addEntityMapping('movie', Movie::class);

        $result = $q->getResult();
        $this->assertCount(2, $result);
        $row = $result[0];

        $this->assertInstanceOf(Person::class, $row['n']);
        $this->assertInternalType('array', $row['actInfo']);
        $this->assertInternalType('array', $row['actInfo']['roles']);
        $this->assertInstanceOf(Movie::class, $row['actInfo']['movie']);
    }

    public function testQueryReturningMapCollectionMixed()
    {
        $q = $this->em->createQuery('MATCH (n:Person)-[r:ACTED_IN]->(m) 
        WITH n, {roles: r.roles, movie: m} AS actInfo 
        RETURN n, collect(actInfo) AS actorInfos LIMIT 2');

        $q->addEntityMapping('n', Person::class);
        $q->addEntityMapping('actorInfos', null, Query::HYDRATE_MAP_COLLECTION);
        $q->addEntityMapping('movie', Movie::class);

        $result = $q->getResult();
        $this->assertCount(2, $result);
        $this->assertInternalType('array', $result[0]['actorInfos']);
        $this->assertInstanceOf(Movie::class, $result[0]['actorInfos'][0]['movie']);
        $this->assertCount(1, $result[0]['actorInfos']);
    }

    public function testQueryReturningCollectionOfEntitiesInMap()
    {
        $q = $this->em->createQuery('MATCH (n:Person)-[r:ACTED_IN]->(m) 
        RETURN n, {score: size((n)-[:ACTED_IN]->()), movies: collect(m)} AS infos LIMIT 10');

        $q->addEntityMapping('n', Person::class);
        $q->addEntityMapping('infos', null, Query::HYDRATE_MAP);
        $q->addEntityMapping('movies', Movie::class, Query::HYDRATE_COLLECTION);

        $result = $q->getResult();

        $this->assertCount(10, $result);
        $row = $result[0];
        $this->assertInstanceOf(Person::class, $row['n']);
        $this->assertInternalType('array', $row['infos']);
        $this->assertEquals($row['infos']['score'], count($row['infos']['movies']));
        $this->assertInstanceOf(Movie::class, $row['infos']['movies'][0]);
    }

    public function testQueryReturningMapAsOnlyColumn()
    {<commit_msg>fixed sporadically failing test<commit_after><?php

namespace GraphAware\Neo4j\OGM\Tests\Integration;

use GraphAware\Neo4j\OGM\Query;
use GraphAware\Neo4j\OGM\Tests\Integration\Models\MoviesDemo\Movie;
use GraphAware\Neo4j\OGM\Tests\Integration\Models\MoviesDemo\Person;

/**
 * @group complex-query
 */
class ComplexQueryResultTest extends IntegrationTestCase
{
    public function setUp()
    {
        parent::setUp();
        $this->clearDb();
        $this->playMovies();
    }

    public function testQueryReturningMap()
    {
        $q = $this->em->createQuery('MATCH (n:Person)-[r:ACTED_IN]->(m)
        RETURN n, {roles: r.roles, movie: m} AS actInfo LIMIT 2');

        $q->addEntityMapping('n', Person::class);
        $q->addEntityMapping('actInfo', null, Query::HYDRATE_MAP);
        $q->addEntityMapping('movie', Movie::class);

        $result = $q->getResult();
        $this->assertCount(2, $result);
        $row = $result[0];

        $this->assertInstanceOf(Person::class, $row['n']);
        $this->assertInternalType('array', $row['actInfo']);
        $this->assertInternalType('array', $row['actInfo']['roles']);
        $this->assertInstanceOf(Movie::class, $row['actInfo']['movie']);
    }

    public function testQueryReturningMapCollectionMixed()
    {
        $this->clearDb();
        $this->playMovies();
        $q = $this->em->createQuery('MATCH (n:Person {name:"<NAME>"})-[r:ACTED_IN]->(m) 
        WITH n, {roles: r.roles, movie: m} AS actInfo 
        RETURN n, collect(actInfo) AS actorInfos LIMIT 2');

        $q->addEntityMapping('n', Person::class);
        $q->addEntityMapping('actorInfos', null, Query::HYDRATE_MAP_COLLECTION);
        $q->addEntityMapping('movie', Movie::class);

        $result = $q->getResult();
        $this->assertCount(1, $result);
        $this->assertInternalType('array', $result[0]['actorInfos']);
        $this->assertInstanceOf(Movie::class, $result[0]['actorInfos'][0]['movie']);
        $this->assertCount(12, $result[0]['actorInfos']);
    }

    public function testQueryReturningCollectionOfEntitiesInMap()
    {
        $q = $this->em->createQuery('MATCH (n:Person)-[r:ACTED_IN]->(m) 
        RETURN n, {score: size((n)-[:ACTED_IN]->()), movies: collect(m)} AS infos LIMIT 10');

        $q->addEntityMapping('n', Person::class);
        $q->addEntityMapping('infos', null, Query::HYDRATE_MAP);
        $q->addEntityMapping('movies', Movie::class, Query::HYDRATE_COLLECTION);

        $result = $q->getResult();

        $this->assertCount(10, $result);
        $row = $result[0];
        $this->assertInstanceOf(Person::class, $row['n']);
        $this->assertInternalType('array', $row['infos']);
        $this->assertEquals($row['infos']['score'], count($row['infos']['movies']));
        $this->assertInstanceOf(Movie::class, $row['infos']['movies'][0]);
    }

    public function testQueryReturningMapAsOnlyColumn()
    {
{'fraction_non_alphanumeric': 0.19068387997208652, 'fraction_numerical': 0.007152826238660153, 'mean_word_length': 3.642105263157895, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 82, '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': '7239867', 'n_tokens_mistral': 1919, 'n_tokens_neox': 1849, 'n_words': 280}