Skip to content

Commit 2fdd6e5

Browse files
committed
Change how to stringify booleans
In PHP and most other languages, booleans are represented as "true" or "false" in lowercase. It's best to keep the same standard in this library for familiarity. Signed-off-by: Henrique Moody <[email protected]>
1 parent 637556b commit 2fdd6e5

File tree

5 files changed

+23
-43
lines changed

5 files changed

+23
-43
lines changed

src/Stringifiers/BoolStringifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ public function stringify(mixed $raw, int $depth): ?string
2828
return null;
2929
}
3030

31-
return $this->quoter->quote($raw ? 'TRUE' : 'FALSE', $depth);
31+
return $this->quoter->quote($raw ? 'true' : 'false', $depth);
3232
}
3333
}

tests/integration/stringify-array.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ output([
2525
]);
2626
?>
2727
--EXPECT--
28-
`[1, NULL, [1.0, [resource <stream>, ..., []]], FALSE, [object] (stdClass: []), ...]`
28+
`[1, NULL, [1.0, [resource <stream>, ..., []]], false, [object] (stdClass: []), ...]`

tests/integration/stringify-bool.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ outputMultiple(
1111
);
1212
?>
1313
--EXPECT--
14-
`TRUE`
15-
`FALSE`
14+
`true`
15+
`false`

tests/integration/stringify-object.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ require 'vendor/autoload.php';
88
output(new Respect\Stringifier\Test\MyObject());
99
?>
1010
--EXPECT--
11-
`[object] (Respect\Stringifier\Test\MyObject: ["foo": TRUE])`
11+
`[object] (Respect\Stringifier\Test\MyObject: ["foo": true])`

tests/unit/Stringifiers/BoolStringifierTest.php

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,65 +13,45 @@
1313
use PHPUnit\Framework\Attributes\CoversClass;
1414
use PHPUnit\Framework\Attributes\Test;
1515
use PHPUnit\Framework\TestCase;
16-
use Respect\Stringifier\Quoter;
1716
use Respect\Stringifier\Stringifiers\BoolStringifier;
17+
use Respect\Stringifier\Test\Double\FakeQuoter;
1818

1919
#[CoversClass(BoolStringifier::class)]
2020
final class BoolStringifierTest extends TestCase
2121
{
22+
private const DEPTH = 0;
23+
2224
#[Test]
23-
public function shouldNotConvertToStringWhenRawValueIsNotBoolean(): void
25+
public function itShouldNotStringifyWhenRawValueIsNotBoolean(): void
2426
{
25-
$raw = 1;
26-
$depth = 0;
27-
28-
$quoterMock = $this->createMock(Quoter::class);
29-
$quoterMock
30-
->expects($this->never())
31-
->method('quote');
32-
33-
$boolStringifier = new BoolStringifier($quoterMock);
27+
$sut = new BoolStringifier(new FakeQuoter());
3428

35-
self::assertNull($boolStringifier->stringify($raw, $depth));
29+
self::assertNull($sut->stringify(1, self::DEPTH));
3630
}
3731

3832
#[Test]
39-
public function shouldConvertToStringWhenRawValueIsTrue(): void
33+
public function itShouldStringifyRawValueWhenItIsTrue(): void
4034
{
41-
$raw = true;
42-
$depth = 0;
35+
$quoter = new FakeQuoter();
4336

44-
$expected = 'TRUE';
37+
$sut = new BoolStringifier($quoter);
4538

46-
$quoterMock = $this->createMock(Quoter::class);
47-
$quoterMock
48-
->expects($this->once())
49-
->method('quote')
50-
->with($expected, $depth)
51-
->willReturn($expected);
39+
$actual = $sut->stringify(true, self::DEPTH);
40+
$expected = $quoter->quote('true', self::DEPTH);
5241

53-
$boolStringifier = new BoolStringifier($quoterMock);
54-
55-
self::assertSame($expected, $boolStringifier->stringify($raw, $depth));
42+
self::assertSame($expected, $actual);
5643
}
5744

5845
#[Test]
59-
public function shouldConvertToStringWhenRawValueIsFalse(): void
46+
public function itShouldStringifyRawValueWhenItIsFalse(): void
6047
{
61-
$raw = false;
62-
$depth = 0;
63-
64-
$expected = 'FALSE';
48+
$quoter = new FakeQuoter();
6549

66-
$quoterMock = $this->createMock(Quoter::class);
67-
$quoterMock
68-
->expects($this->once())
69-
->method('quote')
70-
->with($expected, $depth)
71-
->willReturn($expected);
50+
$sut = new BoolStringifier($quoter);
7251

73-
$boolStringifier = new BoolStringifier($quoterMock);
52+
$actual = $sut->stringify(false, self::DEPTH);
53+
$expected = $quoter->quote('false', self::DEPTH);
7454

75-
self::assertSame($expected, $boolStringifier->stringify($raw, $depth));
55+
self::assertSame($expected, $actual);
7656
}
7757
}

0 commit comments

Comments
 (0)