Completed
Push — 3.x ( 9aa22d...aa303f )
by Grégoire
04:40
created

AdminMakerTest::setup()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\AdminBundle\Tests\Maker;
13
14
use PHPUnit\Framework\TestCase;
15
use Prophecy\Argument;
16
use Sonata\AdminBundle\Maker\AdminMaker;
17
use Sonata\AdminBundle\Model\ModelManagerInterface;
18
use Symfony\Bundle\MakerBundle\ConsoleStyle;
19
use Symfony\Bundle\MakerBundle\FileManager;
20
use Symfony\Bundle\MakerBundle\Generator;
21
use Symfony\Component\Console\Input\ArrayInput;
22
use Symfony\Component\Console\Input\InputArgument;
23
use Symfony\Component\Console\Input\InputDefinition;
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\Console\Input\InputOption;
26
use Symfony\Component\Console\Output\OutputInterface;
27
use Symfony\Component\Console\Output\StreamOutput;
28
use Symfony\Component\Filesystem\Filesystem;
29
30
/**
31
 * @author Gaurav Singh Faujdar <[email protected]>
32
 */
33
class AdminMakerTest extends TestCase
34
{
35
    /**
36
     * @var string
37
     */
38
    private $projectDirectory;
39
    /**
40
     * @var array
41
     */
42
    private $modelManagers = [];
43
    /**
44
     * @var InputInterface
45
     */
46
    private $input;
47
    /**
48
     * @var OutputInterface
49
     */
50
    private $output;
51
    /**
52
     * @var ConsoleStyle
53
     */
54
    private $io;
55
    /**
56
     * @var Generator
57
     */
58
    private $generator;
59
    /**
60
     * @var string
61
     */
62
    private $servicesFile;
63
64
    protected function setup()
65
    {
66
        if (5 == PHP_MAJOR_VERSION || !class_exists('Symfony\Component\Console\CommandLoader\CommandLoaderInterface')) {
67
            $this->markTestSkipped('Test only available for PHP 7 and SF 3.4');
68
        }
69
70
        $managerOrmProxy = $this->prophesize(ModelManagerInterface::class);
71
        $managerOrmProxy->getExportFields(Argument::exact('Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo'))
72
            ->willReturn(['bar', 'baz']);
73
74
        $this->modelManagers = ['sonata.admin.manager.orm' => $managerOrmProxy->reveal()];
75
        $this->servicesFile = sprintf('%s.yml', lcg_value());
76
        $this->projectDirectory = sys_get_temp_dir();
77
    }
78
79
    protected function tearDown()
80
    {
81
        @unlink($this->projectDirectory.'/config/'.$this->servicesFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
82
    }
83
84
    public function testExecute()
85
    {
86
        $maker = new AdminMaker($this->projectDirectory, $this->modelManagers);
87
88
        $in = [
89
            'model' => \Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo::class,
90
            '--admin' => 'FooAdmin',
91
            '--controller' => 'FooAdminController',
92
            '--services' => $this->servicesFile,
93
            '--id' => 'acme_demo_admin.admin.foo',
94
        ];
95
96
        $definition = new InputDefinition([
97
            new InputArgument('model', InputArgument::REQUIRED),
98
            new InputOption('admin', 'a', InputOption::VALUE_REQUIRED),
99
            new InputOption('controller', 'c', InputOption::VALUE_REQUIRED),
100
            new InputOption('manager', 'm', InputOption::VALUE_REQUIRED),
101
            new InputOption('services', 'y', InputOption::VALUE_REQUIRED),
102
            new InputOption('id', 'i', InputOption::VALUE_REQUIRED),
103
        ]);
104
105
        $this->input = new ArrayInput($in, $definition);
106
107
        $this->output = new StreamOutput(fopen('php://memory', 'wb', false));
108
109
        $this->io = new ConsoleStyle($this->input, $this->output);
110
        $fileManager = new FileManager(new Filesystem(), '.');
111
        $fileManager->setIO($this->io);
112
        $this->generator = new Generator($fileManager, 'Sonata\AdminBundle\Tests');
113
114
        $maker->generate($this->input, $this->io, $this->generator);
115
    }
116
}
117