Source of file rssWrapperTest.php

Size: 3,637 Bytes - Last Modified: 2020-07-24T22:00:02+00:00

/root/gitwork/work/tornelib-php-netcurl-6.1/tests/rssWrapperTest.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
<?php

require_once(__DIR__ . '/../vendor/autoload.php');

use PHPUnit\Framework\TestCase;
use TorneLIB\IO\Data\Arrays;
use TorneLIB\Model\Type\dataType;
use TorneLIB\Model\Type\requestMethod;
use TorneLIB\Module\Network\NetWrapper;
use TorneLIB\Module\Network\Wrappers\RssWrapper;

class rssWrapperTest extends TestCase
{
    /**
     * @test
     */
    public function consumeLaminasRss()
    {
        if (!class_exists('\Laminas\Feed\Reader\Reader')) {
            static::markTestSkipped('Laminas is not present.');
            return;
        }
        try {
            $rssFeed = (new RssWrapper())->request('https://www.tornevalls.se/feed/')->getParsed();
            if (method_exists($rssFeed, 'getTitle')) {
                static::assertNotSame($rssFeed->getTitle(), '');
            } else {
                static::assertTrue(
                    isset($rssFeed[0][0]) && strlen($rssFeed[0][0]) > 5
                );
            }
        } catch (Exception $e) {
            static::markTestSkipped(
                sprintf(
                    'Non critical exception in %s: %s (%s).',
                    __FUNCTION__,
                    $e->getMessage(),
                    $e->getCode()
                )
            );
        }
    }

    /**
     * @test
     */
    public function consumeByNetWrapperNormalRequest()
    {
        /** @noinspection DuplicatedCode */
        try {
            $rssFeed = (new NetWrapper())
                ->request(
                    'https://www.tornevalls.se/feed/',
                    [],
                    requestMethod::METHOD_GET,
                    dataType::NORMAL
                )->getParsed();


            // Test failed for PHP 8: Checking methods requires that it is not null first.
            if (!isset($rssFeed) && !empty($rssFeed) && method_exists($rssFeed, 'getTitle')) {
                static::assertNotSame($rssFeed->getTitle(), '');
            } else {
                $rssFeed = (new Arrays())->objectsIntoArray($rssFeed);
                static::assertTrue(
                    isset($rssFeed[0][0]) && strlen($rssFeed[0][0]) > 5
                );
            }
        } catch (Exception $e) {
            static::markTestSkipped(
                sprintf(
                    'Non critical exception in %s: %s (%s).',
                    __FUNCTION__,
                    $e->getMessage(),
                    $e->getCode()
                )
            );
        }
    }

    /**
     * @test
     */
    public function consumeByNetWrapperLaminasChoice()
    {
        if (!class_exists('\Laminas\Feed\Reader\Reader')) {
            static::markTestSkipped('Laminas is not present.');
            return;
        }

        /** @noinspection DuplicatedCode */
        try {
            $rssFeed = (new NetWrapper())
                ->request(
                    'https://www.tornevalls.se/feed/',
                    [],
                    requestMethod::METHOD_GET,
                    dataType::RSS_XML
                )->getParsed();

            if (method_exists($rssFeed, 'getTitle')) {
                static::assertNotSame($rssFeed->getTitle(), '');
            } else {
                static::assertTrue(
                    isset($rssFeed[0][0]) && strlen($rssFeed[0][0]) > 5
                );
            }
        } catch (Exception $e) {
            static::markTestSkipped(
                sprintf(
                    'Non critical exception in %s: %s (%s).',
                    __FUNCTION__,
                    $e->getMessage(),
                    $e->getCode()
                )
            );
        }
    }
}