class AnnounceFetcherUnitTest

Same name in other branches
  1. 10 core/modules/announcements_feed/tests/src/Unit/AnnounceFetcherUnitTest.php \Drupal\Tests\announcements_feed\Unit\AnnounceFetcherUnitTest

Simple test to ensure that asserts pass.

@group announcements_feed

Hierarchy

Expanded class hierarchy of AnnounceFetcherUnitTest

File

core/modules/announcements_feed/tests/src/Unit/AnnounceFetcherUnitTest.php, line 15

Namespace

Drupal\Tests\announcements_feed\Unit
View source
class AnnounceFetcherUnitTest extends UnitTestCase {
    
    /**
     * The Fetcher service object.
     *
     * @var \Drupal\announcements_feed\AnnounceFetcher
     */
    protected AnnounceFetcher $fetcher;
    
    /**
     * {@inheritdoc}
     */
    public function setUp() : void {
        parent::setUp();
        $httpClient = $this->createMock('GuzzleHttp\\ClientInterface');
        $config = $this->getConfigFactoryStub([
            'announcements_feed.settings' => [
                'max_age' => 86400,
                'cron_interval' => 21600,
                'limit' => 10,
            ],
        ]);
        $tempStore = $this->createMock('Drupal\\Core\\KeyValueStore\\KeyValueExpirableFactoryInterface');
        $tempStore->expects($this->once())
            ->method('get')
            ->willReturn($this->createMock('Drupal\\Core\\KeyValueStore\\KeyValueStoreExpirableInterface'));
        $logger = $this->createMock('Psr\\Log\\LoggerInterface');
        $this->fetcher = new AnnounceFetcher($httpClient, $config, $tempStore, $logger, 'https://www.drupal.org/announcements.json');
    }
    
    /**
     * Test the ValidateUrl() method.
     *
     * @covers \Drupal\announcements_feed\AnnounceFetcher::validateUrl
     *
     * @dataProvider urlProvider
     */
    public function testValidateUrl($url, $isValid) : void {
        $this->assertEquals($isValid, $this->fetcher
            ->validateUrl($url));
    }
    
    /**
     * Data for the testValidateUrl.
     */
    public static function urlProvider() : array {
        return [
            [
                'https://www.drupal.org',
                TRUE,
            ],
            [
                'https://drupal.org',
                TRUE,
            ],
            [
                'https://api.drupal.org',
                TRUE,
            ],
            [
                'https://a.drupal.org',
                TRUE,
            ],
            [
                'https://123.drupal.org',
                TRUE,
            ],
            [
                'https://api-new.drupal.org',
                TRUE,
            ],
            [
                'https://api_new.drupal.org',
                TRUE,
            ],
            [
                'https://api-.drupal.org',
                TRUE,
            ],
            [
                'https://www.example.org',
                FALSE,
            ],
            [
                'https://example.org',
                FALSE,
            ],
            [
                'https://api.example.org/project/announce',
                FALSE,
            ],
            [
                'https://-api.drupal.org',
                FALSE,
            ],
            [
                'https://a.example.org/project/announce',
                FALSE,
            ],
            [
                'https://test.drupaal.com',
                FALSE,
            ],
            [
                'https://api.drupal.org.example.com',
                FALSE,
            ],
            [
                'https://example.org/drupal.org',
                FALSE,
            ],
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AnnounceFetcherUnitTest::$fetcher protected property The Fetcher service object.
AnnounceFetcherUnitTest::setUp public function Overrides UnitTestCase::setUp
AnnounceFetcherUnitTest::testValidateUrl public function Test the ValidateUrl() method.
AnnounceFetcherUnitTest::urlProvider public static function Data for the testValidateUrl.
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

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