From 2f50d7a5fcc38c1d1f7da53e47afc722f8945d78 Mon Sep 17 00:00:00 2001 From: Daniel Sasser <221539+dsasser@users.noreply.github.com> Date: Wed, 18 Dec 2024 12:38:41 -0800 Subject: [PATCH] VACMS-19227: Adds unit test coverage for AddPhilippinesAsStateSubscriber. --- .../AddPhilippinesAsStateSubscriberTest.php | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 tests/phpunit/va_gov_address/unit/EventSubscriber/AddPhilippinesAsStateSubscriberTest.php diff --git a/tests/phpunit/va_gov_address/unit/EventSubscriber/AddPhilippinesAsStateSubscriberTest.php b/tests/phpunit/va_gov_address/unit/EventSubscriber/AddPhilippinesAsStateSubscriberTest.php new file mode 100644 index 0000000000..e9fc1b1773 --- /dev/null +++ b/tests/phpunit/va_gov_address/unit/EventSubscriber/AddPhilippinesAsStateSubscriberTest.php @@ -0,0 +1,92 @@ +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()); + } + +}