File: MacroTest.php

package info (click to toggle)
php-nesbot-carbon 2.65.0-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 18,280 kB
  • sloc: php: 157,346; xml: 42; makefile: 26; sh: 12
file content (253 lines) | stat: -rw-r--r-- 6,797 bytes parent folder | download | duplicates (2)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php

declare(strict_types=1);

/**
 * This file is part of the Carbon package.
 *
 * (c) Brian Nesbitt <brian@nesbot.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Tests\PHPStan;

use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Carbon\PHPStan\Macro;
use ReflectionClass;
use Tests\AbstractTestCase;

class MacroTest extends AbstractTestCase
{
    public function testIsStatic()
    {
        $macro = new Macro(Carbon::class, 'calendarBerlin', static function (): string {
            return self::this()->tz('Europe/Berlin')->calendar();
        });

        $this->assertTrue($macro->isStatic());

        $macro = new Macro(Carbon::class, 'calendarBerlin', function (): string {
            return $this->tz('Europe/Berlin')->calendar();
        });

        $this->assertFalse($macro->isStatic());
    }

    public function testGetDeclaringClass()
    {
        $macro = new Macro(Carbon::class, 'lower', 'strtolower');

        $this->assertSame(Carbon::class, $macro->getDeclaringClass()->getName());

        $macro = new Macro(CarbonImmutable::class, 'lower', 'strtolower');

        $this->assertSame(CarbonImmutable::class, $macro->getDeclaringClass()->getName());
    }

    public function testIsPrivate()
    {
        $macro = new Macro(Carbon::class, 'lower', 'strtolower');

        $this->assertFalse($macro->isPrivate());
    }

    public function testIsPublic()
    {
        $macro = new Macro(Carbon::class, 'lower', 'strtolower');

        $this->assertTrue($macro->isPublic());
    }

    public function testIsFinal()
    {
        $mixinClass = new class() {
            // Declaring final won't apply for macro, sub-class will always be able to override macros.
            final public static function foo(): string
            {
                return 'foo';
            }

            public static function bar(): string
            {
                return 'bar';
            }
        };

        $macro = new Macro(Carbon::class, 'foo', [$mixinClass, 'foo']);

        $this->assertFalse($macro->isFinal());

        $macro = new Macro(Carbon::class, 'bar', [$mixinClass, 'bar']);

        $this->assertFalse($macro->isFinal());
    }

    public function testIsInternal()
    {
        $macro = new Macro(Carbon::class, 'lower', 'strtolower');

        $this->assertFalse($macro->isInternal());
    }

    public function testIsAbstract()
    {
        $macro = new Macro(Carbon::class, 'lower', 'strtolower');

        $this->assertFalse($macro->isAbstract());
    }

    public function testGetDocComment()
    {
        $macro = new Macro(
            Carbon::class,
            'foo',
            /**
             * Foo.
             */
            function () {
                return 'foo';
            }
        );

        $this->assertSame(
            "/**\n* Foo.\n*/",
            preg_replace('/^[\t ]+/m', '', $macro->getDocComment())
        );
    }

    public function testGetFileName()
    {
        $macro = new Macro(Carbon::class, 'foo', function () {
        });

        $this->assertSame(__FILE__, $macro->getFileName());
    }

    public function testGetName()
    {
        $macro = new Macro(Carbon::class, 'lower', 'strtolower');

        $this->assertSame('lower', $macro->getName());
    }

    public function testGetParameters()
    {
        $macro = new Macro(Carbon::class, 'lower', function () {
        });

        $this->assertSame([], $macro->getParameters());

        $macro = new Macro(Carbon::class, 'lower', function (string $a, $b = 9) {
        });
        $parameters = $macro->getParameters();

        $this->assertCount(2, $parameters);
        $this->assertSame('a', $parameters[0]->getName());
        $this->assertTrue($parameters[0]->hasType());
        $this->assertFalse($parameters[0]->isDefaultValueAvailable());
        $this->assertSame('string', $parameters[0]->getType()->getName());
        $this->assertSame('b', $parameters[1]->getName());
        $this->assertTrue($parameters[1]->isDefaultValueAvailable());
        $this->assertFalse($parameters[1]->hasType());
        $this->assertNull($parameters[1]->getType());
        $this->assertSame(9, $parameters[1]->getDefaultValue());
    }

    public function testGetReturnType()
    {
        $macro = new Macro(Carbon::class, 'lower', function () {
        });

        $this->assertNull($macro->getReturnType());

        $macro = new Macro(Carbon::class, 'lower', function (): Carbon {
        });

        $this->assertSame(Carbon::class, $macro->getReturnType()->getName());
    }

    public function testGetLines()
    {
        $line = __LINE__;
        $macro = new Macro(Carbon::class, 'foo', function () {
            return 'foo';
        });

        $this->assertSame($line + 1, $macro->getStartLine());
        $this->assertSame($line + 3, $macro->getEndLine());
    }

    public function testIsDeprecated()
    {
        $macro = new Macro(
            Carbon::class,
            'lower',
            /**
             * @deprecated since 3.0.0
             */
            function () {
            }
        );

        $this->assertTrue($macro->isDeprecated()->yes());

        $macro = new Macro(
            Carbon::class,
            'lower',
            /**
             * @discouraged since 3.0.0
             */
            function () {
            }
        );

        $this->assertFalse($macro->isDeprecated()->yes());
    }

    public function testIsVariadic()
    {
        $macro = new Macro(Carbon::class, 'lower', function (...$params) {
        });

        $this->assertTrue($macro->isVariadic());

        $macro = new Macro(Carbon::class, 'lower', function ($params) {
        });

        $this->assertFalse($macro->isVariadic());
    }

    public function testGetPrototype()
    {
        $macro = new Macro(Carbon::class, 'lower', function () {
        });

        $this->assertSame($macro, $macro->getPrototype());
    }

    public function testGetReflection()
    {
        require_once __DIR__.'/MixinClass.php';

        $mixinClass = MixinClass::class;

        $macro = new Macro(Carbon::class, 'foo', [$mixinClass, 'foo']);

        $this->assertSame(
            (new ReflectionClass($mixinClass))->getName(),
            $macro->getReflection()->getDeclaringClass()->getName()
        );
        $this->assertSame('foo', $macro->getReflection()->getName());
        $this->assertNull($macro->getTentativeReturnType());
        $this->assertTrue($macro->returnsByReference()->no());

        $macro = new Macro(Carbon::class, 'bar', function () {
        });

        $this->assertNull($macro->getReflection());
        $this->assertNull($macro->getTentativeReturnType());
    }
}