Skip to content

Commit b8e6ad7

Browse files
committed
Added tests.
1 parent 9faf962 commit b8e6ad7

14 files changed

+984
-318
lines changed

composer.lock

+49-310
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Model/SessionQuery.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public function __construct(
3232
*
3333
* @throws \JsonException if an error occurs while decoding this value
3434
*/
35-
public function decode(): mixed
35+
public function decode(bool $associative = false, int $flags = 0): mixed
3636
{
37-
return \json_decode(json: $this->value, flags: \JSON_THROW_ON_ERROR);
37+
return \json_decode($this->value, $associative, flags: $flags | \JSON_THROW_ON_ERROR);
3838
}
3939
}

src/Twig/FormatExtension.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ final class FormatExtension extends AbstractExtension
3737
'full' => \IntlDateFormatter::FULL,
3838
];
3939

40+
private ?CoreExtension $extension = null;
41+
4042
public function __construct(private readonly TranslatorInterface $translator)
4143
{
4244
}
@@ -120,8 +122,6 @@ private function dateFilter(
120122
* @return string the formatted date
121123
*
122124
* @throws RuntimeError if the date format or the time format is invalid
123-
*
124-
* @psalm-suppress InternalMethod
125125
*/
126126
private function dateTimeFilter(
127127
Environment $env,
@@ -138,12 +138,21 @@ private function dateTimeFilter(
138138
return '';
139139
}
140140

141-
/** @noinspection PhpInternalEntityUsedInspection */
142-
$date = CoreExtension::dateConverter($env, $date);
141+
$extension = $this->getCoreExtension($env);
142+
$date = $extension->convertDate($date);
143143

144144
return FormatUtils::formatDateTime($date, $dateType, $timeType, $pattern);
145145
}
146146

147+
private function getCoreExtension(Environment $env): CoreExtension
148+
{
149+
if (!$this->extension instanceof CoreExtension) {
150+
$this->extension = $env->getExtension(CoreExtension::class);
151+
}
152+
153+
return $this->extension;
154+
}
155+
147156
/**
148157
* Formats a time for the current locale; ignoring the date part.
149158
*

tests/Model/CommandResultTest.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/*
3+
* This file is part of the Calculation package.
4+
*
5+
* (c) bibi.nu <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace App\Tests\Model;
14+
15+
use App\Model\CommandResult;
16+
use PHPUnit\Framework\Attributes\CoversClass;
17+
use PHPUnit\Framework\TestCase;
18+
use Symfony\Component\Console\Command\Command;
19+
20+
#[CoversClass(CommandResult::class)]
21+
class CommandResultTest extends TestCase
22+
{
23+
public function testProperties(): void
24+
{
25+
$actual = new CommandResult(Command::SUCCESS, 'content');
26+
self::assertSame(Command::SUCCESS, $actual->status);
27+
self::assertSame('content', $actual->content);
28+
}
29+
30+
public function testResult(): void
31+
{
32+
$actual = new CommandResult(Command::SUCCESS, 'content');
33+
self::assertTrue($actual->isSuccess());
34+
$actual = new CommandResult(Command::FAILURE, 'content');
35+
self::assertFalse($actual->isSuccess());
36+
}
37+
}

tests/Model/HelpDownloadQueryTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/*
3+
* This file is part of the Calculation package.
4+
*
5+
* (c) bibi.nu <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace App\Tests\Model;
14+
15+
use App\Model\HelpDownloadQuery;
16+
use PHPUnit\Framework\Attributes\CoversClass;
17+
use PHPUnit\Framework\TestCase;
18+
19+
#[CoversClass(HelpDownloadQuery::class)]
20+
class HelpDownloadQueryTest extends TestCase
21+
{
22+
public function testProperties(): void
23+
{
24+
$actual = new HelpDownloadQuery(1, 'location', 'image');
25+
self::assertSame(1, $actual->index);
26+
self::assertSame('location', $actual->location);
27+
self::assertSame('image', $actual->image);
28+
}
29+
}

tests/Model/SessionQueryTest.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/*
3+
* This file is part of the Calculation package.
4+
*
5+
* (c) bibi.nu <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace App\Tests\Model;
14+
15+
use App\Model\SessionQuery;
16+
use PHPUnit\Framework\Attributes\CoversClass;
17+
use PHPUnit\Framework\TestCase;
18+
19+
#[CoversClass(SessionQuery::class)]
20+
class SessionQueryTest extends TestCase
21+
{
22+
/**
23+
* @throws \JsonException
24+
*/
25+
public function testDecodeArray(): void
26+
{
27+
$query = new SessionQuery('name', '{"key": "value"}');
28+
$actual = $query->decode(true);
29+
self::assertIsArray($actual);
30+
self::assertArrayHasKey('key', $actual);
31+
self::assertSame('value', $actual['key']);
32+
}
33+
34+
public function testDecodeInvalid(): void
35+
{
36+
self::expectException(\JsonException::class);
37+
$query = new SessionQuery('name', '{"key": "value"');
38+
$query->decode();
39+
}
40+
41+
/**
42+
* @throws \JsonException
43+
*/
44+
public function testDecodeValid(): void
45+
{
46+
$query = new SessionQuery('name', '{"key": "value"}');
47+
$actual = $query->decode();
48+
self::assertInstanceOf(\stdClass::class, $actual);
49+
self::assertObjectHasProperty('key', $actual);
50+
self::assertSame('value', $actual->key);
51+
}
52+
53+
public function testProperties(): void
54+
{
55+
$actual = new SessionQuery('name', 'value');
56+
self::assertSame('name', $actual->name);
57+
self::assertSame('value', $actual->value);
58+
}
59+
}
+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/*
3+
* This file is part of the Calculation package.
4+
*
5+
* (c) bibi.nu <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace App\Tests\Model;
14+
15+
use App\Model\SwissPostUpdateResult;
16+
use PHPUnit\Framework\Attributes\CoversClass;
17+
use PHPUnit\Framework\TestCase;
18+
19+
#[CoversClass(SwissPostUpdateResult::class)]
20+
class SwissPostUpdateResultTest extends TestCase
21+
{
22+
public function testAdd(): void
23+
{
24+
$result = new SwissPostUpdateResult();
25+
self::assertSame(0, $result->getValidEntriesCount());
26+
self::assertSame(0, $result->getInvalidEntriesCount());
27+
28+
$result->addCity(true);
29+
$result->addState(true);
30+
$result->addStreet(true);
31+
self::assertSame(3, $result->getValidEntriesCount());
32+
self::assertSame(0, $result->getInvalidEntriesCount());
33+
34+
$result->addCity(false);
35+
$result->addState(false);
36+
$result->addStreet(false);
37+
self::assertSame(3, $result->getValidEntriesCount());
38+
self::assertSame(3, $result->getInvalidEntriesCount());
39+
}
40+
41+
public function testEmpty(): void
42+
{
43+
$actual = new SwissPostUpdateResult();
44+
self::assertNull($actual->getError());
45+
46+
self::assertCount(3, $actual->getInvalidEntries());
47+
self::assertSame(0, $actual->getInvalidEntriesCount());
48+
49+
self::assertCount(3, $actual->getOldEntries());
50+
self::assertSame(0, $actual->getOldEntriesCount());
51+
52+
self::assertSame('', $actual->getSourceFile());
53+
self::assertSame('', $actual->getSourceName());
54+
55+
self::assertCount(3, $actual->getValidEntries());
56+
self::assertSame(0, $actual->getValidEntriesCount());
57+
58+
self::assertNull($actual->getValidity());
59+
self::assertFalse($actual->isOverwrite());
60+
self::assertTrue($actual->isValid());
61+
}
62+
63+
public function testError(): void
64+
{
65+
$expected = 'This is an error';
66+
$result = new SwissPostUpdateResult();
67+
$result->setError($expected);
68+
self::assertSame($expected, $result->getError());
69+
self::assertFalse($result->isValid());
70+
}
71+
72+
public function testOldEntries(): void
73+
{
74+
$expected = [
75+
'state' => 1,
76+
'city' => 10,
77+
'street' => 100,
78+
];
79+
$result = new SwissPostUpdateResult();
80+
$result->setOldEntries($expected);
81+
self::assertSame($expected, $result->getOldEntries());
82+
self::assertSame(111, $result->getOldEntriesCount());
83+
}
84+
85+
public function testProperties(): void
86+
{
87+
$result = new SwissPostUpdateResult();
88+
89+
self::assertFalse($result->isOverwrite());
90+
$result->setOverwrite(true);
91+
self::assertTrue($result->isOverwrite());
92+
93+
$expected = 'sourceFile';
94+
$result->setSourceFile($expected);
95+
self::assertSame($expected, $result->getSourceFile());
96+
97+
$expected = 'sourceName';
98+
$result->setSourceName($expected);
99+
self::assertSame($expected, $result->getSourceName());
100+
}
101+
102+
public function testValidity(): void
103+
{
104+
$expected = new \DateTime();
105+
$result = new SwissPostUpdateResult();
106+
$result->setValidity($expected);
107+
self::assertSame($expected->getTimestamp(), $result->getValidity()?->getTimestamp());
108+
}
109+
}

tests/Model/TranslateQueryTest.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/*
3+
* This file is part of the Calculation package.
4+
*
5+
* (c) bibi.nu <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace App\Tests\Model;
14+
15+
use App\Model\TranslateQuery;
16+
use PHPUnit\Framework\Attributes\CoversClass;
17+
use PHPUnit\Framework\TestCase;
18+
19+
#[CoversClass(TranslateQuery::class)]
20+
class TranslateQueryTest extends TestCase
21+
{
22+
public function testWithDefaultValues(): void
23+
{
24+
$actual = new TranslateQuery();
25+
self::assertSame('', $actual->from);
26+
self::assertSame('', $actual->to);
27+
self::assertSame('', $actual->text);
28+
self::assertNull($actual->service);
29+
self::assertFalse($actual->html);
30+
}
31+
32+
public function testWithValues(): void
33+
{
34+
$actual = new TranslateQuery('from', 'to', 'text', 'service', true);
35+
self::assertSame('from', $actual->from);
36+
self::assertSame('to', $actual->to);
37+
self::assertSame('text', $actual->text);
38+
self::assertSame('service', $actual->service);
39+
self::assertTrue($actual->html);
40+
}
41+
}

tests/Pdf/PdfBarScaleTest.php

+48
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,30 @@ public static function getValues(): \Generator
5050
yield [self::createScale(0.0, 9.5, 0), 0.0, 10.0, 10.0];
5151
}
5252

53+
public function testFixBounds(): void
54+
{
55+
$actual = self::createScale(1.0, 1.0);
56+
self::assertEqualsWithDelta(0.985, $actual->getLowerBound(), 0.01);
57+
self::assertEqualsWithDelta(1.015, $actual->getUpperBound(), 0.01);
58+
self::assertSame(0.005, $actual->getTickSpacing());
59+
}
60+
61+
public function testLowerNegative(): void
62+
{
63+
$actual = self::createScale(-1.0, 100.0, 20);
64+
self::assertSame(-10.0, $actual->getLowerBound());
65+
self::assertSame(110.0, $actual->getUpperBound());
66+
self::assertSame(10.0, $actual->getTickSpacing());
67+
}
68+
69+
public function testProperties(): void
70+
{
71+
$actual = self::createScale(0.0, 100.0, 20);
72+
self::assertSame(0.0, $actual->getLowerBound());
73+
self::assertSame(110.0, $actual->getUpperBound());
74+
self::assertSame(10.0, $actual->getTickSpacing());
75+
}
76+
5377
#[\PHPUnit\Framework\Attributes\DataProvider('getValues')]
5478
public function testScale(PdfBarScale $scale, float $lowerBound, float $upperBound, float $tickSpacing): void
5579
{
@@ -58,6 +82,30 @@ public function testScale(PdfBarScale $scale, float $lowerBound, float $upperBou
5882
self::assertSame($tickSpacing, $scale->getTickSpacing());
5983
}
6084

85+
public function testUpperLessLower(): void
86+
{
87+
$actual = self::createScale(100.0, 1.0, 20);
88+
self::assertSame(0.0, $actual->getLowerBound());
89+
self::assertSame(110.0, $actual->getUpperBound());
90+
self::assertSame(10.0, $actual->getTickSpacing());
91+
}
92+
93+
public function testUpperNegative(): void
94+
{
95+
$actual = self::createScale(-100.0, -10.0);
96+
self::assertSame(-110.0, $actual->getLowerBound());
97+
self::assertSame(0.0, $actual->getUpperBound());
98+
self::assertSame(10.0, $actual->getTickSpacing());
99+
}
100+
101+
public function testUpperZero(): void
102+
{
103+
$actual = self::createScale(-100.0, 0.0);
104+
self::assertSame(-120.0, $actual->getLowerBound());
105+
self::assertSame(0.0, $actual->getUpperBound());
106+
self::assertSame(20.0, $actual->getTickSpacing());
107+
}
108+
61109
private static function createScale(float $lowerBound, float $upperBound, int $maxTicks = 10): PdfBarScale
62110
{
63111
return new PdfBarScale($lowerBound, $upperBound, $maxTicks);

0 commit comments

Comments
 (0)