Completed
Pull Request — master (#6194)
by Vincent
05:04
created

CreateClassCacheCommandTest::testExecute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Command;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Command\CreateClassCacheCommand;
18
use Symfony\Component\Console\Application;
19
use Symfony\Component\Console\Tester\CommandTester;
20
21
/**
22
 * @group legacy
23
 *
24
 * NEXT_MAJOR: Remove this class.
25
 *
26
 * @author Andrej Hudec <[email protected]>
27
 */
28
class CreateClassCacheCommandTest extends TestCase
29
{
30
    /**
31
     * @var string
32
     */
33
    private $tempDirectory;
34
35
    /**
36
     * @var Application
37
     */
38
    private $application;
39
40
    protected function setUp(): void
41
    {
42
        $tempFile = tempnam(sys_get_temp_dir(), 'sonata_');
43
        if (file_exists($tempFile)) {
44
            unlink($tempFile);
45
        }
46
47
        if (mkdir($tempFile)) {
48
            $this->tempDirectory = $tempFile;
49
            file_put_contents(sprintf('%s/classes.map', $this->tempDirectory), '<?php return [\'Sonata\\AdminBundle\\Tests\\Fixtures\\Controller\\FooAdminController\', \'Sonata\\AdminBundle\\Tests\\Fixtures\\Controller\\BarAdminController\',];');
50
        } else {
51
            $this->markTestSkipped(sprintf('Temp directory "%s" creation error.', $tempFile));
52
        }
53
54
        $this->application = new Application();
55
        $command = new CreateClassCacheCommand($this->tempDirectory, false);
0 ignored issues
show
Deprecated Code introduced by
The class Sonata\AdminBundle\Command\CreateClassCacheCommand has been deprecated with message: since version sonata-project/admin-bundle 3.39.0 and will be removed in 4.0.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
56
57
        $this->application->add($command);
58
    }
59
60
    protected function tearDown(): void
61
    {
62
        if ($this->tempDirectory) {
63
            if (file_exists(sprintf('%s/classes.map', $this->tempDirectory))) {
64
                unlink(sprintf('%s/classes.map', $this->tempDirectory));
65
            }
66
67
            if (file_exists(sprintf('%s/classes.php', $this->tempDirectory))) {
68
                unlink(sprintf('%s/classes.php', $this->tempDirectory));
69
            }
70
71
            if (file_exists($this->tempDirectory) && is_dir($this->tempDirectory)) {
72
                rmdir($this->tempDirectory);
73
            }
74
        }
75
    }
76
77
    public function testExecute(): void
78
    {
79
        $this->markTestSkipped();
80
        $this->assertFileExists(sprintf('%s/classes.map', $this->tempDirectory));
81
        $this->assertFileNotExists(sprintf('%s/classes.php', $this->tempDirectory));
82
83
        $command = $this->application->find('cache:create-cache-class');
84
        $commandTester = new CommandTester($command);
85
        $commandTester->execute(['command' => $command->getName()]);
86
87
        $this->assertRegExp('@Writing cache file ...\s+done!@', $commandTester->getDisplay());
88
89
        $this->assertFileExists(sprintf('%s/classes.php', $this->tempDirectory));
90
        $this->assertFileEquals(sprintf('%s/../Fixtures/Command/classes.php', __DIR__), sprintf('%s/classes.php', $this->tempDirectory));
91
    }
92
93
    public function testExecuteWithException(): void
94
    {
95
        $this->assertFileExists(sprintf('%s/classes.map', $this->tempDirectory));
96
        unlink(sprintf('%s/classes.map', $this->tempDirectory));
97
98
        try {
99
            $command = $this->application->find('cache:create-cache-class');
100
            $commandTester = new CommandTester($command);
101
            $commandTester->execute(['command' => $command->getName()]);
102
        } catch (\RuntimeException $e) {
103
            $this->assertSame(sprintf('The file %s/classes.map does not exist', $this->tempDirectory), $e->getMessage());
104
105
            return;
106
        }
107
108
        $this->fail('An expected exception has not been raised.');
109
    }
110
}
111