You are here

class DrupalClientTest in Facebook Instant Articles 3.x

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/DrupalClientTest.php \Drupal\Tests\fb_instant_articles\Unit\DrupalClientTest

Test the Drupal FBIA client wrapper.

@group fb_instant_articles

@coversDefaultClass \Drupal\fb_instant_articles\DrupalClient

Hierarchy

Expanded class hierarchy of DrupalClientTest

File

tests/src/Unit/DrupalClientTest.php, line 21

Namespace

Drupal\Tests\fb_instant_articles\Unit
View source
class DrupalClientTest extends UnitTestCase {
  protected $logger;
  protected $serializer;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->serializer = $this
      ->createMock(NormalizerInterface::class);
    $this->logger = $this
      ->createMock(LoggerChannelInterface::class);
  }

  /**
   * Test the import entity method.
   *
   * @covers ::importEntity
   */
  public function testImportEntity() {
    $client = $this
      ->getMockBuilder(DrupalClient::class)
      ->disableOriginalConstructor()
      ->onlyMethods([
      'importArticle',
    ])
      ->getMock();
    $client
      ->setStringTranslation($this
      ->getStringTranslationStub());
    $client
      ->setSerializer($this->serializer);
    $client
      ->setLogger($this->logger);
    $client
      ->expects($this
      ->once())
      ->method('importArticle')
      ->with($this
      ->isNull(), $this
      ->isTrue());
    $client
      ->importEntity($this
      ->createMock(ContentEntityInterface::class));

    // Test that importArticle is called with FALSE for an unpublished entity.
    $client = $this
      ->getMockBuilder(DrupalClient::class)
      ->disableOriginalConstructor()
      ->onlyMethods([
      'importArticle',
    ])
      ->getMock();
    $client
      ->setStringTranslation($this
      ->getStringTranslationStub());
    $client
      ->setSerializer($this->serializer);
    $client
      ->setLogger($this->logger);
    $client
      ->expects($this
      ->once())
      ->method('importArticle')
      ->with($this
      ->isNull(), $this
      ->isFalse());
    $entity = $this
      ->getMockBuilder(NodeInterface::class)
      ->getMock();
    $entity
      ->method('isPublished')
      ->willReturn(FALSE);
    $client
      ->importEntity($entity);
  }

  /**
   * Test the remove entity method.
   *
   * @covers ::removeEntity
   */
  public function testRemoveEntity() {

    // Test that removeArticle is called with the canonical URL from the
    // iaNormalizer object.
    $client = $this
      ->getMockBuilder(DrupalClient::class)
      ->disableOriginalConstructor()
      ->onlyMethods([
      'removeArticle',
    ])
      ->getMock();
    $ia_status = $this
      ->getMockBuilder(InstantArticleStatus::class)
      ->disableOriginalConstructor()
      ->getMock();
    $client
      ->method('removeArticle')
      ->willReturn($ia_status);
    $client
      ->setStringTranslation($this
      ->getStringTranslationStub());
    $client
      ->setLogger($this->logger);
    $ia_normalizer = $this
      ->getMockBuilder(InstantArticleContentEntityNormalizer::class)
      ->disableOriginalConstructor()
      ->getMock();
    $ia_normalizer
      ->method('entityCanonicalUrl')
      ->willReturn('http://www.example.com/node/1');
    $client
      ->setIaNormalizer($ia_normalizer);
    $client
      ->expects($this
      ->once())
      ->method('removeArticle')
      ->with('http://www.example.com/node/1');
    $client
      ->removeEntity($this
      ->createMock(ContentEntityInterface::class));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalClientTest::$logger protected property
DrupalClientTest::$serializer protected property
DrupalClientTest::setUp protected function Overrides UnitTestCase::setUp
DrupalClientTest::testImportEntity public function Test the import entity method.
DrupalClientTest::testRemoveEntity public function Test the remove entity method.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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.
UnitTestCase::setUpBeforeClass public static function