testGetVariableShouldReturnDefaultValueIfVariableDoesNotExist()
last analyzed

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
c 0
b 0
f 0
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shoot\Shoot\Tests\Unit;
5
6
use PHPUnit\Framework\TestCase;
7
use Shoot\Shoot\PresentationModel;
8
use TypeError;
9
10
final class PresentationModelTest extends TestCase
11
{
12
    public function testGenericPresentationModelsShouldOnlyAllowStringVariableNames(): void
13
    {
14
        $this->expectException(TypeError::class);
15
16
        new PresentationModel([
17
            0 => 'A non-string key causes a type error to be thrown',
18
        ]);
19
    }
20
21
    public function testSpecificPresentationModelsShouldOnlySetDefinedVariables(): void
22
    {
23
        $presentationModel = new class ([
24
            'name' => 'name',
25
            'non_existing_variable' => 'non_existing_variable',
26
        ]) extends PresentationModel
27
        {
28
            protected $name = '';
29
        };
30
31
        $variables = $presentationModel->getVariables();
32
33
        $this->assertArrayHasKey('name', $variables);
34
        $this->assertArrayNotHasKey('non_existing_variable', $variables);
35
    }
36
37
    public function testGetVariableShouldReturnValueOfVariable(): void
38
    {
39
        $presentationModel = new PresentationModel([
40
            'name' => 'name',
41
        ]);
42
43
        $this->assertSame('name', $presentationModel->getVariable('name', 'default'));
44
    }
45
46
    public function testGetVariableShouldReturnDefaultValueIfVariableDoesNotExist(): void
47
    {
48
        $presentationModel = new PresentationModel();
49
50
        $this->assertSame('default', $presentationModel->getVariable('does_not_exist', 'default'));
51
    }
52
}
53