Completed
Push — 3.x ( f37b5a...b37d45 )
by Christian
26:43
created

ServicesManipulatorTest::setUp()   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 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\Manipulator;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Manipulator\ServicesManipulator;
18
19
/**
20
 * @author Marek Stipek <[email protected]>
21
 */
22
class ServicesManipulatorTest extends TestCase
23
{
24
    /** @var ServicesManipulator */
25
    private $servicesManipulator;
26
27
    /** @var string */
28
    private $file;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected function setUp(): void
34
    {
35
        $this->file = sprintf('%s/%s.yml', sys_get_temp_dir(), lcg_value());
36
        $this->servicesManipulator = new ServicesManipulator($this->file);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function tearDown(): void
43
    {
44
        @unlink($this->file);
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...
45
    }
46
47
    public function testAddResource(): void
48
    {
49
        $this->servicesManipulator->addResource(
50
            'service_id',
51
            'class',
52
            'admin_class',
53
            'controller_name',
54
            'manager_type'
55
        );
56
        $this->assertSame(
57
            "services:
58
    service_id:
59
        class: admin_class
60
        arguments: [~, class, controller_name]
61
        tags:
62
            - { name: sonata.admin, manager_type: manager_type, group: admin, label: class }
63
        public: true\n",
64
            file_get_contents($this->file)
65
        );
66
        $this->servicesManipulator->addResource(
67
            'another_service_id',
68
            'another_class',
69
            'another_admin_class',
70
            'another_controller_name',
71
            'another_manager_type'
72
        );
73
        $this->assertSame(
74
            "services:
75
    service_id:
76
        class: admin_class
77
        arguments: [~, class, controller_name]
78
        tags:
79
            - { name: sonata.admin, manager_type: manager_type, group: admin, label: class }
80
        public: true
81
82
    another_service_id:
83
        class: another_admin_class
84
        arguments: [~, another_class, another_controller_name]
85
        tags:
86
            - { name: sonata.admin, manager_type: another_manager_type, group: admin, label: another_class }
87
        public: true\n",
88
            file_get_contents($this->file)
89
        );
90
    }
91
92
    public function testAddResourceShouldThrowException(): void
93
    {
94
        $this->expectException(\RuntimeException::class);
95
        $this->expectExceptionMessage('The service "service_id" is already defined');
96
97
        $this->servicesManipulator->addResource(
98
            'service_id',
99
            'class',
100
            'admin_class',
101
            'controller_name',
102
            'manager_type'
103
        );
104
        $this->servicesManipulator->addResource(
105
            'service_id',
106
            'class',
107
            'admin_class',
108
            'controller_name',
109
            'manager_type'
110
        );
111
    }
112
113
    public function testAddResourceWithEmptyServices(): void
114
    {
115
        file_put_contents($this->file, 'services:');
116
        $this->servicesManipulator->addResource(
117
            'service_id',
118
            'class',
119
            'admin_class',
120
            'controller_name',
121
            'manager_type'
122
        );
123
        $this->assertSame(
124
            "services:
125
    service_id:
126
        class: admin_class
127
        arguments: [~, class, controller_name]
128
        tags:
129
            - { name: sonata.admin, manager_type: manager_type, group: admin, label: class }
130
        public: true\n",
131
            file_get_contents($this->file)
132
        );
133
    }
134
}
135