Visitar URL original
Merge pull request #20148 from MauricioFauth/AbstractTestCase-reflect… · phpmyadmin/phpmyadmin@95790b1 · GitHub
Skip to content

Commit 95790b1

Browse files
Merge pull request #20148 from MauricioFauth/AbstractTestCase-reflections
Inline some methods from AbstractTestCase
2 parents 1f7cd97 + b93e747 commit 95790b1

27 files changed

Lines changed: 313 additions & 784 deletions

‎phpstan-baseline.neon‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15282,12 +15282,6 @@ parameters:
1528215282
count: 1
1528315283
path: tests/unit/Database/CentralColumnsTest.php
1528415284

15285-
-
15286-
message: '#^Parameter \#1 \$needle of static method PHPUnit\\Framework\\Assert\:\:assertStringContainsString\(\) expects string, mixed given\.$#'
15287-
identifier: argument.type
15288-
count: 1
15289-
path: tests/unit/Database/CentralColumnsTest.php
15290-
1529115285
-
1529215286
message: '''
1529315287
#^Call to deprecated method getInstance\(\) of class PhpMyAdmin\\Dbal\\DatabaseInterface\:
@@ -15465,7 +15459,7 @@ parameters:
1546515459
-
1546615460
message: '#^Parameter \#2 \$haystack of static method PHPUnit\\Framework\\Assert\:\:assertStringContainsString\(\) expects string, mixed given\.$#'
1546715461
identifier: argument.type
15468-
count: 4
15462+
count: 2
1546915463
path: tests/unit/Display/ResultsTest.php
1547015464

1547115465
-

‎psalm-baseline.xml‎

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9757,14 +9757,6 @@
97579757
<DeprecatedMethod>
97589758
<code><![CDATA[Config::getInstance()]]></code>
97599759
</DeprecatedMethod>
9760-
<MixedArgument>
9761-
<code><![CDATA[$this->callFunction(
9762-
$this->centralColumns,
9763-
CentralColumns::class,
9764-
'getHtmlForEditTableRow',
9765-
[self::MODIFIED_COLUMN_DATA[0], 0],
9766-
)]]></code>
9767-
</MixedArgument>
97689760
</file>
97699761
<file src="tests/unit/Database/DesignerTest.php">
97709762
<DeprecatedMethod>
@@ -9848,28 +9840,6 @@
98489840
<MixedArgument>
98499841
<code><![CDATA[$output]]></code>
98509842
<code><![CDATA[$output]]></code>
9851-
<code><![CDATA[$this->callFunction(
9852-
$this->object,
9853-
DisplayResults::class,
9854-
'getDataCellForNonNumericColumns',
9855-
[
9856-
$column,
9857-
$class,
9858-
$meta,
9859-
$map,
9860-
$urlParams,
9861-
$conditionField,
9862-
$transformationPlugin,
9863-
$transformOptions,
9864-
$statementInfo,
9865-
],
9866-
)]]></code>
9867-
<code><![CDATA[$this->callFunction(
9868-
$this->object,
9869-
DisplayResults::class,
9870-
'handleNonPrintableContents',
9871-
[$category, $content, $transformationPlugin, $transformOptions, $meta, $urlParams, &$isTruncated],
9872-
)]]></code>
98739843
</MixedArgument>
98749844
<MixedArrayAccess>
98759845
<code><![CDATA[$_SESSION['tmpval']['pftext']]]></code>

‎tests/unit/AbstractTestCase.php‎

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
use PhpMyAdmin\Template;
2121
use PhpMyAdmin\Tests\Stubs\DbiDummy;
2222
use PhpMyAdmin\Tracking\Tracker;
23-
use PhpMyAdmin\Utils\HttpRequest;
2423
use PHPUnit\Framework\TestCase;
25-
use ReflectionClass;
2624
use ReflectionProperty;
2725

2826
/**
@@ -105,61 +103,4 @@ protected function setLanguage(string $code = 'en'): void
105103
$languageManager->activate($languageEn);
106104
Translator::load();
107105
}
108-
109-
protected function setProxySettings(): void
110-
{
111-
HttpRequest::setProxySettingsFromEnv();
112-
}
113-
114-
/**
115-
* Call protected functions by setting visibility to public.
116-
*
117-
* @param object|null $object The object to inspect, pass null for static objects()
118-
* @param string $className The class name
119-
* @param string $methodName The method name
120-
* @param mixed[] $params The parameters for the invocation
121-
* @phpstan-param class-string $className
122-
*
123-
* @return mixed the output from the protected method.
124-
*/
125-
protected function callFunction(object|null $object, string $className, string $methodName, array $params): mixed
126-
{
127-
$class = new ReflectionClass($className);
128-
$method = $class->getMethod($methodName);
129-
130-
return $method->invokeArgs($object, $params);
131-
}
132-
133-
/**
134-
* Set a private or protected property via reflection.
135-
*
136-
* @param object|null $object The object to inspect, pass null for static objects()
137-
* @param string $className The class name
138-
* @param string $propertyName The method name
139-
* @param mixed $value The parameters for the invocation
140-
* @phpstan-param class-string $className
141-
*/
142-
protected function setProperty(object|null $object, string $className, string $propertyName, mixed $value): void
143-
{
144-
$class = new ReflectionClass($className);
145-
$property = $class->getProperty($propertyName);
146-
147-
$property->setValue($object, $value);
148-
}
149-
150-
/**
151-
* Get a private or protected property via reflection.
152-
*
153-
* @param object $object The object to inspect, pass null for static objects()
154-
* @param string $className The class name
155-
* @param string $propertyName The method name
156-
* @phpstan-param class-string $className
157-
*/
158-
protected function getProperty(object $object, string $className, string $propertyName): mixed
159-
{
160-
$class = new ReflectionClass($className);
161-
$property = $class->getProperty($propertyName);
162-
163-
return $property->getValue($object);
164-
}
165106
}

‎tests/unit/BrowseForeignersTest.php‎

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpMyAdmin\Template;
1111
use PhpMyAdmin\Theme\ThemeManager;
1212
use PHPUnit\Framework\Attributes\CoversClass;
13+
use ReflectionMethod;
1314

1415
#[CoversClass(BrowseForeigners::class)]
1516
class BrowseForeignersTest extends AbstractTestCase
@@ -70,33 +71,21 @@ public function testGetHtmlForGotoPage(): void
7071
$foreignData = new ForeignData(false, 5, '', null, '');
7172
self::assertSame(
7273
'',
73-
$this->callFunction(
74-
$this->browseForeigners,
75-
BrowseForeigners::class,
76-
'getHtmlForGotoPage',
77-
[$foreignData, 0],
78-
),
74+
(new ReflectionMethod(BrowseForeigners::class, 'getHtmlForGotoPage'))
75+
->invokeArgs($this->browseForeigners, [$foreignData, 0]),
7976
);
8077

8178
$foreignData = new ForeignData(false, 5, '', [], '');
8279

8380
self::assertSame(
8481
'',
85-
$this->callFunction(
86-
$this->browseForeigners,
87-
BrowseForeigners::class,
88-
'getHtmlForGotoPage',
89-
[$foreignData, 15],
90-
),
82+
(new ReflectionMethod(BrowseForeigners::class, 'getHtmlForGotoPage'))
83+
->invokeArgs($this->browseForeigners, [$foreignData, 15]),
9184
);
9285

9386
$foreignData = new ForeignData(false, 30, '', [], '');
94-
$result = $this->callFunction(
95-
$this->browseForeigners,
96-
BrowseForeigners::class,
97-
'getHtmlForGotoPage',
98-
[$foreignData, 15],
99-
);
87+
$result = (new ReflectionMethod(BrowseForeigners::class, 'getHtmlForGotoPage'))
88+
->invokeArgs($this->browseForeigners, [$foreignData, 15]);
10089

10190
self::assertStringStartsWith('Page number:', $result);
10291

@@ -118,12 +107,8 @@ public function testGetDescriptionAndTitle(): void
118107

119108
self::assertSame(
120109
['foobar<baz', ''],
121-
$this->callFunction(
122-
$this->browseForeigners,
123-
BrowseForeigners::class,
124-
'getDescriptionAndTitle',
125-
[$desc],
126-
),
110+
(new ReflectionMethod(BrowseForeigners::class, 'getDescriptionAndTitle'))
111+
->invokeArgs($this->browseForeigners, [$desc]),
127112
);
128113

129114
$config = new Config();
@@ -132,12 +117,8 @@ public function testGetDescriptionAndTitle(): void
132117

133118
self::assertSame(
134119
['fooba...', 'foobar<baz'],
135-
$this->callFunction(
136-
$browseForeigners,
137-
BrowseForeigners::class,
138-
'getDescriptionAndTitle',
139-
[$desc],
140-
),
120+
(new ReflectionMethod(BrowseForeigners::class, 'getDescriptionAndTitle'))
121+
->invokeArgs($browseForeigners, [$desc]),
141122
);
142123
}
143124

‎tests/unit/Command/SetVersionCommandTest.php‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPUnit\Framework\Attributes\CoversClass;
1010
use PHPUnit\Framework\Attributes\DataProvider;
1111
use RangeException;
12+
use ReflectionMethod;
1213
use Symfony\Component\Console\Command\Command;
1314

1415
use function class_exists;
@@ -60,12 +61,7 @@ public function testGetGeneratedClassInvalidVersion(string $version): void
6061

6162
$this->expectException(RangeException::class);
6263
$this->expectExceptionMessage('The version number is in the wrong format: ' . $version);
63-
$this->callFunction(
64-
$this->command,
65-
SetVersionCommand::class,
66-
'getGeneratedClass',
67-
[$version],
68-
);
64+
(new ReflectionMethod(SetVersionCommand::class, 'getGeneratedClass'))->invokeArgs($this->command, [$version]);
6965
}
7066

7167
/** @return mixed[][] */
@@ -170,12 +166,8 @@ public function testGetGeneratedClassValidVersion(string $version, string $conte
170166
self::markTestSkipped('The Symfony Console is missing');
171167
}
172168

173-
$output = $this->callFunction(
174-
$this->command,
175-
SetVersionCommand::class,
176-
'getGeneratedClass',
177-
[$version],
178-
);
169+
$output = (new ReflectionMethod(SetVersionCommand::class, 'getGeneratedClass'))
170+
->invokeArgs($this->command, [$version]);
179171
$template = <<<'PHP'
180172
<?php
181173

‎tests/unit/Command/TwigLintCommandTest.php‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpMyAdmin\Config;
99
use PhpMyAdmin\Tests\AbstractTestCase;
1010
use PHPUnit\Framework\Attributes\CoversClass;
11+
use ReflectionMethod;
1112
use Symfony\Component\Console\Command\Command;
1213
use Twig\Error\SyntaxError;
1314

@@ -39,7 +40,7 @@ protected function setUp(): void
3940

4041
public function testGetTemplateContents(): void
4142
{
42-
$contents = $this->callFunction($this->command, TwigLintCommand::class, 'getTemplateContents', [
43+
$contents = (new ReflectionMethod(TwigLintCommand::class, 'getTemplateContents'))->invokeArgs($this->command, [
4344
__DIR__ . '/../_data/file_listing/subfolder/one.ini',
4445
]);
4546

@@ -49,7 +50,7 @@ public function testGetTemplateContents(): void
4950
public function testFindFiles(): void
5051
{
5152
$path = __DIR__ . '/../_data/file_listing';
52-
$filesFound = $this->callFunction($this->command, TwigLintCommand::class, 'findFiles', [$path]);
53+
$filesFound = (new ReflectionMethod(TwigLintCommand::class, 'findFiles'))->invokeArgs($this->command, [$path]);
5354

5455
// Sort results to avoid file system test specific failures
5556
sort($filesFound, SORT_NATURAL);
@@ -65,7 +66,8 @@ public function testFindFiles(): void
6566
public function testGetFilesInfo(): void
6667
{
6768
$path = __DIR__ . '/../_data/file_listing';
68-
$filesInfos = $this->callFunction($this->command, TwigLintCommand::class, 'getFilesInfo', [$path]);
69+
$filesInfos = (new ReflectionMethod(TwigLintCommand::class, 'getFilesInfo'))
70+
->invokeArgs($this->command, [$path]);
6971

7072
// Sort results to avoid file system test specific failures
7173
sort($filesInfos, SORT_REGULAR);
@@ -107,15 +109,18 @@ public function testGetFilesInfoInvalidFile(): void
107109

108110
public function testGetContext(): void
109111
{
110-
$context = $this->callFunction($this->command, TwigLintCommand::class, 'getContext', ['{{ file }', 0]);
112+
$context = (new ReflectionMethod(TwigLintCommand::class, 'getContext'))
113+
->invokeArgs($this->command, ['{{ file }', 0]);
111114

112115
self::assertSame([1 => '{{ file }'], $context);
113116

114-
$context = $this->callFunction($this->command, TwigLintCommand::class, 'getContext', ['{{ file }', 3]);
117+
$context = (new ReflectionMethod(TwigLintCommand::class, 'getContext'))
118+
->invokeArgs($this->command, ['{{ file }', 3]);
115119

116120
self::assertSame([1 => '{{ file }'], $context);
117121

118-
$context = $this->callFunction($this->command, TwigLintCommand::class, 'getContext', ['{{ file }', 5]);
122+
$context = (new ReflectionMethod(TwigLintCommand::class, 'getContext'))
123+
->invokeArgs($this->command, ['{{ file }', 5]);
119124

120125
self::assertSame([], $context);
121126
}

‎tests/unit/Command/WriteGitRevisionCommandTest.php‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpMyAdmin\Command\WriteGitRevisionCommand;
88
use PhpMyAdmin\Tests\AbstractTestCase;
99
use PHPUnit\Framework\Attributes\CoversClass;
10+
use ReflectionMethod;
1011
use Symfony\Component\Console\Command\Command;
1112

1213
use function class_exists;
@@ -59,15 +60,10 @@ public function testGetGeneratedClassValidVersion(): void
5960
],
6061
);
6162

62-
$output = $this->callFunction(
63-
$command,
64-
WriteGitRevisionCommand::class,
65-
'getRevisionInfo',
66-
[
67-
'https://github.com/phpmyadmin/phpmyadmin/commit/%s',
68-
'https://github.com/phpmyadmin/phpmyadmin/tree/%s',
69-
],
70-
);
63+
$output = (new ReflectionMethod(WriteGitRevisionCommand::class, 'getRevisionInfo'))->invokeArgs($command, [
64+
'https://github.com/phpmyadmin/phpmyadmin/commit/%s',
65+
'https://github.com/phpmyadmin/phpmyadmin/tree/%s',
66+
]);
7167
$template = <<<'PHP'
7268
<?php
7369

‎tests/unit/Controllers/Database/StructureControllerTest.php‎

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PHPUnit\Framework\Attributes\CoversClass;
2222
use ReflectionClass;
2323
use ReflectionException;
24+
use ReflectionMethod;
2425

2526
#[CoversClass(StructureController::class)]
2627
class StructureControllerTest extends AbstractTestCase
@@ -338,31 +339,19 @@ public function testGetValuesForMroongaTable(): void
338339

339340
self::assertSame(
340341
[[], '', '', 0],
341-
$this->callFunction(
342-
$structureController,
343-
StructureController::class,
344-
'getValuesForMroongaTable',
345-
[[], 0],
346-
),
342+
(new ReflectionMethod(StructureController::class, 'getValuesForMroongaTable'))
343+
->invokeArgs($structureController, [[], 0]),
347344
);
348345

349346
// Enable stats
350347
$config->settings['ShowStats'] = true;
351-
$this->callFunction(
352-
$structureController,
353-
StructureController::class,
354-
'getDatabaseInfo',
355-
[self::createStub(ServerRequest::class)],
356-
);
348+
(new ReflectionMethod(StructureController::class, 'getDatabaseInfo'))
349+
->invokeArgs($structureController, [self::createStub(ServerRequest::class)]);
357350

358351
self::assertSame(
359352
[['Data_length' => 45, 'Index_length' => 60], '105', 'B', 105],
360-
$this->callFunction(
361-
$structureController,
362-
StructureController::class,
363-
'getValuesForMroongaTable',
364-
[['Data_length' => 45, 'Index_length' => 60], 0],
365-
),
353+
(new ReflectionMethod(StructureController::class, 'getValuesForMroongaTable'))
354+
->invokeArgs($structureController, [['Data_length' => 45, 'Index_length' => 60], 0]),
366355
);
367356

368357
self::assertSame(
@@ -372,12 +361,8 @@ public function testGetValuesForMroongaTable(): void
372361
'B',
373362
180, //105 + 75
374363
],
375-
$this->callFunction(
376-
$structureController,
377-
StructureController::class,
378-
'getValuesForMroongaTable',
379-
[['Data_length' => 45, 'Index_length' => 60], 75],
380-
),
364+
(new ReflectionMethod(StructureController::class, 'getValuesForMroongaTable'))
365+
->invokeArgs($structureController, [['Data_length' => 45, 'Index_length' => 60], 75]),
381366
);
382367
}
383368

0 commit comments

Comments
 (0)