codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.39k stars 1.9k forks source link

Bug: BaseBuilder::updateBatch() may cause ArgumentCountError #6555

Closed kenjis closed 2 years ago

kenjis commented 2 years ago

PHP Version

8.1

CodeIgniter4 Version

4.3 (fee2620b2077d1957e2ed0f9d0a745ab39c2a3af)

CodeIgniter4 Installation Method

Git

Which operating systems have you tested for this bug?

macOS

Which server did you use?

cli-server (PHP built-in webserver)

Database

No response

What happened?

ArgumentCountError : 4 arguments are required, 2 given
 /.../CodeIgniter4/system/Database/BaseBuilder.php:2330
 /.../CodeIgniter4/system/Database/BaseBuilder.php:2229
 /.../CodeIgniter4/tests/system/Database/Builder/UpdateTest.php:229

Steps to Reproduce

The following test fails.

    public function testUpdateBatch()
    {
        $builder = new BaseBuilder('%start', $this->db);

        $updateData = [
            [
                'id'          => 2,
                'name'        => 'Comedian',
                'description' => 'There\'s something in your teeth',
            ],
            [
                'id'          => 3,
                'name'        => 'Cab Driver',
                'description' => 'I am yellow',
            ],
        ];

        $this->db->shouldReturn('execute', 1)->shouldReturn('affectedRows', 1);
        $builder->updateBatch($updateData, 'id');

        $query = $this->db->getLastQuery();
        $this->assertInstanceOf(MockQuery::class, $query);

        $expected = <<<'EOF'
            UPDATE "%start"
            SET
            "name" = _u."name",
            "description" = _u."description"
            FROM (
            SELECT 2 "id", 'Comedian' "name", 'There''s something in your teeth' "description" UNION ALL
            SELECT 3 "id", 'Cab Driver' "name", 'I am yellow' "description"
            ) _u
            WHERE "%start"."id" = _u."id"
            EOF;

        $this->assertSame($expected, $query->getQuery());
    }

Expected Output

No error.

Anything else?

See https://github.com/codeigniter4/CodeIgniter4/pull/6373#pullrequestreview-1113028610

sclubricants commented 2 years ago

https://github.com/codeigniter4/CodeIgniter4/pull/6536 now should fix this.

kenjis commented 2 years ago

Fixed by #6536