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);
    }

}
Komentarze wyłączone