<commit_before><?php
namespace Mongolid\Model;

use Mockery as m;
use MongoDB\BSON\ObjectId;
use Mongolid\Cursor\EmbeddedCursor;
use Mongolid\DataMapper\DataMapper;
use Mongolid\TestCase;

class HasRelationsTraitTest extends TestCase
{
    /**
     * @dataProvider referenceScenarios
     */
    public function testShouldReferenceOne($fieldValue, $expectedQuery)
    {
        // Set
        $model = new UserStub();
        $model->refOne = $fieldValue;

        $dataMapper = $this->instance(DataMapper::class, m::mock(DataMapper::class)->makePartial());
        $expectedQuery = $expectedQuery['referencesOne'];
        $expected = new RelatedStub();

        // Expectations
        $dataMapper->expects()
            ->first($expectedQuery, [], true)
            ->andReturn($expected);

        // Actions
        $result = $model->relationReferencesOne;

        // Assertions
        $this->assertSame($expected, $result);
    }

    /**
     * @dataProvider referenceScenarios
     */
    public function testShouldReferenceMany($fieldValue, $expectedQuery)
    {
        // Set
        $model = new UserStub();
        $model->refMany = $fieldValue;

        $dataMapper = $this->instance(DataMapper::class, m::mock(DataMapper::class)->makePartial());
        $expectedQuery = $expectedQuery['referencesMany'];
        $expected = new EmbeddedCursor(RelatedStub::class, []);

        // Expectations
        $dataMapper->expects()
            ->where($expectedQuery, [], true)
            ->andReturn($expected);

        // Actions
        $result = $model->relationReferencesMany;

        // Assertions
        $this->assertSame($expected, $result);
    }

    /**
     * @dataProvider embedsScenarios
     */
    public function testShouldEmbedsOne($fieldValue, $expectedItems)
    {
        // Set
        $model = new UserStub();
        $model->embOne = $fieldValue;

        $expectedItems = $expectedItems['embedsOne'];

        // Act
        $result = $model->relationEmbedsOne;

        // Assert
        $this->assertInstanceOf(RelatedStub::class, $result);
        $this->assertEquals($expectedItems, $result->getDocumentAttributes());
    }

    /**
     * @dataProvider embedsScenarios
     */
    public function testShouldEmbedsMany($fieldValue, $expectedItems)
    {
        // Set
        $model = new UserStub();
        $model->embMany = $fieldValue;

        $expectedItems = $expectedItems['embedsMany'];

        // Act
        $result = $model->relationEmbedsMany;
        $values = array_map(
            function ($item) {
                return $item->getDocumentAttributes();
            },
            $result->all()
        );

        // Assert
        $this->assertInstanceOf(EmbeddedCursor::class, $result);
        $this->assertContainsOnlyInstancesOf(RelatedStub::class, $result->all());
        $this->assertEquals($expectedItems, $values);
    }

    public function referenceScenarios()
    {
        return [
            'ActiveRecord referenced by string id' => [
                'fieldValue' => 'abc123',
                'expectedQuery' => [
                    'referencesOne' => ['_id' => 'abc123'],
                    'referencesMany' => ['_id' => ['$in' => ['abc123']]],
                ],
            ],
            'ActiveRecord referenced by objectId' => [
                'fieldValue' => '577afb0b4d3cec136058fa82',
                'expectedQuery' => [
                    'referencesOne' => ['_id' => new ObjectId('577afb0b4d3cec136058fa82')],
                    'referencesMany' => ['_id' => ['$in' => [new ObjectId('577afb0b4d3cec136058fa82')]]],
                ],
            ],
            'ActiveRecord referenced with series of string objectIds' => [
                'fieldValue' => [new ObjectId('577afb0b4d3cec136058fa82'), new ObjectId('577afb7e4d3cec136258fa83')],
                'expectedQuery' => [
                    'referencesOne' => ['_id' => new ObjectId('577afb0b4d3cec136058fa82')],
                    'referencesMany' => [
                        '_id' => [
                            '$in' => [
                                new ObjectId('577afb0b4d3cec136058fa82'),
                                new ObjectId('577afb7e4d3cec136258fa83'),
                            ],
                        ],
                    ],
                ],
            ],
            // TODO should not hit database?
            'ActiveRecord referenced with null' => [
                'fieldValue' => null,
                'expectedQuery' => [
                    'referencesOne' => ['_id' => null],
                    'referencesMany' => ['_id' => ['$in' => []]],
                ],
            ],
        ];
    }

