<commit_before><commit_msg>Add integration test for BulkWrite<commit_after><?php
namespace Mongolid\Tests\Integration;

use MongoDB\BSON\ObjectId;
use MongoDB\Driver\WriteResult;
use Mongolid\Query\BulkWrite;
use Mongolid\Tests\Stubs\ReferencedUser;

class BulkWriteTest extends IntegrationTestCase
{
    public function testShouldRunMultipleUpdateOperations()
    {
        $bob = $this->createUser('Bob');
        $john = $this->createUser('John');
        $mary = $this->createUser('Mary');

        $bulkWrite = new BulkWrite(new ReferencedUser());

        $bulkWrite->updateOne(
            ['_id' => $bob->_id],
            ['name' => 'Bulk Updated Bob!']
        );
        $bulkWrite->updateOne(
            ['_id' => $john->_id],
            ['name' => 'Bulk Updated John!']
        );
        $bulkWrite->updateOne(
            ['_id' => $mary->_id],
            ['name' => 'Bulk Updated Mary!']
        );
        $bulkWrite->updateOne(
            ['_id' => $bob->_id],
            ['delete_this' => ''],
            [],
            '$unset'
        );
        $bulkWrite->updateOne(
            ['_id' => $john->_id],
            ['delete_this' => ''],
            [],
            '$unset'
        );
        $bulkWrite->updateOne(
            ['_id' => $mary->_id],
            ['delete_this' => ''],
            [],
            '$unset'
        );

        // Before running
        $this->assertSame('Bob', $bob->name);
        $this->assertSame('John', $john->name);
        $this->assertSame('Mary', $mary->name);

        $this->assertSame('xxxxx', $bob->delete_this);
        $this->assertSame('xxxxx', $john->delete_this);
        $this->assertSame('xxxxx', $mary->delete_this);

        // Runs it
        $result = $bulkWrite->execute();

        $this->assertInstanceOf(WriteResult::class, $result);

        // Refresh models
        $bob = $bob->first($bob->_id);
        $john = $john->first($john->_id);
        $mary = $mary->first($mary->_id);

        // After running
        $this->assertSame('Bulk Updated Bob!', $bob->name);
        $this->assertSame('Bulk Updated John!', $john->name);
        $this->assertSame('Bulk Updated Mary!', $mary->name);

        $this->assertNull($bob->delete_this);
        $this->assertNull($john->delete_this);
        $this->assertNull($mary->delete_this);
    }

    private function createUser(string $name): ReferencedUser
    {
        $user = new ReferencedUser();
        $user->_id = new ObjectId();
        $user->name = $name;
        $user->delete_this = 'xxxxx';
        $this->assertTrue($user->save());

        return $user;
    }
}
{'fraction_non_alphanumeric': 0.1820267686424474, 'fraction_numerical': 0.0, 'mean_word_length': 2.0958579881656805, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 67, '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': '5756143', 'n_tokens_mistral': 824, 'n_tokens_neox': 790, 'n_words': 131}