HEX
Server: Apache
System: Linux WWW 6.1.0-40-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.153-1 (2025-09-20) x86_64
User: web11 (1011)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: /var/www/apklausos/tests/unit/objectpatch/PatcherTest.php
<?php

namespace ls\tests\unit\objectpatch;

use ls\tests\TestBaseClass;

use LimeSurvey\ObjectPatch\ObjectPatchException;
use LimeSurvey\ObjectPatch\Patcher;
use LimeSurvey\ObjectPatch\OpHandler\OpHandlerInterface;

/**
 * @testdox Patcher
 */
class PatcherTest extends TestBaseClass
{
    /**
     * @testdox applyPatch() should execute matched operation
     */
    public function testApplyPatchExecuteMatchedOperation()
    {
        $patch = [
            [
                'entity' => 'person',
                'op' => 'create',
                'id' => 1,
                'props' => ['name' => 'John']
            ]
        ];

        $opHandler = \Mockery::mock(
            OpHandlerInterface::class
        );
        $opHandler->shouldReceive('canHandle')
            ->andReturn(true);
        $opHandler->shouldReceive('handle')
            ->andReturn(true);
        $opHandler->shouldReceive('validateOperation')
            ->andReturn([]);

        $patcher = new Patcher();
        $patcher->addOpHandler($opHandler);
        $returnedData = $patcher->applyPatch($patch);
        $operationsApplied = $returnedData['operationsApplied'];

        $this->assertEquals(1, $operationsApplied);
    }

    /**
     * @testdox applyPatch() throws ObjectPatchException on missing op handler
     */
    public function testApplyPatchThrowsObjectPatchExceptionOnMissingOpHandler()
    {
        $this->expectException(ObjectPatchException::class);

        $patch = [
            [
                'entity' => 'person',
                'op' => 'create',
                'id' => 1,
                'props' => ['name' => 'John']
            ]
        ];

        $patcher = new Patcher();
        $returnedData = $patcher->applyPatch($patch);
        $operationsApplied = $returnedData['operationsApplied'];

        $this->assertEquals(0, $operationsApplied);
    }

    /**
     * @testdox applyPatch() throws ObjectPatchException on missing op
     */
    public function testApplyPatchThrowsObjectPatchExceptionOnMissingOp()
    {
        $this->expectException(ObjectPatchException::class);
        $patch = [
            [
                'entity' => 'person',
                'id' => 1,
                'props' => ['name' => 'John']
            ]
        ];

        $patcher = new Patcher();
        $patcher->applyPatch($patch);
    }
}