Skip to content

Commit cdeee29

Browse files
committed
MockDelegateFunctionBuilder reuses classes if the signature would match. This allows running tests with @backupStaticAttributes enabled
1 parent 2457668 commit cdeee29

File tree

2 files changed

+63
-16
lines changed

2 files changed

+63
-16
lines changed

classes/MockDelegateFunctionBuilder.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ class MockDelegateFunctionBuilder
2020
*/
2121
const METHOD = "delegate";
2222

23-
/**
24-
* @var int The instance counter.
25-
*/
26-
private static $counter = 0;
27-
2823
/**
2924
* @var string The namespace of the build class.
3025
*/
@@ -52,16 +47,23 @@ public function __construct()
5247
*/
5348
public function build($functionName = null)
5449
{
55-
self::$counter++;
56-
57-
$this->namespace = __NAMESPACE__ . self::$counter;
58-
5950
$parameterBuilder = new ParameterBuilder();
6051
$parameterBuilder->build($functionName);
52+
$signatureParameters = $parameterBuilder->getSignatureParameters();
53+
54+
/**
55+
* If a class with the same signature exists, it is considered equivalent
56+
* to the generated class.
57+
*/
58+
$hash = md5($signatureParameters);
59+
$this->namespace = __NAMESPACE__.$hash;
60+
if (class_exists($this->getFullyQualifiedClassName())) {
61+
return;
62+
}
6163

6264
$data = [
63-
"namespace" => $this->namespace,
64-
"signatureParameters" => $parameterBuilder->getSignatureParameters(),
65+
"namespace" => $this->namespace,
66+
"signatureParameters" => $signatureParameters,
6567
];
6668
$this->template->setVar($data, false);
6769
$definition = $this->template->render();

tests/MockDelegateFunctionBuilderTest.php

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,71 @@ public function testBuild()
2626
}
2727

2828
/**
29-
* Test build() would never create the same class name.
29+
* Test build() would never create the same class name for different signatures.
3030
*
3131
* @test
3232
*/
33-
public function testSubsequentCallsProduceDifferentClasses()
33+
public function testDiverseSignaturesProduceDifferentClasses()
3434
{
3535
$builder = new MockDelegateFunctionBuilder();
3636

37-
$builder->build();
37+
$builder->build(create_function('', ''));
3838
$class1 = $builder->getFullyQualifiedClassName();
3939

40-
$builder->build();
40+
$builder->build(create_function('$a', ''));
4141
$class2 = $builder->getFullyQualifiedClassName();
4242

4343
$builder2 = new MockDelegateFunctionBuilder();
44-
$builder2->build();
44+
$builder2->build(create_function('$a, $b', ''));
4545
$class3 = $builder2->getFullyQualifiedClassName();
4646

4747
$this->assertNotEquals($class1, $class2);
4848
$this->assertNotEquals($class1, $class3);
4949
$this->assertNotEquals($class2, $class3);
5050
}
51+
52+
/**
53+
* Test build() would create the same class name for identical signatures.
54+
*
55+
* @test
56+
*/
57+
public function testSameSignaturesProduceSameClass()
58+
{
59+
$signature = '$a';
60+
$builder = new MockDelegateFunctionBuilder();
61+
62+
$builder->build(create_function($signature, ''));
63+
$class1 = $builder->getFullyQualifiedClassName();
64+
65+
$builder->build(create_function($signature, ''));
66+
$class2 = $builder->getFullyQualifiedClassName();
67+
68+
$this->assertEquals($class1, $class2);
69+
}
70+
71+
/**
72+
* Tests declaring a class with enabled backupStaticAttributes.
73+
*
74+
* @test
75+
* @backupStaticAttributes enabled
76+
* @dataProvider provideTestBackupStaticAttributes
77+
*/
78+
public function testBackupStaticAttributes()
79+
{
80+
$builder = new MockDelegateFunctionBuilder();
81+
$builder->build("min");
82+
}
83+
84+
/**
85+
* Just repeat testBackupStaticAttributes a few times.
86+
*
87+
* @return array Test cases.
88+
*/
89+
public function provideTestBackupStaticAttributes()
90+
{
91+
return [
92+
[],
93+
[]
94+
];
95+
}
5196
}

0 commit comments

Comments
 (0)