Przykład test z mocowaniem metod

//metoda

public function updateStepNumberForTransaction(?string $transactionId = null, ?string $httpReferer = null) : void {
        if (!$transactionId || !$httpReferer) {
            return ;
        }
        
        // get step nr from httpreferer
        $httpRefererStepNr = null;
        // parse url
        $parseUrl = parse_url($httpReferer);
        $query = [];
        if (isset($parseUrl['query'])) {
            parse_str($parseUrl['query'], $query);
        }
        if (isset($query['sn'])) {
            $httpRefererStepNr = (int)$query['sn'];
        }
        if (!$httpRefererStepNr) {
            return ;
        }
        
        // find transaction
        /* @var $transaction TransactionEntity */
        $transaction = $this->transactionRepository->findOneBy(['transaction_id' => $transactionId]);
        if (!$transaction) {
            return ;
        }
        
        // get step number from transaction
        $transactionStepNr = null;
        // parse url
        $parseUrl = parse_url($transaction->getHttpReferer());
        $query = [];
        if (isset($parseUrl['query'])) {
            parse_str($parseUrl['query'], $query);
        }
        if (isset($query['sn'])) {
            $transactionStepNr = (int)$query['sn'];
        }
        
        // check if update
        if (!$transactionStepNr || $transactionStepNr < $httpRefererStepNr) {
            $query['sn'] = $httpRefererStepNr;
            
            // update query values
            $parseUrl['query'] = http_build_query($query);
            //joining a new address
            $newUrl = ($parseUrl['scheme'] ?? '') . "://" . ($parseUrl['host'] ?? '') . ($parseUrl['path'] ?? '') . '?' . ($parseUrl['query'] ?? '');

            // if correct new url - update it
            if (filter_var($newUrl, FILTER_VALIDATE_URL)) {
                $transaction->setHttpReferer($newUrl);
                $this->transactionRepository->flush();
            }
        }
    }

//test

<?php

namespace Tests\DM\MultiStepFormBundle\Service\Transaction;

use Tests\ModelTestCase;
use DM\MultiStepFormBundle\Service\Transaction\TransactionHelper;
use DM\MultiStepFormBundle\Repository\Transaction\TransactionRepository;
use DM\MultiStepFormBundle\Entity\Transaction;

class TransactionHelperClassTest extends ModelTestCase {

    /**
     * @dataProvider updateStepNumberForTransactionProvider
     */
    public function testUpdateStepNumberForTransaction(?string $transactionId, ?string $httpRefer, $transactionHttpRefer, $expected) {
        $transaction = new Transaction();
        $transaction->setTransactionId($transactionId);
        $transaction->setHttpReferer($transactionHttpRefer);

        $transactionRepositoryMock = $this->createMock(TransactionRepository::class);
        $transactionRepositoryMock->expects($this->any())->method('findOneBy')->willReturn($transaction);
        $transactionRepositoryMock->expects($this->any())->method('flush')->willReturn(null);

        $service = new TransactionHelper($transactionRepositoryMock);
        $serviceResult = $service->updateStepNumberForTransaction($transactionId, $httpRefer);

        return $this->assertEquals($expected, $transaction->getHttpReferer());
    }

    public function updateStepNumberForTransactionProvider() {
        return [
            [
                null,
                'https://test.pl/?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8&sn=3',
                'https://test.pl/?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8&sn=3',
                'https://test.pl/?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8&sn=3',
            ],
            [
                '797d3d68-fac6-48d2-b716-a6d9636476e8',
                null,
                'https://test.pl/?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8&sn=3',
                'https://test.pl/?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8&sn=3',
            ],
            [
                '797d3d68-fac6-48d2-b716-a6d9636476e8',
                'https://test.pl/?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8&sn=10',
                'https://test.pl/?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8',
                'https://test.pl/?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8&sn=10',
            ],
            [
                '797d3d68-fac6-48d2-b716-a6d9636476e8',
                'https://test.pl/',
                'https://test.pl/?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8',
                'https://test.pl/?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8',
            ],
            [
                '797d3d68-fac6-48d2-b716-a6d9636476e8',
                'https://test.pl/?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8&sn=8',
                'https://test.pl/?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8&sn=4',
                'https://test.pl/?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8&sn=8',
            ],
            [
                '797d3d68-fac6-48d2-b716-a6d9636476e8',
                'https://test.pl/?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8&sn=4',
                'https://test.pl/?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8&sn=8',
                'https://test.pl/?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8&sn=8',
            ],
            [
                '797d3d68-fac6-48d2-b716-a6d9636476e8',
                'incorrectUrl?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8&sn=4',
                'https://test.pl/?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8&sn=8',
                'https://test.pl/?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8&sn=8',
            ],
            [
                '797d3d68-fac6-48d2-b716-a6d9636476e8',
                'https://test.pl/?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8&sn=8',
                'incorrectUrlfs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8&sn=4',
                'https://test.pl/?fs=ttl-wniosek&tId=797d3d68-fac6-48d2-b716-a6d9636476e8&sn=8',
            ],
        ];
    }

}
Komentarze wyłączone