Failed Conditions
Pull Request — master (#4279)
by Owen
11:57
created

StringHelperLocaleTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Shared;
6
7
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
8
use PHPUnit\Framework\Attributes;
9
use PHPUnit\Framework\TestCase;
10
11
// separate processes due to setLocale
12
#[Attributes\RunTestsInSeparateProcesses]
13
class StringHelperLocaleTest extends TestCase
14
{
15
    /**
16
     * @var false|string
17
     */
18
    private $currentLocale;
19
20
    protected function setUp(): void
21
    {
22
        $this->currentLocale = setlocale(LC_ALL, '0');
23
    }
24
25
    protected function tearDown(): void
26
    {
27
        if (is_string($this->currentLocale)) {
28
            setlocale(LC_ALL, $this->currentLocale);
29
        }
30
        StringHelper::setCurrencyCode(null);
31
    }
32
33
    public function testCurrency(): void
34
    {
35
        if (!setlocale(LC_ALL, 'de_DE.UTF-8', 'deu_deu.utf8')) {
36
            self::markTestSkipped('Unable to set German UTF8 locale for testing.');
37
        }
38
        $result = StringHelper::getCurrencyCode();
39
        self::assertSame('€', $result);
40
        if (!setlocale(LC_ALL, 'en_us')) {
41
            self::markTestSkipped('Unable to set US locale for testing.');
42
        }
43
        $result = StringHelper::getCurrencyCode();
44
        self::assertSame('€', $result, 'result persists despite locale change');
45
        StringHelper::setCurrencyCode(null);
46
        $result = StringHelper::getCurrencyCode();
47
        self::assertSame('$', $result, 'locale now used');
48
        StringHelper::setCurrencyCode(null);
49
        if (!setlocale(LC_ALL, 'deu_deu', 'de_DE')) {
50
            self::markTestSkipped('Unable to set German single-byte locale for testing.');
51
        }
52
        // Seems like Linux returns trailing blank, Win doesn't
53
        $result = StringHelper::getCurrencyCode(true); // trim if alt symbol is used
54
        self::assertSame('EUR', $result, 'non-UTF8 result ignored');
55
    }
56
}
57