You are here

class ChainCountryResolverTest in Commerce Core 8.2

@coversDefaultClass Drupal\commerce\Resolver\ChainCountryResolver @group commerce

Hierarchy

Expanded class hierarchy of ChainCountryResolverTest

File

tests/src/Unit/Resolver/ChainCountryResolverTest.php, line 13

Namespace

Drupal\Tests\commerce\Unit\Resolver
View source
class ChainCountryResolverTest extends UnitTestCase {

  /**
   * The chain country resolver.
   *
   * @var \Drupal\commerce\Resolver\ChainCountryResolver
   */
  protected $chainCountryResolver;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->chainCountryResolver = new ChainCountryResolver();
  }

  /**
   * Tests the resolver and priority.
   *
   * ::covers addResolver
   * ::covers getResolvers
   * ::covers resolve.
   */
  public function testResolver() {
    $container = new ContainerBuilder();
    $mock_builder = $this
      ->getMockBuilder('Drupal\\commerce\\Resolver\\CountryResolverInterface')
      ->disableOriginalConstructor();
    $first_resolver = $mock_builder
      ->getMock();
    $first_resolver
      ->expects($this
      ->once())
      ->method('resolve');
    $container
      ->set('commerce.first_resolver', $first_resolver);
    $second_resolver = $mock_builder
      ->getMock();
    $second_resolver
      ->expects($this
      ->once())
      ->method('resolve')
      ->willReturn('RS');
    $container
      ->set('commerce.second_resolver', $second_resolver);
    $third_resolver = $mock_builder
      ->getMock();
    $third_resolver
      ->expects($this
      ->never())
      ->method('resolve');
    $container
      ->set('commerce.third_resolver', $third_resolver);

    // Mimic how the container would add the services.
    // @see \Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass::process
    $resolvers = [
      'commerce.first_resolver' => 900,
      'commerce.second_resolver' => 400,
      'commerce.third_resolver' => -100,
    ];
    arsort($resolvers, SORT_NUMERIC);
    foreach ($resolvers as $id => $priority) {
      $this->chainCountryResolver
        ->addResolver($container
        ->get($id));
    }
    $result = $this->chainCountryResolver
      ->resolve();
    $this
      ->assertEquals('RS', $result);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ChainCountryResolverTest::$chainCountryResolver protected property The chain country resolver.
ChainCountryResolverTest::setUp protected function Overrides UnitTestCase::setUp
ChainCountryResolverTest::testResolver public function Tests the resolver and priority.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.