You are here

class ChainStoreResolverTest in Commerce Core 8.2

@coversDefaultClass \Drupal\commerce_store\Resolver\ChainStoreResolver @group commerce_store

Hierarchy

Expanded class hierarchy of ChainStoreResolverTest

File

modules/store/tests/src/Unit/Resolver/ChainStoreResolverTest.php, line 13

Namespace

Drupal\Tests\commerce_store\Unit\Resolver
View source
class ChainStoreResolverTest extends UnitTestCase {

  /**
   * The resolver.
   *
   * @var \Drupal\commerce_store\Resolver\ChainStoreResolver
   */
  protected $resolver;

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

  /**
   * Tests the resolver and priority.
   *
   * ::covers addResolver
   * ::covers getResolvers
   * ::covers resolve.
   */
  public function testResolver() {
    $container = new ContainerBuilder();
    $mock_builder = $this
      ->getMockBuilder('Drupal\\commerce_store\\Resolver\\StoreResolverInterface')
      ->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('testStore');
    $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->resolver
        ->addResolver($container
        ->get($id));
    }
    $result = $this->resolver
      ->resolve();
    $this
      ->assertEquals('testStore', $result);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ChainStoreResolverTest::$resolver protected property The resolver.
ChainStoreResolverTest::setUp protected function Overrides UnitTestCase::setUp
ChainStoreResolverTest::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.