Skip to content

Commit

Permalink
VACMS-19227: Adds unit test coverage for AddPhilippinesAsStateSubscri…
Browse files Browse the repository at this point in the history
…ber.
  • Loading branch information
dsasser committed Dec 18, 2024
1 parent 0273d81 commit 2f50d7a
Showing 1 changed file with 92 additions and 0 deletions.
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());
}

}

0 comments on commit 2f50d7a

Please sign in to comment.