<commit_before><?php

namespace InsertOnDuplicateKey;

use Carbon\Carbon;
use InsertOnDuplicateKey\Models\Role;
use InsertOnDuplicateKey\Models\User;

class InsertOnDuplicateKeyTest extends InsertOnDuplicateKeyTestCase
{
    protected $updatedUser = [
        'id'    => 1,
        'name'  => 'new name 1',
        'email' => '<EMAIL>',
    ];

    protected $updatedPivotRow = [
        'user_id'    => 1,
        'role_id'    => 1,
        'expires_at' => '2000-01-01 00:00:00',
    ];

    /**
     * Seed the database.
     */
    protected function setUp()
    {
        parent::setUp();

        User::insert([
            ['id' => 1, 'name' => 'foo', 'email' => '<EMAIL>'],
            ['id' => 2, 'name' => 'foo', 'email' => '<EMAIL>'],
            ['id' => 3, 'name' => 'foo', 'email' => '<EMAIL>'],
        ]);

        Role::insert([
            ['id' => 1, 'name' => 'foo'],
            ['id' => 2, 'name' => 'foo'],
            ['id' => 3, 'name' => 'foo'],
        ]);

        (new User(['id' => 1]))->roles()->attach([
            1 => ['expires_at' => Carbon::now()],
            2 => ['expires_at' => Carbon::now()],
        ]);
    }

    public function testInsertOnDuplicateKeyFullUpdate()
    {
        $updatedUser2 = [
            'id'    => 2,
            'name'  => '<NAME>',
            'email' => '<EMAIL>',
        ];

        User::insertOnDuplicateKey([$this->updatedUser, $updatedUser2]);

        $this->assertDatabaseHas('users', $this->updatedUser);
        $this->assertDatabaseHas('users', $updatedUser2);
    }

    public function testInsertOnDuplicateKeyPartialUpdate()
    {
        User::insertOnDuplicateKey(
            [
                [
                    'id'    => 1,
                    'name'  => '<NAME>',
                    'email' => '<EMAIL>',
                ],
            ],
            ['name']
        );

        $this->assertDatabaseHas('users', ['id' => 1, 'name' => '<NAME>', 'email' => '<EMAIL>']);
    }

    public function testInsertIgnore()
    {
        $newUser = [
            'id'    => 4,
            'name'  => '<NAME>',
            'email' => null,
        ];

        User::insertIgnore([$this->updatedUser, $newUser]);

        $this->assertDatabaseMissing('users', $this->updatedUser);
        $this->assertDatabaseHas('users', $newUser);
    }

    public function testAttachOnDuplicateKey()
    {
        (new User(['id' => 1]))->roles()->attachOnDuplicateKey([
            1 => ['expires_at' => '2000-01-01 00:00:00'],
            2 => ['expires_at' => Carbon::tomorrow()],
        ]);

        $this->assertDatabaseHas('role_user', $this->updatedPivotRow);
        $this->assertDatabaseHas('role_user', [
            'user_id'    => 1,
            'role_id'    => 2,
            'expires_at' => Carbon::tomorrow(),
        ]);
    }

    public function testAttachIgnore()
    {
        (new User(['id' => 1]))->roles()->attachIgnore([
            1 => ['expires_at' => '2000-01-01 00:00:00'],
            3 => ['expires_at' => Carbon::tomorrow()],
        ]);

        // The row with user_id = 1 and role_id = 1
        // should have kept the initial value of expires_at = now().
        $this->assertDatabaseMissing('role_user', $this->updatedPivotRow);
        $this->assertDatabaseHas('role_user', [
            'user_id'    => 1,
            'role_id'    => 3,
            'expires_at' => Carbon::tomorrow(),
        ]);
    }
}<commit_msg>Use Model::make()<commit_after><?php

namespace InsertOnDuplicateKey;

