Szybkie posty

Jeżeli coś jest wartego uwagi a nie jest to materiał na pełnoprawnwgo posta na blogu to będę starać się to umieszczać w tym miejscu.

#ajax #apache #behat #bitbacket #bootstrap #composer #cookies #cqrs #css #css flex #ct8 #curl #docker #doctrine #edukacja #enet #enp sla #filmy #firma #funkcje php #git #google #htaccess #html #inne #javascript #jedzenie #jquery #js/jquery #kawały #krypto #laravel #linux #oop #pdo #php #php wzorce narzędzia #phpmyadmin #phpspec #phpstan #phpstorm #phpunit #podcast #rabbit #redis #seo #soap #sql #symfony #szukanie po stringach w php #twig #virtual host #visual studio code #vue #wamp #windows #wino-nalewki #wyrazenia regularne #wzorce projektowe #xml #xxx #zdjecia #złote myśli
  • Szukasz po tagu: phpunit
  • Ilość rekordów w bazie: 4

wiele wywołań metody

        $updateOriginalPricesForItemCommand1 = new UpdateOriginalPricesForItemCommand(
            new ItemPrices(
                'x11x',
                $cartItem1Money,
                $cartItem1Money
            )
        );

        $updateOriginalPricesForItemCommand2 = new UpdateOriginalPricesForItemCommand(
            new ItemPrices(
                'x22x',
                $cartItem2Money,
                $cartItem2Money
            )
        );

        $commandBus = $this->createMock(CommandBusInterface::class);
        $commandBus->expects($this->exactly(2))->method('handle')
            ->will($this->onConsecutiveCalls([
                $updateOriginalPricesForItemCommand1, $updateOriginalPricesForItemCommand2
            ]));

phpunit błedy testów przez rzeczy deprcated

Trzeba dodać paramter w tene sposób

SYMFONY_DEPRECATIONS_HELPER
    <php>
        <ini name="display_errors" value="1" />
        <ini name="error_reporting" value="-1" />
        <server name="APP_ENV" value="test" force="true" />
        <env name="KERNEL_CLASS" value="Shared\Framework\Infrastructure\Kernel" force="true" />
        <server name="SHELL_VERBOSITY" value="-1" />
        <server name="SYMFONY_PHPUNIT_REMOVE" value="" />
        <server name="SYMFONY_PHPUNIT_VERSION" value="9.5" />
        <env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" />
    </php>

w tym pliku

phpunit.xml.dist

lub do pliku env.test dodać

SYMFONY_DEPRECATIONS_HELPER=disabled

Ustawianie prywatne wartości w testach

<?php

namespace Tests\MultiStepFormBundle\Domain\Flow\Manager;

use MultiStepFormBundle\Domain\Flow\Manager\FlowManager;
use Tests\ModelTestCase;

class FlowManagerTest extends ModelTestCase {

    /**
     * @dataProvider mergeClientDataWithDataFromSidByMinModifyDateProvider
     */
    public function testMergeClientDataWithDataFromSidByMinModifyDate(array $clientData, array $clientDataFromSid, array $expected) {
        $transactionModelFactoryMock = $this->createMock(\MultiStepFormBundle\Domain\Transaction\Factory\TransactionModelFactory::class);
        $clientDataModelFactoryMock = $this->createMock(\MultiStepFormBundle\Domain\ClientData\Factory\ClientDataModelFactory::class);
        $formModelFactoryMock = $this->createMock(\MultiStepFormBundle\Domain\Form\Factory\FormModelFactory::class);
        $containerMock = $this->createMock(\Symfony\Component\DependencyInjection\Container::class);

        $service = new FlowManager($transactionModelFactoryMock, $clientDataModelFactoryMock, $formModelFactoryMock, $containerMock);

        $this->setPrivateVariable($service, 'copyClientData', $clientData);

        $method = $this->getMethod(FlowManager::class,
                'mergeClientDataWithDataFromSidByMinModifyDate');
        $result = $method->invokeArgs($service, [$clientDataFromSid]);

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

    public function mergeClientDataWithDataFromSidByMinModifyDateProvider() {
        return [
            [
                [
                    'imie' => 'Jan',
                ],
                [
                    'imie' => [
                        'value' => 'Jan',
                        'modify' => (new \DateTime('now + 5minutes'))->format('Y-m-d H:i:s')
                    ],
                ],
                [
                    'imie' => 'Jan',
                ],
            ],
            [
                [
                    'imie' => 'Jan',
                ],
                [
                    'imie' => [
                        'value' => 'Stefan',
                        'modify' => (new \DateTime('now + 5minutes'))->format('Y-m-d H:i:s')
                    ],
                ],
                [
                    'imie' => 'Jan',
                ],
            ],
            [
                [
                    'imie' => '',
                ],
                [
                    'imie' => [
                        'value' => 'Stefan',
                        'modify' => (new \DateTime('now + 5minutes'))->format('Y-m-d H:i:s')
                    ],
                ],
                [
                    'imie' => 'Stefan',
                ],
            ],
            [
                [
                    'imie' => 'Dupa',
                ],
                [
                    'imie' => [
                        'value' => 'Stefan',
                        'modify' => (new \DateTime('now - 5 days'))->format('Y-m-d H:i:s')
                    ],
                ],
                [
                    'imie' => 'Dupa',
                ],
            ],
            [
                [
                    'imie' => '',
                ],
                [
                    'imie' => [
                        'value' => 'Stefan',
                        'modify' => (new \DateTime('now - 5 days'))->format('Y-m-d H:i:s')
                    ],
                ],
                [
                    'imie' => '',
                ],
            ],
            [
                [
                    'imie' => 'Jan',
                ],
                [
                    'imie' => [
                        'value' => 'Stefan',
                        'modify' => null
                    ],
                ],
                [
                    'imie' => 'Jan',
                ],
            ],
            [
                [
                    'imie' => 'Jan',
                ],
                [
                    'imie' => [
                        'value' => 'Stefan',
                        'modify' => ''
                    ],
                ],
                [
                    'imie' => 'Jan',
                ],
            ],
            [
                [
                    'imie' => 'Jan',
                ],
                [],
                [
                    'imie' => 'Jan',
                ],
            ],
            [
                [
                ],
                [
                    'imie' => [
                        'value' => 'Stefan',
                        'modify' => (new \DateTime('now + 5 days'))->format('Y-m-d H:i:s')
                    ],
                ],
                [
                    'imie' => 'Stefan',
                ],
            ],
            [
                [],
                [
                    'imie' => [
                        'value' => 'Stefan'
                    ],
                ],
                [],
            ],
            [
                [
                    'imie' => 'janusz'
                ],
                [
                    'imie' => [
                        'value' => 'Stefan'
                    ],
                ],
                [
                    'imie' => 'janusz'
                ],
            ],
        ];
    }

    /**
     * get acces to private method
     * @param string $className
     * @param string $methodName
     * @return \ReflectionMethod
     */
    protected static function getMethod(string $className, string $methodName) {
        $class = new \ReflectionClass($className);
        $method = $class->getMethod($methodName);
        $method->setAccessible(true);
        return $method;
    }

    protected static function setPrivateVariable($object, string $objectParameter, $objectValue) {
        $reflection = new \ReflectionClass($object);
        $property = $reflection->getProperty($objectParameter);
        $property->setAccessible(true);
        $property->setValue($object, $objectValue);
    }

}

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',
            ],
        ];
    }

}