HasReadWriteProperties   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 15
c 1
b 0
f 0
dl 0
loc 46
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A testPropertiesWithFullAccessAreReadablesAndWritables() 0 30 2
1
<?php
2
3
/**
4
 * PHP: Nelson Martell Library file
5
 *
6
 * Copyright © 2016-2021 Nelson Martell (http://nelson6e65.github.io)
7
 *
8
 * Licensed under The MIT License (MIT)
9
 * For full copyright and license information, please see the LICENSE
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright 2016-2021 Nelson Martell
13
 * @link      http://nelson6e65.github.io/php_nml/
14
 * @since     v0.7.0
15
 * @license   http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
16
 * */
17
18
declare(strict_types=1);
19
20
namespace NelsonMartell\Test\Helpers;
21
22
use NelsonMartell\Extensions\Text;
23
use NelsonMartell\IStrictPropertiesContainer;
24
use SebastianBergmann\Exporter\Exporter;
25
use PHPUnit\Framework\TestCase;
26
27
/**
28
 * Split of ImplementsIStrictPropertiesContainer, for classes implementing any read-only property
29
 *
30
 * @author Nelson Martell <[email protected]>
31
 * */
32
trait HasReadWriteProperties
33
{
34
    abstract public function testImplementsIStrictPropertiesContainerInterface(): void;
35
36
    abstract public function readwritePropertiesProvider(): array;
37
38
39
    /**
40
     * @depends testImplementsIStrictPropertiesContainerInterface
41
     * @dataProvider readwritePropertiesProvider
42
     *
43
     * @param IStrictPropertiesContainer    $obj
44
     * @param string                        $property
45
     * @param mixed                         $value
46
     * @param mixed                         $expected
47
     */
48
    public function testPropertiesWithFullAccessAreReadablesAndWritables(
49
        IStrictPropertiesContainer $obj,
50
        string $property,
51
        $value,
52
        $expected
53
    ): void {
54
        $exporter = new Exporter();
55
56
        $var = get_class($obj);
57
        $var = Text::variable(substr(
58
            $var,
59
            strrpos($var, '\\') === false ? 0 : strrpos($var, '\\') + 1
60
        ));
61
62
        $obj->$property = $value;
63
64
        $actual = $obj->$property;
65
66
        $message = Text::format(
67
            '${var}->{property} = {value}; $actual = ${var}->{property}; // {actual}',
68
            [
69
                'var'      => $var,
70
                'property' => $property,
71
                'actual'   => $exporter->shortenedExport($actual),
72
                'value'    => $exporter->shortenedExport($value),
73
            ]
74
        );
75
76
        /** @var TestCase $this */
77
        $this->assertEquals($expected, $actual, $message);
78
    }
79
}
80