Skip to content
This repository was archived by the owner on Sep 10, 2023. It is now read-only.

Commit c46766d

Browse files
committed
Added missing unit tests
1 parent ddad062 commit c46766d

3 files changed

Lines changed: 115 additions & 17 deletions

File tree

src/Assert/AbstractAssert.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ protected static function valueToString($value): string
5656

5757
if (is_object($value)) {
5858
if (method_exists($value, '__toString')) {
59-
return get_class($value) . ': ' . self::valueToString($value->__toString());
59+
$format = '%1$s: %2$s';
60+
return sprintf($format, get_class($value), self::valueToString($value->__toString()));
6061
}
61-
6262
return get_class($value);
6363
}
6464

@@ -67,23 +67,10 @@ protected static function valueToString($value): string
6767
}
6868

6969
if (is_string($value)) {
70-
return sprintf('"%1$s"', $value);
70+
$format = '"%1$s"';
71+
return sprintf($format, $value);
7172
}
7273

7374
return (string) $value;
7475
}
75-
76-
/**
77-
* Convert value to class name or type
78-
*
79-
* @param mixed $value
80-
*
81-
* @return string
82-
*/
83-
protected static function typeToString($value): string
84-
{
85-
$ret = is_object($value) ? get_class($value) : gettype($value);
86-
87-
return (string) $ret;
88-
}
8976
}

test/Assert/AssertTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
namespace TxTextControlTest\ReportingCloud\Assert;
1616

1717
use PHPUnit\Framework\TestCase;
18+
use stdClass;
19+
use TxTextControlTest\ReportingCloud\Assert\TestAsset\ConcreteAssert as Assert;
1820

1921
/**
2022
* Class AssertTest
@@ -47,4 +49,75 @@ class AssertTest extends TestCase
4749
use AssertStringTraitTest;
4850
use AssertIntegerTraitTest;
4951
use AssertBooleanTraitTest;
52+
53+
public function testValueToStringNull(): void
54+
{
55+
$expected = 'null';
56+
$actual = Assert::publicValueToString(null);
57+
$this->assertEquals($expected, $actual);
58+
}
59+
60+
public function testValueToStringTrue(): void
61+
{
62+
$expected = 'true';
63+
$actual = Assert::publicValueToString(true);
64+
$this->assertEquals($expected, $actual);
65+
}
66+
67+
public function testValueToStringFalse(): void
68+
{
69+
$expected = 'false';
70+
$actual = Assert::publicValueToString(false);
71+
$this->assertEquals($expected, $actual);
72+
}
73+
74+
public function testValueToStringArray(): void
75+
{
76+
$expected = 'array';
77+
$actual = Assert::publicValueToString([1, 2, 3]);
78+
$this->assertEquals($expected, $actual);
79+
}
80+
81+
public function testValueToStringObject(): void
82+
{
83+
$stdClass = new stdClass();
84+
85+
$expected = get_class($stdClass);
86+
$actual = Assert::publicValueToString($stdClass);
87+
88+
$this->assertEquals($expected, $actual);
89+
}
90+
91+
public function testValueToStringObjectWithToString(): void
92+
{
93+
$stdClass = new class extends stdClass
94+
{
95+
public function __toString()
96+
{
97+
return 'abc';
98+
}
99+
};
100+
101+
$expected = sprintf('%1$s: %2$s', get_class($stdClass), Assert::publicValueToString($stdClass->__toString()));
102+
$actual = Assert::publicValueToString($stdClass);
103+
104+
$this->assertEquals($expected, $actual);
105+
}
106+
107+
public function testValueToStringResource(): void
108+
{
109+
$handle = fopen(__FILE__, 'r');
110+
111+
$expected = 'resource';
112+
$actual = Assert::publicValueToString($handle);
113+
114+
$this->assertEquals($expected, $actual);
115+
}
116+
117+
public function testValueToStringString(): void
118+
{
119+
$expected = '"abc"';
120+
$actual = Assert::publicValueToString('abc');
121+
$this->assertEquals($expected, $actual);
122+
}
50123
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* ReportingCloud PHP SDK
6+
*
7+
* PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH.
8+
*
9+
* @link https://www.reporting.cloud to learn more about ReportingCloud
10+
* @link https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
11+
* @license https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
12+
* @copyright © 2019 Text Control GmbH
13+
*/
14+
15+
namespace TxTextControlTest\ReportingCloud\Assert\TestAsset;
16+
17+
use TxTextControl\ReportingCloud\Assert\AbstractAssert;
18+
19+
/**
20+
* Class ConcreteAssert (for testing only)
21+
*
22+
* @package TxTextControlTest\ReportingCloud
23+
* @author Jonathan Maron (@JonathanMaron)
24+
*/
25+
class ConcreteAssert extends AbstractAssert
26+
{
27+
/**
28+
* Convert value to string (public version for testing)
29+
*
30+
* @param mixed $value
31+
*
32+
* @return string
33+
*/
34+
public static function publicValueToString($value): string
35+
{
36+
return self::valueToString($value);
37+
}
38+
}

0 commit comments

Comments
 (0)