summaryrefslogtreecommitdiff
path: root/tests/RegionManagerTest.php
blob: bf2a5bee51d4e9beb011cf010415cd7e32a49f35 (plain)
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
<?php

declare(strict_types=1);

namespace Zhineng\RegionChina\Tests;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Zhineng\RegionChina\RegionException;
use Zhineng\RegionChina\RegionManager;

#[CoversClass(RegionManager::class)]
final class RegionManagerTest extends TestCase
{
    public function test_name_resolution(): void
    {
        $manager = new RegionManager(['_110000' => '北京市'], [], []);
        $this->assertSame('北京市', $manager->getName(110000));
    }

    public function test_name_resolution_with_non_existent_code(): void
    {
        $this->expectException(RegionException::class);
        $this->expectExceptionMessage('The region code does not exist.');
        $manager = new RegionManager([], [], []);
        $manager->getName(110000);
    }

    public function test_top_level_nodes_resolution(): void
    {
        $manager = new RegionManager([], [], [1, 2, 3]);
        $this->assertSame([1, 2, 3], $manager->getTopLevelNodes());
    }

    public function test_nodes_resolution(): void
    {
        $manager = new RegionManager([], ['_1' => [2]], []);
        $this->assertSame([2], $manager->getNodes(1));
    }

    public function test_nodes_resolution_with_non_existent_code(): void
    {
        $this->expectException(RegionException::class);
        $this->expectExceptionMessage('Could not find any nodes under the region code.');
        $manager = new RegionManager([], [], []);
        $manager->getNodes(1);
    }
}