-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathElasticSearchToCentsTest.php
More file actions
52 lines (43 loc) · 1.39 KB
/
ElasticSearchToCentsTest.php
File metadata and controls
52 lines (43 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare(strict_types=1);
namespace Drupal\Tests\asu_rest\Unit;
use Drupal\asu_rest\Plugin\rest\resource\ElasticSearch;
use Drupal\Tests\UnitTestCase;
/**
* Tests cents conversion used by the /elasticsearch resource.
*
* @group asu_rest
*/
final class ElasticSearchToCentsTest extends UnitTestCase {
/**
* Tests that euro decimals are converted to integer cents.
*
* @dataProvider centsProvider
*/
public function testToCents(?string $input, int $expected): void {
$ref = new \ReflectionClass(ElasticSearch::class);
/** @var \Drupal\asu_rest\Plugin\rest\resource\ElasticSearch $resource */
$resource = $ref->newInstanceWithoutConstructor();
$method = new \ReflectionMethod(ElasticSearch::class, 'toCents');
$method->setAccessible(TRUE);
$actual = $method->invoke($resource, $input);
$this->assertSame($expected, $actual);
}
/**
* Data provider for cents conversion.
*
* @return array<string, array{0: ?string, 1: int}>
* Test cases keyed by human-readable description.
*/
public static function centsProvider(): array {
return [
'null becomes zero' => [NULL, 0],
'empty becomes zero' => ['', 0],
'zero becomes zero' => ['0', 0],
'integer euros' => ['123', 12300],
'two decimals' => ['123.45', 12345],
'one decimal rounds' => ['123.4', 12340],
'round half up-ish' => ['0.005', 1],
];
}
}