Skip to content

Commit 69ad9bd

Browse files
committed
chore: update ci
1 parent af1b878 commit 69ad9bd

File tree

5 files changed

+25
-20
lines changed

5 files changed

+25
-20
lines changed

.github/workflows/php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ jobs:
2626
# Docs: https://getcomposer.org/doc/articles/scripts.md
2727

2828
- name: Run test suite
29-
run: php example/test_state_mechine.php
29+
run: php example/test_state_mechine.php && php example/stringify.php

Parser.php renamed to Parser/Parser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
namespace Parser;
34

45
class Parser
56
{

ToJson.php renamed to Parser/ToJson.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
22

3+
namespace Parser;
4+
35
class ToJson
46
{
57
/**
68
* @param $obj
79
* @param bool $pretty
810
* @return string|bool|int|float|null
9-
* @throws Exception
11+
* @throws \Exception
1012
*/
1113
public function stringify($obj, bool $pretty = false): string|bool|int|float|null
1214
{
@@ -33,7 +35,7 @@ protected function encodeBasicValue($obj): int|float|string|null
3335
* @param int $indent
3436
* @param bool $pretty
3537
* @return float|int|string
36-
* @throws Exception
38+
* @throws \Exception
3739
*/
3840
protected function encode($obj, int &$indent, bool $pretty = false): string|float|int
3941
{

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
},
1212
"autoload": {
1313
"psr-4": {
14-
"StateMachine\\": "StateMachine"
14+
"StateMachine\\": "StateMachine",
15+
"Parser\\": "Parser"
1516
}
1617
}
1718
}

example/stringify.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
<?php
22

3-
require_once __DIR__ . '/../ToJson.php';
3+
require_once __DIR__ . '/../vendor/autoload.php';
44

5-
try {
6-
$toJSON = new ToJson();
7-
$res = $toJSON->stringify([1, 2, 4.3, 3, ['a' => 'b', 'c' => 1, 'd' => [1, 2, 3]]]);
8-
echo $res, PHP_EOL;
9-
echo PHP_EOL;
10-
var_dump(json_decode($res, true));
11-
echo PHP_EOL;
12-
$res = $toJSON->stringify([
5+
use Parser\ToJson;
6+
7+
$testcases = [
8+
[1, 2, 4.3, 3, ['a' => 'b', 'c' => 1, 'd' => [1, 2, 3]]],
9+
[
1310
'a' => 2.3,
1411
'b' => false,
1512
'd' => true,
@@ -29,10 +26,14 @@
2926
],
3027
]],
3128
'string' => 'sfsadf"sdfsadfsf'
32-
], true);
33-
echo $res, PHP_EOL;
34-
echo PHP_EOL;
35-
var_dump(json_decode($res, true));
36-
} catch (\Exception $ex) {
37-
var_dump($ex->getMessage());
38-
}
29+
],
30+
];
31+
32+
$toJSON = new ToJson();
33+
foreach ($testcases as $testcase) {
34+
$res = $toJSON->stringify($testcase);
35+
assert(json_encode($res) === $res);
36+
$res = $toJSON->stringify($testcase, true);
37+
assert(json_encode($res, JSON_PRETTY_PRINT) === $res);
38+
}
39+

0 commit comments

Comments
 (0)