use Carbon\Carbon;
use InsertOnDuplicateKey\Models\Role;
use InsertOnDuplicateKey\Models\User;

class InsertOnDuplicateKeyTest extends InsertOnDuplicateKeyTestCase
{
    protected $updatedUser = [
        'id'    => 1,
        'name'  => '<NAME>',
        'email' => '<EMAIL>',
    ];

    protected $updatedPivotRow = [
        'user_id'    => 1,
        'role_id'    => 1,
        'expires_at' => '2000-01-01 00:00:00',
    ];

    /**
     * Seed the database.
     */
    protected function setUp()
    {
        parent::setUp();

        User::insert([
            ['id' => 1, 'name' => 'foo', 'email' => '<EMAIL>'],
            ['id' => 2, 'name' => 'foo', 'email' => '<EMAIL>'],
            ['id' => 3, 'name' => 'foo', 'email' => '<EMAIL>'],
        ]);

        Role::insert([
            ['id' => 1, 'name' => 'foo'],
            ['id' => 2, 'name' => 'foo'],
            ['id' => 3, 'name' => 'foo'],
        ]);

        User::make(['id' => 1])->roles()->attach([
            1 => ['expires_at' => Carbon::now()],
            2 => ['expires_at' => Carbon::now()],
        ]);
    }

    public function testInsertOnDuplicateKeyFullUpdate()
    {
        $updatedUser2 = [
            'id'    => 2,
            'name'  => '<NAME>',
            'email' => '<EMAIL>',
        ];

        User::insertOnDuplicateKey([$this->updatedUser, $updatedUser2]);

        $this->assertDatabaseHas('users', $this->updatedUser);
        $this->assertDatabaseHas('users', $updatedUser2);
    }

    public function testInsertOnDuplicateKeyPartialUpdate()
    {
        User::insertOnDuplicateKey(
            [
                [
                    'id'    => 1,
                    'name'  => '<NAME>',
                    'email' => '<EMAIL>',
                ],
            ],
            ['name']
        );

        $this->assertDatabaseHas('users', ['id' => 1, 'name' => '<NAME>', 'email' => '<EMAIL>']);
    }

    public function testInsertIgnore()
    {
        $newUser = [
            'id'    => 4,
            'name'  => '<NAME>',
            'email' => null,
        ];

        User::insertIgnore([$this->updatedUser, $newUser]);

        $this->assertDatabaseMissing('users', $this->updatedUser);
        $this->assertDatabaseHas('users', $newUser);
    }

    public function testAttachOnDuplicateKey()
    {
        User::make(['id' => 1])->roles()->attachOnDuplicateKey([
            1 => ['expires_at' => '2000-01-01 00:00:00'],
            2 => ['expires_at' => Carbon::tomorrow()],
        ]);

        $this->assertDatabaseHas('role_user', $this->updatedPivotRow);
        $this->assertDatabaseHas('role_user', [
            'user_id'    => 1,
            'role_id'    => 2,
            'expires_at' => Carbon::tomorrow(),
        ]);
    }

    public function testAttachIgnore()
    {
        User::make(['id' => 1])->roles()->attachIgnore([
            1 => ['expires_at' => '2000-01-01 00:00:00'],
            3 => ['expires_at' => Carbon::tomorrow()],
        ]);

        // The row with user_id = 1 and role_id = 1
        // should have kept the initial value of expires_at = now().
        $this->assertDatabaseMissing('role_user', $this->updatedPivotRow);
        $this->assertDatabaseHas('role_user', [
            'user_id'    => 1,
            'role_id'    => 3,
            'expires_at' => Carbon::tomorrow(),
        ]);
    }
}
{'fraction_non_alphanumeric': 0.19289044289044288, 'fraction_numerical': 0.021416083916083916, 'mean_word_length': 1.8450062163282221, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 176, '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': '3727570', 'n_tokens_mistral': 2266, 'n_tokens_neox': 2120, 'n_words': 378}