Skip to content

Commit 85b07dc

Browse files
Extract class
1 parent a244f58 commit 85b07dc

File tree

4 files changed

+103
-80
lines changed

4 files changed

+103
-80
lines changed

src/Framework/MockObject/Generator/Generator.php

Lines changed: 7 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,10 @@ private function generateCodeForTestDoubleClass(string $type, bool $mockObject,
891891
'use_statements' => $useStatements,
892892
'mock_class_name' => $_mockClassName['className'],
893893
'methods' => $mockedMethods,
894-
'property_hooks' => $this->codeForPropertyHooks($propertiesWithHooks, $_mockClassName['className']),
894+
'property_hooks' => (new HookedPropertyGenerator)->generate(
895+
$_mockClassName['className'],
896+
$propertiesWithHooks,
897+
),
895898
],
896899
);
897900

@@ -1162,7 +1165,7 @@ private function interfaceMethods(string $interfaceName, bool $cloneArguments):
11621165
}
11631166

11641167
/**
1165-
* @param list<Property> $propertiesWithHooks
1168+
* @param list<HookedProperty> $propertiesWithHooks
11661169
*
11671170
* @return list<ConfigurableMethod>
11681171
*/
@@ -1211,7 +1214,7 @@ private function configurableMethods(MockMethodSet $methods, array $propertiesWi
12111214
/**
12121215
* @param ?ReflectionClass<object> $class
12131216
*
1214-
* @return list<Property>
1217+
* @return list<HookedProperty>
12151218
*/
12161219
private function properties(?ReflectionClass $class): array
12171220
{
@@ -1264,7 +1267,7 @@ private function properties(?ReflectionClass $class): array
12641267
continue;
12651268
}
12661269

1267-
$properties[] = new Property(
1270+
$properties[] = new HookedProperty(
12681271
$property->getName(),
12691272
$mapper->fromPropertyType($property),
12701273
$hasGetHook,
@@ -1274,72 +1277,4 @@ private function properties(?ReflectionClass $class): array
12741277

12751278
return $properties;
12761279
}
1277-
1278-
/**
1279-
* @param list<Property> $propertiesWithHooks
1280-
* @param class-string $className
1281-
*
1282-
* @return non-empty-string
1283-
*/
1284-
private function codeForPropertyHooks(array $propertiesWithHooks, string $className): string
1285-
{
1286-
$propertyHooks = '';
1287-
1288-
foreach ($propertiesWithHooks as $property) {
1289-
$propertyHooks .= sprintf(
1290-
<<<'EOT'
1291-
1292-
public %s $%s {
1293-
EOT,
1294-
$property->type()->asString(),
1295-
$property->name(),
1296-
);
1297-
1298-
if ($property->hasGetHook()) {
1299-
$propertyHooks .= sprintf(
1300-
<<<'EOT'
1301-
1302-
get {
1303-
return $this->__phpunit_getInvocationHandler()->invoke(
1304-
new \PHPUnit\Framework\MockObject\Invocation(
1305-
'%s', '$%s::get', [], '%s', $this, false
1306-
)
1307-
);
1308-
}
1309-
1310-
EOT,
1311-
$className,
1312-
$property->name(),
1313-
$property->type()->asString(),
1314-
);
1315-
}
1316-
1317-
if ($property->hasSetHook()) {
1318-
$propertyHooks .= sprintf(
1319-
<<<'EOT'
1320-
1321-
set (%s $value) {
1322-
$this->__phpunit_getInvocationHandler()->invoke(
1323-
new \PHPUnit\Framework\MockObject\Invocation(
1324-
'%s', '$%s::set', [$value], 'void', $this, false
1325-
)
1326-
);
1327-
}
1328-
1329-
EOT,
1330-
$property->type()->asString(),
1331-
$className,
1332-
$property->name(),
1333-
);
1334-
}
1335-
1336-
$propertyHooks .= <<<'EOT'
1337-
}
1338-
1339-
EOT;
1340-
1341-
}
1342-
1343-
return $propertyHooks;
1344-
}
13451280
}

src/Framework/MockObject/Generator/Property.php renamed to src/Framework/MockObject/Generator/HookedProperty.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @internal This class is not covered by the backward compatibility promise for PHPUnit
1818
*/
19-
final readonly class Property
19+
final readonly class HookedProperty
2020
{
2121
/**
2222
* @var non-empty-string
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Framework\MockObject\Generator;
11+
12+
use function sprintf;
13+
14+
/**
15+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
16+
*
17+
* @internal This class is not covered by the backward compatibility promise for PHPUnit
18+
*/
19+
final class HookedPropertyGenerator
20+
{
21+
/**
22+
* @param class-string $className
23+
* @param list<HookedProperty> $properties
24+
*
25+
* @return non-empty-string
26+
*/
27+
public function generate(string $className, array $properties): string
28+
{
29+
$code = '';
30+
31+
foreach ($properties as $property) {
32+
$code .= sprintf(
33+
<<<'EOT'
34+
35+
public %s $%s {
36+
EOT,
37+
$property->type()->asString(),
38+
$property->name(),
39+
);
40+
41+
if ($property->hasGetHook()) {
42+
$code .= sprintf(
43+
<<<'EOT'
44+
45+
get {
46+
return $this->__phpunit_getInvocationHandler()->invoke(
47+
new \PHPUnit\Framework\MockObject\Invocation(
48+
'%s', '$%s::get', [], '%s', $this, false
49+
)
50+
);
51+
}
52+
53+
EOT,
54+
$className,
55+
$property->name(),
56+
$property->type()->asString(),
57+
);
58+
}
59+
60+
if ($property->hasSetHook()) {
61+
$code .= sprintf(
62+
<<<'EOT'
63+
64+
set (%s $value) {
65+
$this->__phpunit_getInvocationHandler()->invoke(
66+
new \PHPUnit\Framework\MockObject\Invocation(
67+
'%s', '$%s::set', [$value], 'void', $this, false
68+
)
69+
);
70+
}
71+
72+
EOT,
73+
$property->type()->asString(),
74+
$className,
75+
$property->name(),
76+
);
77+
}
78+
79+
$code .= <<<'EOT'
80+
}
81+
82+
EOT;
83+
84+
}
85+
86+
return $code;
87+
}
88+
}

tests/unit/Framework/MockObject/Generator/PropertyTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use PHPUnit\Framework\TestCase;
1616
use SebastianBergmann\Type\Type;
1717

18-
#[CoversClass(Property::class)]
18+
#[CoversClass(HookedProperty::class)]
1919
#[Group('test-doubles')]
2020
#[Small]
2121
final class PropertyTest extends TestCase
@@ -24,7 +24,7 @@ public function testHasName(): void
2424
{
2525
$name = 'property-name';
2626

27-
$property = new Property($name, Type::fromName('string', false), false, false);
27+
$property = new HookedProperty($name, Type::fromName('string', false), false, false);
2828

2929
$this->assertSame($name, $property->name());
3030
}
@@ -33,35 +33,35 @@ public function testHasType(): void
3333
{
3434
$type = Type::fromName('string', false);
3535

36-
$property = new Property('property-name', $type, false, false);
36+
$property = new HookedProperty('property-name', $type, false, false);
3737

3838
$this->assertSame($type, $property->type());
3939
}
4040

4141
public function testMayHaveGetHook(): void
4242
{
43-
$property = new Property('property-name', Type::fromName('string', false), true, false);
43+
$property = new HookedProperty('property-name', Type::fromName('string', false), true, false);
4444

4545
$this->assertTrue($property->hasGetHook());
4646
}
4747

4848
public function testMayNotHaveGetHook(): void
4949
{
50-
$property = new Property('property-name', Type::fromName('string', false), false, false);
50+
$property = new HookedProperty('property-name', Type::fromName('string', false), false, false);
5151

5252
$this->assertFalse($property->hasGetHook());
5353
}
5454

5555
public function testMayHaveSetHook(): void
5656
{
57-
$property = new Property('property-name', Type::fromName('string', false), false, true);
57+
$property = new HookedProperty('property-name', Type::fromName('string', false), false, true);
5858

5959
$this->assertTrue($property->hasSetHook());
6060
}
6161

6262
public function testMayNotHaveSetHook(): void
6363
{
64-
$property = new Property('property-name', Type::fromName('string', false), false, false);
64+
$property = new HookedProperty('property-name', Type::fromName('string', false), false, false);
6565

6666
$this->assertFalse($property->hasSetHook());
6767
}

0 commit comments

Comments
 (0)