    public function embedsScenarios()
    {
        return [
            'Embedded document referent to an Schema' => [
                'fieldValue' => ['_id' => 12345, 'name' => 'batata'],
                'expectedItems' => [
                    'embedsOne' => ['_id' => 12345, 'name' => 'batata'],
                    'embedsMany' => [['_id' => 12345, 'name' => 'batata']],
                ],
            ],
            'Embedded documents referent to an Schema' => [
                'fieldValue' => [['_id' => 12345, 'name' => 'batata'], ['_id' => 67890, 'name' => 'bar']],
                'expectedItems' => [
                    'embedsOne' => ['_id' => 12345, 'name' => 'batata'],
                    'embedsMany' => [
                        ['_id' => 12345, 'name' => 'batata'],
                        ['_id' => 67890, 'name' => 'bar'],
                    ],
                ],
            ],
            'Embedded document referent to an ActiveRecord entity' => [
                'fieldValue' => ['_id' => 12345, 'name' => 'batata'],
                'expectedItems' => [
                    'embedsOne' => ['_id' => 12345, 'name' => 'batata'],
                    'embedsMany' => [['_id' => 12345, 'name' => 'batata']],
                ],
            ],
            'Embedded documents referent to an ActiveRecord entity' => [
                'fieldValue' => [['_id' => 12345, 'name' => 'batata'], ['_id' => 67890, 'name' => 'bar']],
                'expectedItems' => [
                    'embedsOne' => ['_id' => 12345, 'name' => 'batata'],
                    'embedsMany' => [['_id' => 12345, 'name' => 'batata'], ['_id' => 67890, 'name' => 'bar']],
                ],
            ],
        ];
    }
}

class UserStub extends AbstractActiveRecord
{
    /**
     * {@inheritdoc}
     */
    protected $collection = 'users';

    public function relationReferencesOne()
    {
        return $this->referencesOne(RelatedStub::class, 'refOne');
    }

    public function relationReferencesMany()
    {
        return $this->referencesMany(RelatedStub::class, 'refMany');
    }

    public function relationEmbedsOne()
    {
        return $this->embedsOne(RelatedStub::class, 'embOne');
    }<commit_msg>Improve tests scenarios<commit_after><?php
namespace Mongolid\Model;

use Mockery as m;
use MongoDB\BSON\ObjectId;
use Mongolid\Cursor\EmbeddedCursor;
use Mongolid\DataMapper\DataMapper;
use Mongolid\TestCase;

class HasRelationsTraitTest extends TestCase
{
    /**
     * @dataProvider referenceScenarios
     */
    public function testShouldReferenceOne($fieldValue, $expectedQuery)
    {
        // Set
        $model = new UserStub();
        $model->refOne = $fieldValue;

        $dataMapper = $this->instance(DataMapper::class, m::mock(DataMapper::class)->makePartial());
        $expectedQuery = $expectedQuery['referencesOne'];
        $expected = new RelatedStub();

        // Expectations
        $dataMapper->expects()
            ->first($expectedQuery, [], true)
            ->andReturn($expected);

        // Actions
        $result = $model->relationReferencesOne;

        // Assertions
        $this->assertSame($expected, $result);
    }

    /**
     * @dataProvider referenceScenarios
     */
    public function testShouldReferenceMany($fieldValue, $expectedQuery)
    {
        // Set
        $model = new UserStub();
        $model->refMany = $fieldValue;

        $dataMapper = $this->instance(DataMapper::class, m::mock(DataMapper::class)->makePartial());
        $expectedQuery = $expectedQuery['referencesMany'];
        $expected = new EmbeddedCursor(RelatedStub::class, []);

        // Expectations
        $dataMapper->expects()
            ->where($expectedQuery, [], true)
            ->andReturn($expected);

        // Actions
        $result = $model->relationReferencesMany;

        // Assertions
        $this->assertSame($expected, $result);
    }

    /**
     * @dataProvider embedsScenarios
     */
    public function testShouldEmbedsOne($fieldValue, $expectedItems)
    {
        // Set
        $model = new UserStub();
        $model->embOne = $fieldValue;

        $expectedItems = $expectedItems['embedsOne'];

        // Act
        $result = $model->relationEmbedsOne;

        // Assert
        $this->assertInstanceOf(RelatedStub::class, $result);
        $this->assertEquals($expectedItems, $result->getDocumentAttributes());
    }

