Completed
Push — master ( 01b9e4...761718 )
by Yaroslav
02:28 queued 12s
created

SonataAdminClassTestCase::assertHasFormFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Harentius\BlogBundle\Test;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\Mapping\ClassMetadata;
9
use Doctrine\Persistence\ManagerRegistry;
10
use PHPUnit\Framework\TestCase;
11
use Sonata\AdminBundle\Admin\AbstractAdmin;
12
use Sonata\AdminBundle\Builder\FormContractorInterface;
13
use Sonata\AdminBundle\Route\RouteGeneratorInterface;
14
use Sonata\AdminBundle\Translator\NoopLabelTranslatorStrategy;
15
use Sonata\DoctrineORMAdminBundle\Builder\ListBuilder;
16
use Sonata\DoctrineORMAdminBundle\Guesser\TypeGuesser;
17
use Sonata\DoctrineORMAdminBundle\Model\ModelManager;
18
use Symfony\Component\Form\FormBuilderInterface;
19
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetadata;
20
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
21
use Symfony\Component\Validator\Validator\ValidatorInterface;
22
23
class SonataAdminClassTestCase extends TestCase
24
{
25
    public function assertHasListFields(AbstractAdmin $abstractAdmin, array $fields): void
26
    {
27
        $list = $abstractAdmin->getList();
28
        $elements = $list->getElements();
29
        $this->assertArraysSame(array_keys($elements), $fields);
30
    }
31
32
    public function assertHasFormFields(AbstractAdmin $abstractAdmin, array $fields): void
33
    {
34
        $list = $abstractAdmin->getFormFieldDescriptions();
35
        $this->assertArraysSame(array_keys($list), $fields);
36
    }
37
38
    protected function createAdmin(string $class): AbstractAdmin
39
    {
40
        if (!is_subclass_of($class, AbstractAdmin::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Sonata\AdminBundle\Admin\AbstractAdmin::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
41
            throw new \InvalidArgumentException("Class '{$class}' is not subclass of 'AbstractAdmin'");
42
        }
43
44
        /** @var AbstractAdmin $admin */
45
        $admin = new $class('', '', '');
46
        $listBuilder = new ListBuilder(new TypeGuesser());
47
48
        $admin->setListBuilder($listBuilder);
49
        $admin->setRouteGenerator($this->createMock(RouteGeneratorInterface::class));
0 ignored issues
show
Documentation introduced by
$this->createMock(\Sonat...eratorInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundl...outeGeneratorInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
50
        $managerRegistry = $this->createMock(ManagerRegistry::class);
51
        $entityManager = $this->createMock(EntityManagerInterface::class);
52
        $metadataFactory = $this->createMock(MetadataFactoryInterface::class);
53
        $metadataFactory
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
            ->method('getMetadataFor')
55
            ->willReturn($this->createMock(ClassMetadata::class))
56
        ;
57
        $entityManager
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
            ->method('getMetadataFactory')
59
            ->willReturn($metadataFactory)
60
        ;
61
        $managerRegistry
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
            ->method('getManagerForClass')
63
            ->willReturn($entityManager)
64
        ;
65
        $modelManager = new ModelManager($managerRegistry);
0 ignored issues
show
Documentation introduced by
$managerRegistry is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\Common\P...stence\ManagerRegistry>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
        $admin->setModelManager($modelManager);
67
        $admin->setLabelTranslatorStrategy(new NoopLabelTranslatorStrategy());
68
        $formContractor = $this->createMock(FormContractorInterface::class);
69
        $formContractor->method('getFormBuilder')->willReturn($this->createMock(FormBuilderInterface::class));
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
        $admin->setFormContractor($formContractor);
0 ignored issues
show
Documentation introduced by
$formContractor is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundl...ormContractorInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
        $validator = $this->createMock(ValidatorInterface::class);
72
        $validator->method('getMetadataFor')->willReturn($this->createMock(ValidatorClassMetadata::class));
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
        $admin->setValidator($validator);
0 ignored issues
show
Documentation introduced by
$validator is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...tor\ValidatorInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
75
        return $admin;
76
    }
77
78
    private function assertArraysSame(array $array, array $arraySubset): void
79
    {
80
        $this->assertTrue(empty(array_diff($arraySubset, $array)) && empty(array_diff($array, $arraySubset)));
81
    }
82
}
83