CountryCodeConstraintTest.php

Same filename and directory in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Validation/Plugin/Validation/Constraint/CountryCodeConstraintTest.php
  2. 10 core/tests/Drupal/Tests/Core/Validation/Plugin/Validation/Constraint/CountryCodeConstraintTest.php

Namespace

Drupal\Tests\Core\Validation\Plugin\Validation\Constraint

File

core/tests/Drupal/Tests/Core/Validation/Plugin/Validation/Constraint/CountryCodeConstraintTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\Core\Validation\Plugin\Validation\Constraint;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Locale\CountryManagerInterface;
use Drupal\Core\Validation\Plugin\Validation\Constraint\CountryCodeConstraint;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use Symfony\Component\Validator\Constraints\Choice;

/**
 * Tests Country Code Constraint.
 */
class CountryCodeConstraintTest extends UnitTestCase {
  public function testConstraintLoadsChoicesFromCountryManager() : void {
    $countries = [
      'US' => 'United States',
      'CA' => 'Canada',
      'MX' => 'Mexico',
    ];
    // The CountryCode constraint should call the country manager's getList()
    // method to compile a list of valid choices.
    $country_manager = $this->createMock(CountryManagerInterface::class);
    $country_manager->expects($this->atLeastOnce())
      ->method('getList')
      ->willReturn($countries);
    $container = new ContainerBuilder();
    $container->set(CountryManagerInterface::class, $country_manager);
    $constraint = CountryCodeConstraint::create($container, [], 'CountryCode', []);
    $this->assertInstanceOf(Choice::class, $constraint);
    $this->assertSame(array_keys($countries), $constraint->choices);
  }

}

Classes

Title Deprecated Summary
CountryCodeConstraintTest Tests Country Code Constraint.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.