Passed
Pull Request — master (#11)
by Victor
01:49
created

testGenericPresentationModelsShouldOnlyAllowStringVariableNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
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
    /**
13
     * @return void
14
     */
15
    public function testGenericPresentationModelsShouldOnlyAllowStringVariableNames()
16
    {
17
        $this->expectException(TypeError::class);
18
19
        new PresentationModel([
20
            0 => 'A non-string key causes a type error to be thrown',
21
        ]);
22
    }
23
24
    /**
25
     * @return void
26
     */
27
    public function testSpecificPresentationModelsShouldOnlySetDefinedVariables()
28
    {
29
        $presentationModel = new class ([
30
            'name' => 'name',
31
            'non_existing_variable' => 'non_existing_variable',
32
        ]) extends PresentationModel
33
        {
34
            protected $name = '';
35
        };
36
37
        $variables = $presentationModel->getVariables();
38
39
        $this->assertArrayHasKey('name', $variables);
40
        $this->assertArrayNotHasKey('non_existing_variable', $variables);
41
    }
42
43
    /**
44
     * @return void
45
     */
46
    public function testGetVariableShouldReturnValueOfVariable()
47
    {
48
        $presentationModel = new PresentationModel([
49
            'name' => 'name',
50
        ]);
51
52
        $this->assertSame('name', $presentationModel->getVariable('name', 'default'));
53
    }
54
55
    /**
56
     * @return void
57
     */
58
    public function testGetVariableShouldReturnDefaultValueIfVariableDoesNotExist()
59
    {
60
        $presentationModel = new PresentationModel();
61
62
        $this->assertSame('default', $presentationModel->getVariable('does_not_exist', 'default'));
63
    }
64
}
65