Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
Merge 3a45fa2 into 0fcb1fe
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Mar 20, 2019
2 parents 0fcb1fe + 3a45fa2 commit f8d77de
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -237,7 +237,7 @@ foreach ($response as $region) {
$region->getCodeExt();
$region->getFiasGuid();
$region->getCountryName();
$region->getCountryCode();
$region->getCountryCodeISO();
$region->getCountryCodeExt();
}
```
Expand Down
2 changes: 1 addition & 1 deletion examples/050_RegionsRequest.php
Expand Up @@ -47,6 +47,6 @@
$region->getCodeExt();
$region->getFiasGuid();
$region->getCountryName();
$region->getCountryCode();
$region->getCountryCodeISO();
$region->getCountryCodeExt();
}
3 changes: 3 additions & 0 deletions src/Common/Location.php
Expand Up @@ -34,6 +34,9 @@ final class Location
{
/**
* @var array<string, int>
*
* @see Region::getCountryCode()
* @see Location::getCountryCode()
*/
const COUNTRY_CODE_LOOKUP = [
'RU' => 1,
Expand Down
21 changes: 19 additions & 2 deletions src/Common/Region.php
Expand Up @@ -90,9 +90,9 @@ final class Region

/**
* @JMS\XmlAttribute
* @JMS\Type("int")
* @JMS\Type("string")
*
* @var int
* @var string
*/
private $countryCode;

Expand Down Expand Up @@ -139,7 +139,24 @@ public function getCountryName(): string
return $this->countryName;
}

/**
* @deprecated СДЭК отказались от числовых кодов на 19.03.2019
* @see Region::getCountryCodeISO()
*/
public function getCountryCode(): int
{
if (is_numeric($this->countryCode)) {
return (int) $this->countryCode;
}

if (array_key_exists($this->countryCode, Location::COUNTRY_CODE_LOOKUP)) {
return Location::COUNTRY_CODE_LOOKUP[$this->countryCode];
}

return 0;
}

public function getCountryCodeISO(): string
{
return $this->countryCode;
}
Expand Down
41 changes: 40 additions & 1 deletion tests/Deserialization/RegionsResponseTest.php
Expand Up @@ -38,7 +38,7 @@
*/
class RegionsResponseTest extends TestCase
{
public function test_it_deserializes()
public function test_it_deserializes_old_style_response()
{
$response = $this->getSerializer()->deserialize(FixtureLoader::load('Regions.xml'), RegionsResponse::class, 'xml');

Expand Down Expand Up @@ -76,6 +76,45 @@ public function test_it_deserializes()
$this->assertSame($region, $item);
}

public function test_it_deserializes()
{
$response = $this->getSerializer()->deserialize(FixtureLoader::load('RegionsISO.xml'), RegionsResponse::class, 'xml');

/** @var $response RegionsResponse */
$this->assertFalse($response->hasErrors());

$this->assertInstanceOf(RegionsResponse::class, $response);

$this->assertNotEmpty($response->getItems());

$this->assertCount(1, $response->getItems());
$this->assertCount(1, $response);

foreach ($response->getItems() as $region) {
$this->assertInstanceOf(Region::class, $region);
}

$region = $response->getItems()[0];
/** @var $region Region */
$this->assertSame('0c4563e7-3465-4325-a6d8-be0e06a1ab48', $region->getUuid(), sprintf('Received unexpected region: %s', var_export($region, true)));
$this->assertSame('Хабаровский', $region->getName());
$this->assertSame('край', $region->getPrefix());
$this->assertSame(14, $region->getCode());
$this->assertSame(27, $region->getCodeExt());
$this->assertSame('7d468b39-1afa-41ec-8c4f-97a8603cb3d4', $region->getFiasGuid());
$this->assertSame('Russia', $region->getCountryName());
$this->assertSame(643, $region->getCountryCodeExt());
$this->assertSame(1, $region->getCountryCode());
$this->assertSame('RU', $region->getCountryCodeISO());

foreach ($response as $item) {
break;
}

assert(isset($item));
$this->assertSame($region, $item);
}

public function test_it_has_no_errors()
{
$response = new RegionsResponse();
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixtures/data/RegionsISO.xml
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Regions><Region regionName="Хабаровский" regionCode="14" regionUuid="0c4563e7-3465-4325-a6d8-be0e06a1ab48" prefix="край" regionCodeExt="27" regionFiasGuid="7d468b39-1afa-41ec-8c4f-97a8603cb3d4" countryName="Russia" countryCode="RU" countryCodeExt="643"/></Regions>

16 changes: 8 additions & 8 deletions tests/Integration/RegionsRequestTest.php
Expand Up @@ -58,15 +58,15 @@ public function test_example()
$this->markTestSkipped("Unknown country: {$region->getCountryName()}");
}

$this->assertSame('18aff43f-58b8-4608-ade7-92fdab7fc39f', $region->getUuid());
$this->assertSame('Тверская', $region->getName());
$this->assertSame('обл', $region->getPrefix());
$this->assertSame(50, $region->getCode());
$this->assertSame(69, $region->getCodeExt());
$this->assertSame('61723327-1c20-42fe-8dfa-402638d9b396', $region->getFiasGuid());
$this->assertSame('Россия', $region->getCountryName());
$this->assertSame(1, $region->getCountryCode());
$this->assertSame('0c4563e7-3465-4325-a6d8-be0e06a1ab48', $region->getUuid(), sprintf('Received unexpected region: %s', var_export($region, true)));
$this->assertSame('Хабаровский', $region->getName());
$this->assertSame('край', $region->getPrefix());
$this->assertSame(14, $region->getCode());
$this->assertSame(27, $region->getCodeExt());
$this->assertSame('7d468b39-1afa-41ec-8c4f-97a8603cb3d4', $region->getFiasGuid());
$this->assertSame('Russia', $region->getCountryName());
$this->assertSame(643, $region->getCountryCodeExt());
$this->assertSame('RU', $region->getCountryCodeISO());
}

public function test_with_everything()
Expand Down

0 comments on commit f8d77de

Please sign in to comment.