it_slugifies_the_name_property()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Modules\Block\Tests\Integration;
4
5
use Faker\Factory;
6
use Illuminate\Support\Facades\App;
7
use Illuminate\Support\Facades\Event;
8
use Modules\Block\Events\BlockIsCreating;
9
use Modules\Block\Events\BlockIsUpdating;
10
use Modules\Block\Events\BlockWasCreated;
11
use Modules\Block\Events\BlockWasUpdated;
12
use Modules\Block\Facades\BlockFacade as Block;
13
14
class EloquentBlockRepositoryTest extends BaseBlockTest
15
{
16
    /** @test */
17
    public function it_creates_blocks()
18
    {
19
        $block = $this->block->create(['name' => 'testBlock', 'en' => ['body' => 'lorem en'], 'fr' => ['body' => 'lorem fr']]);
20
        $blocks = $this->block->all();
21
22
        $this->assertCount(1, $blocks);
23
        $this->assertEquals('testblock', $block->name);
24
        $this->assertEquals('lorem en', $block->translate('en')->body);
25
        $this->assertEquals('lorem fr', $block->translate('fr')->body);
26
    }
27
28
    /** @test */
29
    public function it_gets_only_online_blocks()
30
    {
31
        $this->createRandomBlock();
32
        $this->createRandomBlock(true, true);
33
        $this->createRandomBlock(true, true);
34
        $this->createRandomBlock(true, false);
35
36
        $allBlocks = $this->block->all();
37
        $onlineBlocksFr = $this->block->allOnlineInLang('fr');
38
        $onlineBlocksEn = $this->block->allOnlineInLang('en');
39
40
        $this->assertCount(4, $allBlocks);
41
        $this->assertCount(2, $onlineBlocksFr);
42
        $this->assertCount(3, $onlineBlocksEn);
43
    }
44
45
    /** @test */
46
    public function it_gets_block_by_name()
47
    {
48
        $this->block->create(['name' => 'testblock', 'en' => ['body' => 'lorem en', 'online' => true], 'fr' => ['body' => 'lorem fr', 'online' => true]]);
49
        $this->createRandomBlock(true, true);
50
        $this->createRandomBlock(true, true);
51
        $this->createRandomBlock(true, true);
52
53
        $block = $this->block->get('testblock');
54
55
        $this->assertEquals('lorem en', $block);
56
    }
57
58
    /** @test */
59
    public function it_gets_block_by_name_if_online()
60
    {
61
        $this->block->create(['name' => 'testBlock', 'en' => ['body' => 'lorem en', 'online' => true], 'fr' => ['body' => 'lorem fr', 'online' => false]]);
62
        $this->createRandomBlock(true, true);
63
        $this->createRandomBlock(true, true);
64
        $this->createRandomBlock(true, true);
65
66
        App::setLocale('fr');
67
        $block = $this->block->get('testBlock');
68
        $this->assertEquals('', $block);
69
70
        App::setLocale('en');
71
        $block = $this->block->get('testblock');
72
        $this->assertEquals('lorem en', $block);
73
    }
74
75
    /** @test */
76
    public function it_gets_block_by_facade()
77
    {
78
        $this->block->create(['name' => 'testblock', 'en' => ['body' => 'lorem en', 'online' => true], 'fr' => ['body' => 'lorem fr', 'online' => false]]);
79
        $this->createRandomBlock(true, true);
80
        $this->createRandomBlock(true, true);
81
82
        $block = Block::get('testblock');
83
84
        $this->assertEquals('lorem en', $block);
85
    }
86
87
    /** @test */
88
    public function it_slugifies_the_name_property()
89
    {
90
        $block = $this->block->create(['name' => 'test block', 'en' => ['body' => 'lorem en', 'online' => true], 'fr' => ['body' => 'lorem fr', 'online' => false]]);
91
92
        $this->assertEquals('test-block', $block->name);
93
    }
94
95 View Code Duplication
    public function it_makes_name_unique()
96
    {
97
        $this->block->create(['name' => 'test block']);
98
        $block = $this->block->create(['name' => 'test block']);
99
100
        $this->assertEquals('test-block-1', $block->name);
101
    }
102
103
    /** @test */
104
    public function it_increments_name_if_not_unique()
105
    {
106
        $this->block->create(['name' => 'test block']);
107
        $this->block->create(['name' => 'test block']);
108
        $block1 = $this->block->create(['name' => 'test block']);
109
        $block2 = $this->block->create(['name' => 'test block']);
110
111
        $this->assertEquals('test-block-2', $block1->name);
112
        $this->assertEquals('test-block-3', $block2->name);
113
    }
114
115
    /** @test */
116 View Code Duplication
    public function it_updates_block_without_name_change()
117
    {
118
        $block = $this->block->create(['name' => 'test block']);
119
        $this->block->update($block, ['name' => 'test-block']);
120
121
        $this->assertEquals($block->name, 'test-block');
122
    }
123
124
    /** @test */
125 View Code Duplication
    public function it_updates_block_with_name_change()
126
    {
127
        $block = $this->block->create(['name' => 'test block']);
128
        $this->block->update($block, ['name' => 'my awesome block']);
129
130
        $this->assertEquals($block->name, 'my-awesome-block');
131
    }
132
133
    /** @test */
134
    public function it_returns_empty_string_if_block_doesnt_exist()
135
    {
136
        $block = $this->block->get('heya');
137
138
        $this->assertSame('', $block);
139
    }
140
141
    /** @test */
142
    public function it_triggers_event_when_block_was_created()
143
    {
144
        Event::fake();
145
146
        $block = $this->createRandomBlock();
147
148
        Event::assertDispatched(BlockWasCreated::class, function ($e) use ($block) {
149
            return $e->block->name === $block->name;
150
        });
151
    }
152
153
    /** @test */
154
    public function it_triggers_event_when_block_is_creating()
155
    {
156
        Event::fake();
157
158
        $block = $this->createRandomBlock();
159
160
        Event::assertDispatched(BlockIsCreating::class, function ($e) use ($block) {
161
            return $e->getAttribute('name') === $block->name;
162
        });
163
    }
164
165
    /** @test */
166
    public function it_can_change_data_when_it_is_creating_event()
167
    {
168 View Code Duplication
        Event::listen(BlockIsCreating::class, function (BlockIsCreating $event) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
            $event->setAttributes(['name' => 'awesome block']);
170
            $event->setAttributes([
171
                'en' => ['body' => 'no more lorem! en'],
172
                'fr' => ['body' => 'no more lorem! fr'],
173
            ]);
174
        });
175
176
        $block = $this->block->create(['name' => 'testBlock', 'en' => ['body' => 'lorem en'], 'fr' => ['body' => 'lorem fr']]);
177
178
        $this->assertEquals('awesome block', $block->name);
179
        $this->assertEquals('no more lorem! en', $block->translate('en')->body);
180
        $this->assertEquals('no more lorem! fr', $block->translate('fr')->body);
181
    }
