File: NamedParameterMapTest.php

package info (click to toggle)
php-ramsey-collection 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 612 kB
  • sloc: php: 3,239; xml: 31; makefile: 22
file content (144 lines) | stat: -rw-r--r-- 5,315 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php

declare(strict_types=1);

namespace Ramsey\Collection\Test\Map;

use DateTime;
use Ramsey\Collection\Exception\InvalidArgumentException;
use Ramsey\Collection\Map\NamedParameterMap;
use Ramsey\Collection\Test\Mock\Foo;
use Ramsey\Collection\Test\TestCase;
use stdClass;

use function fopen;

/**
 * Tests for NamedParameterMap
 */
class NamedParameterMapTest extends TestCase
{
    public function testNamedParameters(): void
    {
        $inputParams = [
            'myArray' => 'array',
            'myBool' => 'bool',
            'myCallable' => 'callable',
            'myFloat' => 'float',
            'myDouble' => 'double',
            'myInt' => 'int',
            'myInteger' => 'integer',
            'myNull' => 'null',
            'myNumericFloat' => 'numeric',
            'myNumericInt' => 'numeric',
            'myObject' => 'object',
            'myResource' => 'resource',
            'myScalarString' => 'scalar',
            'myScalarInt' => 'scalar',
            'myScalarFloat' => 'scalar',
            'myScalarBool' => 'scalar',
            'myString' => 'string',
            'myFoo' => Foo::class,
            'myMixedInt',
            'myMixedObject',
            'myMixedNull',
            'myMixedResource',
        ];

        $expectedParams = [
            'myArray' => 'array',
            'myBool' => 'bool',
            'myCallable' => 'callable',
            'myFloat' => 'float',
            'myDouble' => 'double',
            'myInt' => 'int',
            'myInteger' => 'integer',
            'myNull' => 'null',
            'myNumericFloat' => 'numeric',
            'myNumericInt' => 'numeric',
            'myObject' => 'object',
            'myResource' => 'resource',
            'myScalarString' => 'scalar',
            'myScalarInt' => 'scalar',
            'myScalarFloat' => 'scalar',
            'myScalarBool' => 'scalar',
            'myString' => 'string',
            'myFoo' => Foo::class,
            'myMixedInt' => 'mixed',
            'myMixedObject' => 'mixed',
            'myMixedNull' => 'mixed',
            'myMixedResource' => 'mixed',
        ];

        $namedParameterMap = new NamedParameterMap($inputParams);

        $namedParameterMap['myArray'] = $this->faker->words();
        $namedParameterMap['myBool'] = $this->faker->boolean();
        $namedParameterMap['myCallable'] = fn (): bool => true;
        $namedParameterMap['myFloat'] = $this->faker->randomFloat();
        $namedParameterMap['myDouble'] = $this->faker->randomFloat();
        $namedParameterMap['myInt'] = $this->faker->randomNumber();
        $namedParameterMap['myInteger'] = $this->faker->randomNumber();
        $namedParameterMap['myNull'] = null;
        $namedParameterMap['myNumericFloat'] = (string) $this->faker->randomFloat();
        $namedParameterMap['myNumericInt'] = (string) $this->faker->randomNumber();
        $namedParameterMap['myObject'] = new stdClass();
        $namedParameterMap['myResource'] = fopen('php://memory', 'rb');
        $namedParameterMap['myScalarString'] = $this->faker->name();
        $namedParameterMap['myScalarInt'] = $this->faker->randomNumber();
        $namedParameterMap['myScalarFloat'] = $this->faker->randomFloat();
        $namedParameterMap['myScalarBool'] = $this->faker->boolean();
        $namedParameterMap['myString'] = $this->faker->text();
        $namedParameterMap['myFoo'] = new Foo();
        $namedParameterMap['myMixedInt'] = $this->faker->randomNumber();
        $namedParameterMap['myMixedObject'] = new Foo();
        $namedParameterMap['myMixedNull'] = null;
        $namedParameterMap['myMixedResource'] = fopen('php://memory', 'rb');

        $this->assertSame(
            $expectedParams,
            $namedParameterMap->getNamedParameters(),
        );
    }

    public function testNamedParametersWithUnnamedParameterThrowException(): void
    {
        $namedParameterMap = new NamedParameterMap(['foo']);

        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Attempting to set value for unconfigured parameter \'bar\'');
        $namedParameterMap['bar'] = 123;
    }

    public function testNamedParametersWithWrongTypeThrowsException(): void
    {
        $namedParameterMap = new NamedParameterMap(['foo' => 'int']);

        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Value for \'foo\' must be of type int');
        $namedParameterMap['foo'] = $this->faker->text();
    }

    public function testNamedParameterWithNoStringValue(): void
    {
        $namedParameterMap = new NamedParameterMap(['foo' => 'int']);

        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Value for \'foo\' must be of type int');
        $namedParameterMap['foo'] = new DateTime();
    }

    public function testNamedParametersWithNullKeyThrowsException(): void
    {
        $namedParameterMap = new NamedParameterMap(['foo']);

        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage("Attempting to set value for unconfigured parameter 'NULL'");

        /**
         * @phpstan-ignore-next-line
         * @psalm-suppress NullArgument
         */
        $namedParameterMap[] = 123;
    }
}