anonymous//tests/Http/HttpTest.php$0   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 7
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 10
wmc 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SteamMarketProviders\ParserManager\Tests\Http;
6
7
use PHPUnit\Framework\TestCase;
8
use SteamMarketProviders\ParserManager\Contract\StrategyInterface;
9
use SteamMarketProviders\ParserManager\Exception\InvalidArgumentException;
10
use SteamMarketProviders\ParserManager\Http\Http;
11
use SteamMarketProviders\ParserManager\Http\Options;
12
use SteamMarketProviders\ParserManager\Http\Response;
13
use SteamMarketProviders\ParserManager\Http\Strategy\GuzzleStrategy;
14
15
class HttpTest extends TestCase
16
{
17
    private readonly Http $http;
18
    private StrategyInterface $strategy;
19
20
    public function setUp(): void
21
    {
22
        $this->http = new Http();
0 ignored issues
show
Bug introduced by
The property http is declared read-only in SteamMarketProviders\Par...ger\Tests\Http\HttpTest.
Loading history...
23
24
        $this->strategy = new class () implements StrategyInterface {
25
            public function __construct(?Options $options = null)
26
            {
27
            }
28
29
            public function sendRequest(string $url): Response
30
            {
31
            }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return SteamMarketProviders\ParserManager\Http\Response. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
32
        };
33
    }
34
35
    public function testSetUrl(): void
36
    {
37
        $result = $this->http->setUrl("https://steamcommunity.com/market/listings/730/AWP%20%7C%20Phobos%20%28Field-Tested%29/render?start=0&count=1&currency=1&format=json");
38
        $this->assertInstanceOf(Http::class, $result);
39
    }
40
41
    public function testFailureSetUrl(): void
42
    {
43
        $url = "https://steamcommunity.com/market/listings/730/Awp Dragon lord/render?start=0&count=1&currency=1&format=json";
44
45
        $this->expectException(InvalidArgumentException::class);
46
        $this->expectExceptionMessage('The url: "https://steamcommunity.com/market/listings/730/Awp Dragon lord/render?start=0&count=1&currency=1&format=json" is not in a valid format');
47
48
        $this->http->setUrl($url);
49
    }
50
51
    public function testDefaultStrategy(): void
52
    {
53
        $this->assertInstanceOf(GuzzleStrategy::class, $this->http->getStrategy());
54
    }
55
56
    public function testSetStrategy(): void
57
    {
58
        $this->http->setStrategy($this->strategy);
59
60
        $this->assertInstanceOf(StrategyInterface::class, $this->http->getStrategy());
61
    }
62
63
    public function testSendRequestWhenUrlIsNotSet(): void
64
    {
65
        $this->expectExceptionMessage('Typed property SteamMarketProviders\ParserManager\Http\Http::$url must not be accessed before initialization');
66
        $this->http->sendRequest();
67
    }
68
}
69