Skip to content
This repository was archived by the owner on Feb 21, 2025. It is now read-only.

Commit da9e571

Browse files
ivyhjkusox
authored andcommitted
3 default boolean value (#4)
* Fix default boolean values (#3) * Clarifying change (#3)
1 parent 70f88c1 commit da9e571

3 files changed

Lines changed: 95 additions & 1 deletion

File tree

src/Mock.hh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,17 @@ final class Mock<TC> implements MockInterface {
6161

6262
if ($parameter->isDefaultValueAvailable() === true) {
6363
if ($parameter->isDefaultValueConstant()) {
64+
/**
65+
* HHVM seems to treat default values for bool
66+
* parameters like constants, php does not.
67+
* @see https://github.com/usox/hackmock/issues/3.
68+
* @see https://github.com/usox/hackmock/pull/4.
69+
*/
6470
$gen_method->addParameterf(
6571
'%s $%s = %s',
6672
(string) $parameter->getType(),
6773
$parameter->getName(),
68-
$parameter->getDefaultValue() === null ? 'null' : $parameter->getDefaultValue()
74+
$parameter->getDefaultValueText()
6975
);
7076
} else {
7177
if (Str\contains((string) $parameter->getType(), 'string')) {

tests/DefaultValueTest.hh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?hh // strict
2+
3+
use function Usox\HackMock\{mock, prospect};
4+
5+
class DefaultValueTest extends \Usox\HackMock\HackMock {
6+
public function testDefaultBooleanFalse(): void {
7+
$mock = mock(Stub\DefaultValueClass::class);
8+
9+
prospect($mock, 'defaultBooleanFalse')->execute(vec[]);
10+
}
11+
12+
public function testDefaultBooleanTrue(): void {
13+
$mock = mock(Stub\DefaultValueClass::class);
14+
15+
prospect($mock, 'defaultBooleanTrue')->execute(vec[]);
16+
}
17+
18+
public function testDefaultNullString(): void {
19+
$mock = mock(Stub\DefaultValueClass::class);
20+
21+
prospect($mock, 'defaultNullString')->execute(vec[]);
22+
}
23+
24+
public function testDefaultNullArray(): void {
25+
$mock = mock(Stub\DefaultValueClass::class);
26+
27+
prospect($mock, 'defaultNullArray')->execute(vec[]);
28+
}
29+
30+
public function testDefaultConstantInt(): void {
31+
$mock = mock(Stub\DefaultValueClass::class);
32+
33+
prospect($mock, 'defaultConstantInt')->execute(vec[]);
34+
}
35+
36+
public function testDefaultConstantString(): void {
37+
$mock = mock(Stub\DefaultValueClass::class);
38+
39+
prospect($mock, 'defaultConstantString')->execute(vec[]);
40+
}
41+
42+
public function testDefaultConstantArraykey(): void {
43+
$mock = mock(Stub\DefaultValueClass::class);
44+
45+
prospect($mock, 'defaultConstantArraykey')->execute(vec[]);
46+
}
47+
}

tests/Stub/DefaultValueClass.hh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?hh // strict
2+
3+
namespace Stub;
4+
5+
/**
6+
* Version 1.0.1 just fails with boolean values (#3).
7+
* TODO: add more tests cases?.
8+
*/
9+
class DefaultValueClass {
10+
const int FOO = 0;
11+
const string BAR = 'bar';
12+
const arraykey BAZ = 1;
13+
14+
public function defaultBooleanFalse(bool $param = false): void {
15+
16+
}
17+
18+
public function defaultBooleanTrue(bool $param = true): void {
19+
20+
}
21+
22+
public function defaultNullString(?string $param = null): void {
23+
24+
}
25+
26+
public function defaultNullArray(?array<arraykey, mixed> $param = null): void {
27+
28+
}
29+
30+
public function defaultConstantInt(int $param = self::FOO): void {
31+
32+
}
33+
34+
public function defaultConstantString(string $param = self::BAR): void {
35+
36+
}
37+
38+
public function defaultConstantArraykey(arraykey $param = self::BAZ): void {
39+
40+
}
41+
}

0 commit comments

Comments
 (0)