Completed
Push — master ( 3fd2dd...aba624 )
by Grégoire
04:19
created

tests/Generator/AdminGeneratorTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Tests\Generator;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Generator\AdminGenerator;
18
use Sonata\AdminBundle\Model\ModelManagerInterface;
19
use Sonata\AdminBundle\Tests\Fixtures\Admin\ModelAdmin;
20
use Symfony\Component\Filesystem\Filesystem;
21
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
22
23
/**
24
 * @author Marek Stipek <[email protected]>
25
 */
26
class AdminGeneratorTest extends TestCase
27
{
28
    /** @var AdminGenerator */
29
    private $adminGenerator;
30
31
    /** @var BundleInterface|\PHPUnit_Framework_MockObject_MockObject */
32
    private $bundleMock;
33
34
    /** @var string */
35
    private $bundlePath;
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected function setUp(): void
41
    {
42
        $this->adminGenerator = new AdminGenerator(
43
            $this->createModelManagerMock(),
44
            __DIR__.'/../../src/Resources/skeleton'
45
        );
46
        $this->bundleMock = $this->createBundleMock();
47
        $this->bundlePath = $this->bundleMock->getPath();
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected function tearDown(): void
54
    {
55
        $filesystem = new Filesystem();
56
        $filesystem->remove($this->bundlePath);
57
    }
58
59
    public function testGenerate(): void
60
    {
61
        $this->adminGenerator->generate($this->bundleMock, 'ModelAdmin', 'Model');
62
        $file = $this->adminGenerator->getFile();
63
        $this->assertSame(ModelAdmin::class, $this->adminGenerator->getClass());
64
        $this->assertSame('ModelAdmin.php', basename($file));
65
        $this->assertFileEquals(__DIR__.'/../Fixtures/Admin/ModelAdmin.php', $file);
66
67
        $this->expectException(\RuntimeException::class, 'already exists');
0 ignored issues
show
The call to AdminGeneratorTest::expectException() has too many arguments starting with 'already exists'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
68
69
        $this->adminGenerator->generate($this->bundleMock, 'ModelAdmin', 'Model');
70
    }
71
72
    private function createModelManagerMock(): ModelManagerInterface
73
    {
74
        $modelManagerMock = $this->getMockForAbstractClass(ModelManagerInterface::class);
75
        $modelManagerMock
76
            ->expects($this->any())
77
            ->method('getExportFields')
78
            ->with('Model')
79
            ->will($this->returnValue(['foo', 'bar', 'baz']))
80
        ;
81
82
        return $modelManagerMock;
83
    }
84
85
    private function createBundleMock(): BundleInterface
86
    {
87
        $bundleMock = $this->getMockForAbstractClass(BundleInterface::class);
88
        $bundleMock
89
            ->expects($this->any())
90
            ->method('getNamespace')
91
            ->will($this->returnValue('Sonata\AdminBundle\Tests\Fixtures'))
92
        ;
93
        $bundleMock
94
            ->expects($this->any())
95
            ->method('getPath')
96
            ->will($this->returnValue(sprintf('%s/%s', sys_get_temp_dir(), lcg_value())))
97
        ;
98
99
        return $bundleMock;
100
    }
101
}
102