    /**
     * @dataProvider embedsScenarios
     */
    public function testShouldEmbedsMany($fieldValue, $expectedItems)
    {
        // Set
        $model = new UserStub();
        $model->embMany = $fieldValue;

        $expectedItems = $expectedItems['embedsMany'];

        // Act
        $result = $model->relationEmbedsMany;
        $values = array_map(
            function ($item) {
                return $item->getDocumentAttributes();
            },
            $result->all()
        );

        // Assert
        $this->assertInstanceOf(EmbeddedCursor::class, $result);
        $this->assertContainsOnlyInstancesOf(RelatedStub::class, $result->all());
        $this->assertEquals($expectedItems, $values);
    }

    public function referenceScenarios()
    {
        return [
            'referenced by string id' => [
                'fieldValue' => 'abc123',
                'expectedQuery' => [
                    'referencesOne' => ['_id' => 'abc123'],
                    'referencesMany' => ['_id' => ['$in' => ['abc123']]],
                ],
            ],
            'referenced by objectId represented as string' => [
                'fieldValue' => '577afb0b4d3cec136058fa82',
                'expectedQuery' => [
                    'referencesOne' => ['_id' => new ObjectId('577afb0b4d3cec136058fa82')],
                    'referencesMany' => ['_id' => ['$in' => [new ObjectId('577afb0b4d3cec136058fa82')]]],
                ],
            ],
            'referenced by an objectId itself' => [
                'fieldValue' => new ObjectId('577afb0b4d3cec136058fa82'),
                'expectedQuery' => [
                    'referencesOne' => ['_id' => new ObjectId('577afb0b4d3cec136058fa82')],
                    'referencesMany' => ['_id' => ['$in' => [new ObjectId('577afb0b4d3cec136058fa82')]]],
                ],
            ],
            'series of objectIds' => [
                'fieldValue' => [new ObjectId('577afb0b4d3cec136058fa82'), new ObjectId('577afb7e4d3cec136258fa83')],
                'expectedQuery' => [
                    'referencesOne' => ['_id' => new ObjectId('577afb0b4d3cec136058fa82')],
                    'referencesMany' => [
                        '_id' => [
                            '$in' => [
                                new ObjectId('577afb0b4d3cec136058fa82'),
                                new ObjectId('577afb7e4d3cec136258fa83'),
                            ],
                        ],
                    ],
                ],
            ],
            'series of objectIds as strings' => [
                'fieldValue' => ['577afb0b4d3cec136058fa82', '577afb7e4d3cec136258fa83'],
                'expectedQuery' => [
                    'referencesOne' => ['_id' => new ObjectId('577afb0b4d3cec136058fa82')],
                    'referencesMany' => [
                        '_id' => [
                            '$in' => [
                                new ObjectId('577afb0b4d3cec136058fa82'),
                                new ObjectId('577afb7e4d3cec136258fa83'),
                            ],
                        ],
                    ],
                ],
            ],
            // TODO should not hit database?
            'ActiveRecord referenced with null' => [
                'fieldValue' => null,
                'expectedQuery' => [
                    'referencesOne' => ['_id' => null],
                    'referencesMany' => ['_id' => ['$in' => []]],
                ],
            ],
        ];
    }

    public function embedsScenarios()
    {
        return [
            'A single embedded document' => [
                'fieldValue' => ['_id' => 12345, 'name' => 'batata'],
                'expectedItems' => [
                    'embedsOne' => ['_id' => 12345, 'name' => 'batata'],
                    'embedsMany' => [['_id' => 12345, 'name' => 'batata']],
                ],
            ],
            'Many embedded documents' => [
                'fieldValue' => [['_id' => 12345, 'name' => 'batata'], ['_id' => 67890, 'name' => 'bar']],
                'expectedItems' => [
                    'embedsOne' => ['_id' => 12345, 'name' => 'batata'],
                    'embedsMany' => [['_id' => 12345, 'name' => 'batata'], ['_id' => 67890, 'name' => 'bar']],
                ],
            ],
        ];
    }
}

class UserStub extends AbstractActiveRecord
{
    /**
     * {@inheritdoc}
     */
    protected $collection = 'users';

    public function relationReferencesOne()
    {
        return $this->referencesOne(RelatedStub::class, 'refOne');
    }

    public function relationReferencesMany()
    {
        return $this->referencesMany(RelatedStub::class, 'refMany');
    }

    public function relationEmbedsOne()
    {
        return $this->embedsOne(RelatedStub::class, 'embOne');
    }
{'fraction_non_alphanumeric': 0.14932224055081403, 'fraction_numerical': 0.03399555332424873, 'mean_word_length': 1.7154819863680624, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 225, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6334184', 'n_tokens_mistral': 4303, 'n_tokens_neox': 3866, 'n_words': 672}