-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VACMS-19227: Adds unit test coverage for AddPhilippinesAsStateSubscri…
…ber.
- Loading branch information
Showing
1 changed file
with
92 additions
and
0 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
tests/phpunit/va_gov_address/unit/EventSubscriber/AddPhilippinesAsStateSubscriberTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace tests\phpunit\va_gov_address\unit\EventSubscriber; | ||
|
||
use Drupal\address\Event\AddressEvents; | ||
use Drupal\address\Event\SubdivisionsEvent; | ||
use Drupal\va_gov_address\EventSubscriber\AddPhilippinesAsStateSubscriber; | ||
use Drupal\Tests\UnitTestCase; | ||
use Prophecy\Argument; | ||
|
||
/** | ||
* Tests custom US address group. | ||
* | ||
* @coversDefaultClass \Drupal\va_gov_address\EventSubscriber\AddPhilippinesAsStateSubscriber | ||
* | ||
* @group va_gov_address | ||
*/ | ||
class AddPhilippinesAsStateSubscriberTest extends UnitTestCase { | ||
|
||
/** | ||
* The event subscriber under test. | ||
* | ||
* @var \Drupal\va_gov_address\EventSubscriber\AddPhilippinesAsStateSubscriber | ||
*/ | ||
protected AddPhilippinesAsStateSubscriber $subscriber; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp(): void { | ||
parent::setUp(); | ||
$this->subscriber = new AddPhilippinesAsStateSubscriber(); | ||
} | ||
|
||
/** | ||
* Tests the getSubscribedEvents method. | ||
* | ||
* @covers ::getSubscribedEvents | ||
*/ | ||
public function testGetSubscribedEvents() { | ||
$events = AddPhilippinesAsStateSubscriber::getSubscribedEvents(); | ||
$this->assertArrayHasKey(AddressEvents::SUBDIVISIONS, $events); | ||
$this->assertIsArray($events[AddressEvents::SUBDIVISIONS]); | ||
$this->assertEquals(['onSubdivisions'], array_column($events[AddressEvents::SUBDIVISIONS], 0)); | ||
} | ||
|
||
/** | ||
* Tests the onSubdivisions method. | ||
* | ||
* @covers ::onSubdivisions | ||
*/ | ||
public function testOnSubdivisions() { | ||
// Create a mock event. | ||
/** @var \Prophecy\Prophecy\ObjectProphecy|\Drupal\address\Event\SubdivisionsEvent $event */ | ||
$event = $this->prophesize(SubdivisionsEvent::class); | ||
|
||
// Mock the getParents method to return ['US']. | ||
$event->getParents()->willReturn(['US']); | ||
|
||
// Set up expected calls for getParents() and setDefinitions(). | ||
$event->getParents()->willReturn(['US']); | ||
$event->setDefinitions(Argument::that(function ($definitions) { | ||
// Validate that Philippines (PH) is added as a subdivision. | ||
return isset($definitions['subdivisions']['PH']) | ||
&& $definitions['subdivisions']['PH']['name'] === 'Philippines'; | ||
}))->shouldBeCalled(); | ||
|
||
// Call the onSubdivisions method. | ||
$this->subscriber->onSubdivisions($event->reveal()); | ||
} | ||
|
||
/** | ||
* Tests that onSubdivisions does not modify definitions for non-US parents. | ||
* | ||
* @covers ::onSubdivisions | ||
*/ | ||
public function testOnSubdivisionsNonUS() { | ||
// Create a mock event. | ||
/** @var \Prophecy\Prophecy\ObjectProphecy|\Drupal\address\Event\SubdivisionsEvent $event */ | ||
$event = $this->prophesize(SubdivisionsEvent::class); | ||
|
||
// Mock the getParents method to return a non-US parent. | ||
$event->getParents()->willReturn(['CA']); | ||
|
||
// Assert that setDefinitions is never called. | ||
$event->setDefinitions()->shouldNotBeCalled(); | ||
|
||
// Call the onSubdivisions method. | ||
$this->subscriber->onSubdivisions($event->reveal()); | ||
} | ||
|
||
} |