Skip to content

Commit 3393615

Browse files
committed
Create BoolStringifier
1 parent 9e18cf0 commit 3393615

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Respect/Stringifier.
5+
*
6+
* (c) Henrique Moody <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the "LICENSE.md"
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Respect\Stringifier\Stringifiers;
15+
16+
use function is_bool;
17+
use Respect\Stringifier\Quoter;
18+
use Respect\Stringifier\Stringifier;
19+
20+
/**
21+
* Converts a boolean value into a string.
22+
*
23+
* @author Henrique Moody <[email protected]>
24+
*/
25+
final class BoolStringifier implements Stringifier
26+
{
27+
/**
28+
* @var Quoter
29+
*/
30+
private $quoter;
31+
32+
/**
33+
* Initializes the stringifier.
34+
*
35+
* @param Quoter $quoter
36+
*/
37+
public function __construct(Quoter $quoter)
38+
{
39+
$this->quoter = $quoter;
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function stringify($raw, int $depth): ?string
46+
{
47+
if (!is_bool($raw)) {
48+
return null;
49+
}
50+
51+
return $this->quoter->quote($raw ? 'TRUE' : 'FALSE', $depth);
52+
}
53+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Respect/Stringifier.
5+
*
6+
* (c) Henrique Moody <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the "LICENSE.md"
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Respect\Stringifier\Test\Stringifiers;
15+
16+
use Respect\Stringifier\Quoter;
17+
use Respect\Stringifier\Stringifiers\BoolStringifier;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* @covers \Respect\Stringifier\Stringifiers\BoolStringifier
22+
*
23+
* @author Henrique Moody <[email protected]>
24+
*/
25+
final class BoolStringifierTest extends TestCase
26+
{
27+
/**
28+
* @test
29+
*/
30+
public function shouldNotConvertToStringWhenRawValueIsNotBoolean(): void
31+
{
32+
$raw = 1;
33+
$depth = 0;
34+
35+
$quoterMock = $this->createMock(Quoter::class);
36+
$quoterMock
37+
->expects($this->never())
38+
->method('quote');
39+
40+
$boolStringifier = new BoolStringifier($quoterMock);
41+
42+
self::assertNull($boolStringifier->stringify($raw, $depth));
43+
}
44+
45+
/**
46+
* @test
47+
*/
48+
public function shouldConvertToStringWhenRawValueIsTrue(): void
49+
{
50+
$raw = true;
51+
$depth = 0;
52+
53+
$expected = 'TRUE';
54+
55+
$quoterMock = $this->createMock(Quoter::class);
56+
$quoterMock
57+
->expects($this->once())
58+
->method('quote')
59+
->with($expected, $depth)
60+
->willReturn($expected);
61+
62+
$boolStringifier = new BoolStringifier($quoterMock);
63+
64+
self::assertSame($expected, $boolStringifier->stringify($raw, $depth));
65+
}
66+
67+
/**
68+
* @test
69+
*/
70+
public function shouldConvertToStringWhenRawValueIsFalse(): void
71+
{
72+
$raw = false;
73+
$depth = 0;
74+
75+
$expected = 'FALSE';
76+
77+
$quoterMock = $this->createMock(Quoter::class);
78+
$quoterMock
79+
->expects($this->once())
80+
->method('quote')
81+
->with($expected, $depth)
82+
->willReturn($expected);
83+
84+
$boolStringifier = new BoolStringifier($quoterMock);
85+
86+
self::assertSame($expected, $boolStringifier->stringify($raw, $depth));
87+
}
88+
}

0 commit comments

Comments
 (0)