You are here

class TwitterProfileTest in Twitter Profile Widget 8

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/TwitterProfileTest.php \Drupal\Tests\twitter_profile_widget\Unit\TwitterProfileTest
  2. 3.x tests/src/Unit/TwitterProfileTest.php \Drupal\Tests\twitter_profile_widget\Unit\TwitterProfileTest

Tests the "TwitterProfile" service, which builds Twitter API queries.

@coversDefaultClass \Drupal\twitter_profile_widget\TwitterProfile @group twitter_profile_widget

Hierarchy

Expanded class hierarchy of TwitterProfileTest

See also

Drupal\twitter_profile_widget\TwitterProfile

File

tests/src/Unit/TwitterProfileTest.php, line 16

Namespace

Drupal\Tests\twitter_profile_widget\Unit
View source
class TwitterProfileTest extends UnitTestCase {

  /**
   * Get an accessible method using reflection.
   */
  public function getAccessibleMethod($class_name, $method_name) {
    $class = new \ReflectionClass($class_name);
    $method = $class
      ->getMethod($method_name);
    $method
      ->setAccessible(TRUE);
    return $method;
  }

  /**
   * Test TwitterProfile::buildQuery().
   *
   * Test that an expected Twitter REST URL for the twitter timeline returns.
   * Since buildQuery() is a protected method, alter the class using reflection.
   *
   * @dataProvider queryDataProvider
   */
  public function testQuery($config, $expected) {

    // Get a reflected, accessible version of the buildQuery() method.
    $protected_method = $this
      ->getAccessibleMethod('Drupal\\twitter_profile_widget\\TwitterProfile', 'buildQuery');

    // Create a new TwitterProfile object.
    $pp = new TwitterProfile();

    // Use the reflection to invoke on the object.
    $result = $protected_method
      ->invokeArgs($pp, [
      $config['account'],
      $config['type'],
      $config['timeline'],
      $config['search'],
      $config['replies'],
      $config['retweets'],
    ]);

    // Make an assertion.
    $this
      ->assertEquals($expected['url'], $result['url']);
    $this
      ->assertEquals($expected['getfield'], $result['getfield']);
  }

  /**
   * Data provider for testQuery().
   */
  public function queryDataProvider() {
    return [
      [
        [
          'account' => 'testuser',
          'type' => 'timeline',
          'timeline' => 'mytimeline',
          'search' => 'search param',
          'replies' => 1,
          'retweets' => 1,
        ],
        [
          'url' => 'https://api.twitter.com/1.1/lists/statuses.json',
          'getfield' => '?count=10&slug=mytimeline&owner_screen_name=testuser&include_rts=1',
        ],
      ],
      [
        [
          'account' => 'testuser',
          'type' => 'timeline',
          'timeline' => 'mytimeline',
          'search' => 'search param',
          'replies' => 0,
          'retweets' => 0,
        ],
        [
          'url' => 'https://api.twitter.com/1.1/lists/statuses.json',
          'getfield' => '?count=10&slug=mytimeline&owner_screen_name=testuser&include_rts=0&exclude_replies=1',
        ],
      ],
      [
        [
          'account' => 'testuser',
          'type' => 'search',
          'timeline' => 'mytimeline',
          'search' => 'search param',
          'replies' => 1,
          'retweets' => 1,
        ],
        [
          'url' => 'https://api.twitter.com/1.1/search/tweets.json',
          'getfield' => '?q=search+param&count=10',
        ],
      ],
      [
        [
          'account' => 'testuser',
          'type' => 'search',
          'timeline' => 'mytimeline',
          'search' => '#search . param%',
          'replies' => 1,
          'retweets' => 1,
        ],
        [
          'url' => 'https://api.twitter.com/1.1/search/tweets.json',
          'getfield' => '?q=%23search+.+param%25&count=10',
        ],
      ],
      [
        [
          'account' => 'testuser',
          'type' => 'favorites',
          'timeline' => 'mytimeline',
          'search' => 'search param',
          'replies' => 1,
          'retweets' => 1,
        ],
        [
          'url' => 'https://api.twitter.com/1.1/favorites/list.json',
          'getfield' => '?count=10&screen_name=testuser',
        ],
      ],
      [
        [
          'account' => 'testuser',
          'type' => 'status',
          'timeline' => 'mytimeline',
          'search' => 'search param',
          'replies' => 1,
          'retweets' => 1,
        ],
        [
          'url' => 'https://api.twitter.com/1.1/statuses/user_timeline.json',
          'getfield' => '?count=10&screen_name=testuser&include_rts=1',
        ],
      ],
      [
        [
          'account' => 'testuser',
          'type' => 'status',
          'timeline' => 'mytimeline',
          'search' => 'search param',
          'replies' => 0,
          'retweets' => 0,
        ],
        [
          'url' => 'https://api.twitter.com/1.1/statuses/user_timeline.json',
          'getfield' => '?count=10&screen_name=testuser&include_rts=0&exclude_replies=1',
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
TwitterProfileTest::getAccessibleMethod public function Get an accessible method using reflection.
TwitterProfileTest::queryDataProvider public function Data provider for testQuery().
TwitterProfileTest::testQuery public function Test TwitterProfile::buildQuery().
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.
UnitTestCase::setUp protected function 340