Passed
Pull Request — 4 (#10244)
by Steve
08:00
created

EmbedUnitTest::testPass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace SilverStripe\View\Tests\Embed;
4
5
use SilverStripe\Core\Injector\Injector;
6
use SilverStripe\Dev\SapphireTest;
7
use SilverStripe\View\Embed\EmbedContainer;
8
use SilverStripe\View\Embed\Embeddable;
9
use Embed\Http\Crawler;
10
use Embed\Embed;
11
12
/**
13
 * Special unit test class to faciliate mock embed/embed requests
14
 */
15
class EmbedUnitTest extends SapphireTest
16
{
17
    private bool $firstRequest = true;
18
19
    public function getFirstRequest(): bool
20
    {
21
        return $this->firstRequest;
22
    }
23
24
    public function setFirstRequest(bool $b): void
25
    {
26
        $this->firstRequest = $b;
27
    }
28
29
    protected function createEmbedContainer(
30
        string $urlA,
31
        string $urlB,
32
        string $firstResponse,
33
        string $secondResponse
34
    ): EmbedContainer {
35
        $this->registerCrawlerService($urlA, $urlB, $firstResponse, $secondResponse);
36
        $embedContainer = EmbedContainer::create($urlA);
37
        return $embedContainer;
38
    }
39
40
    private function registerCrawlerService(
41
        string $urlA,
42
        string $urlB,
43
        string $firstResponse,
44
        string $secondResponse
45
    ): void {
46
        $mockUriA = new MockUri($urlA);
47
        $mockUriB = new MockUri($urlB);
48
        $crawlerMock = $this->createMock(Crawler::class);
49
        $crawlerMock->method('getResponseUri')->willReturn($mockUriA);
50
        $crawlerMock->method('createUri')->willReturn($mockUriB);
51
        $crawlerMock->method('sendRequest')->willReturn(new MockResponse($this, $firstResponse, $secondResponse));
52
        $crawlerMock->method('createRequest')->willReturn(new MockRequest($this, $mockUriA));
53
        Injector::inst()->registerService($crawlerMock, Crawler::class);
54
        // replace the existing registered Embed singleton with a new singleton that is
55
        // created using $crawlerMock as the the __constructor argument - see oembed.yml
56
        $embed = Injector::inst()->create(Embed::class, $crawlerMock);
57
        Injector::inst()->registerService($embed, Embed::class);
58
    }
59
60
    /**
61
     * This is to prevent the following warning:
62
     * No tests found in class "SilverStripe\View\Tests\Embed\EmbedUnitTest".
63
     */
64
    public function testPass()
65
    {
66
        $this->assertTrue(true);
67
    }
68
}
69