182
183
    /** @test */
184 View Code Duplication
    public function it_triggers_event_when_block_was_updated()
185
    {
186
        Event::fake();
187
188
        $block = $this->createRandomBlock();
189
        $block = $this->block->update($block, ['name' => 'something else']);
190
191
        Event::assertDispatched(BlockWasUpdated::class, function ($e) use ($block) {
192
            return $e->block->name === $block->name;
193
        });
194
    }
195
196
    /** @test */
197 View Code Duplication
    public function it_triggers_event_when_block_is_updating()
198
    {
199
        Event::fake();
200
201
        $block = $this->createRandomBlock();
202
        $block = $this->block->update($block, ['name' => 'something else']);
203
204
        Event::assertDispatched(BlockIsUpdating::class, function ($e) use ($block) {
205
            return $e->getAttribute('name') === $block->name;
206
        });
207
    }
208
209
    /** @test */
210
    public function it_can_change_data_when_it_is_updating_event()
211
    {
212 View Code Duplication
        Event::listen(BlockIsUpdating::class, function (BlockIsUpdating $event) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
213
            $event->setAttributes(['name' => 'awesome block']);
214
            $event->setAttributes([
215
                'en' => ['body' => 'no more lorem! en'],
216
                'fr' => ['body' => 'no more lorem! fr'],
217
            ]);
218
        });
219
220
        $block = $this->createRandomBlock();
221
        $block = $this->block->update($block, ['name' => 'something else']);
222
223
        $this->assertEquals('awesome block', $block->name);
224
        $this->assertEquals('no more lorem! en', $block->translate('en')->body);
225
        $this->assertEquals('no more lorem! fr', $block->translate('fr')->body);
226
    }
227
228
    /**
229
     * Create a block with random properties
230
     * @param bool $statusEn
231
     * @param bool $statusFr
232
     * @return mixed
233
     */
234
    private function createRandomBlock($statusEn = false, $statusFr = false)
235
    {
236
        $factory = Factory::create();
237
238
        $data = [
239
            'name' => $factory->word,
240
            'en' => [
241
                'body' => $factory->text,
242
                'online' => $statusEn,
243
            ],
244
            'fr' => [
245
                'body' => $factory->text,
246
                'online' => $statusFr,
247
            ],
248
        ];
249
250
        return $this->block->create($data);
251
    }
252
}
253