QuoteTwigExtensionTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 43
dl 0
loc 161
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A testQuoteFunctionWithBadDomain() 0 5 1
A testQuoteFunctionWithoutProvider() 0 5 1
A test__construct() 0 10 1
A testGetFunctions() 0 21 1
A testQuoteAuthorFunction() 0 5 1
A testQuoteContentFunctionWithBadDomain() 0 5 1
A testQuoteAuthorFunctionWithBadDomain() 0 5 1
A testQuoteContentFunction() 0 5 1
A testQuoteFunction() 0 9 1
1
<?php
2
3
/*
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2019 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\CoreBundle\Tests\Twig\Extension;
13
14
use Throwable;
15
use Twig\Extension\ExtensionInterface;
16
use Twig\Node\Node;
17
use Twig\TwigFunction;
18
use WBW\Bundle\CoreBundle\Tests\AbstractTestCase;
19
use WBW\Bundle\CoreBundle\Twig\Extension\QuoteTwigExtension;
20
use WBW\Library\Symfony\Manager\QuoteManager;
21
use WBW\Library\Symfony\Provider\Quote\WorldsWisdomQuoteProvider;
22
23
/**
24
 * Quote Twig extension test.
25
 *
26
 * @author webeweb <https://github.com/webeweb>
27
 * @package WBW\Bundle\CoreBundle\Tests\Twig\Extension
28
 */
29
class QuoteTwigExtensionTest extends AbstractTestCase {
30
31
    /**
32
     * Quote manager.
33
     *
34
     * @var QuoteManager
35
     */
36
    private $quoteManager;
37
38
    /**
39
     * {@inheritDoc}
40
     */
41
    protected function setUp(): void {
42
        parent::setUp();
43
44
        // Set a Quote provider mock.
45
        $quoteProvider = new WorldsWisdomQuoteProvider();
46
47
        // Set a Quote manager mock.
48
        $this->quoteManager = new QuoteManager($this->logger);
49
        $this->quoteManager->addProvider($quoteProvider);
50
    }
51
52
    /**
53
     * Test getFunctions()
54
     *
55
     * @return void
56
     */
57
    public function testGetFunctions(): void {
58
59
        $obj = new QuoteTwigExtension($this->twigEnvironment, $this->quoteManager);
60
61
        $res = $obj->getFunctions();
62
        $this->assertCount(3, $res);
63
64
        $this->assertInstanceOf(TwigFunction::class, $res[0]);
65
        $this->assertEquals("quote", $res[0]->getName());
66
        $this->assertEquals([$obj, "quoteFunction"], $res[0]->getCallable());
67
        $this->assertEquals(["html"], $res[0]->getSafe(new Node()));
68
69
        $this->assertInstanceOf(TwigFunction::class, $res[1]);
70
        $this->assertEquals("quoteAuthor", $res[1]->getName());
71
        $this->assertEquals([$obj, "quoteAuthorFunction"], $res[1]->getCallable());
72
        $this->assertEquals(["html"], $res[1]->getSafe(new Node()));
73
74
        $this->assertInstanceOf(TwigFunction::class, $res[2]);
75
        $this->assertEquals("quoteContent", $res[2]->getName());
76
        $this->assertEquals([$obj, "quoteContentFunction"], $res[2]->getCallable());
77
        $this->assertEquals(["html"], $res[2]->getSafe(new Node()));
78
    }
79
80
    /**
81
     * Test quoteAuthorFunction()
82
     *
83
     * @return void
84
     * @throws Throwable Throws an exception if an error occurs.
85
     */
86
    public function testQuoteAuthorFunction(): void {
87
88
        $obj = new QuoteTwigExtension($this->twigEnvironment, $this->quoteManager);
89
90
        $this->assertNotNull($obj->quoteAuthorFunction());
91
    }
92
93
    /**
94
     * Test quoteAuthorFunction()
95
     *
96
     * @return void
97
     * @throws Throwable Throws an exception if an error occurs.
98
     */
99
    public function testQuoteAuthorFunctionWithBadDomain(): void {
100
101
        $obj = new QuoteTwigExtension($this->twigEnvironment, $this->quoteManager);
102
103
        $this->assertEquals("", $obj->quoteAuthorFunction("github"));
104
    }
105
106
    /**
107
     * Test quoteContentFunction()
108
     *
109
     * @return void
110
     * @throws Throwable Throws an exception if an error occurs.
111
     */
112
    public function testQuoteContentFunction(): void {
113
114
        $obj = new QuoteTwigExtension($this->twigEnvironment, $this->quoteManager);
115
116
        $this->assertNotNull($obj->quoteContentFunction());
117
    }
118
119
    /**
120
     * Test quoteContentFunction()
121
     *
122
     * @return void
123
     * @throws Throwable Throws an exception if an error occurs.
124
     */
125
    public function testQuoteContentFunctionWithBadDomain(): void {
126
127
        $obj = new QuoteTwigExtension($this->twigEnvironment, $this->quoteManager);
128
129
        $this->assertEquals("", $obj->quoteContentFunction("github"));
130
    }
131
132
    /**
133
     * Test quoteFunction()
134
     *
135
     * @return void
136
     * @throws Throwable Throws an exception if an error occurs.
137
     */
138
    public function testQuoteFunction(): void {
139
140
        $obj = new QuoteTwigExtension($this->twigEnvironment, $this->quoteManager);
141
142
        $res = $obj->quoteFunction();
143
        $this->assertNotNull($res);
144
145
        $this->assertSame($res, $obj->quoteFunction("WorldsWisdom.fr"));
146
        $this->assertSame($res, $obj->quoteFunction());
147
    }
148
149
    /**
150
     * Test quoteFunction()
151
     *
152
     * @return void
153
     * @throws Throwable Throws an exception if an error occurs.
154
     */
155
    public function testQuoteFunctionWithBadDomain(): void {
156
157
        $obj = new QuoteTwigExtension($this->twigEnvironment, $this->quoteManager);
158
159
        $this->assertNull($obj->quoteFunction("github"));
160
    }
161
162
    /**
163
     * Test quoteFunction()
164
     *
165
     * @return void
166
     * @throws Throwable Throws an exception if an error occurs.
167
     */
168
    public function testQuoteFunctionWithoutProvider(): void {
169
170
        $obj = new QuoteTwigExtension($this->twigEnvironment, new QuoteManager($this->logger));
171
172
        $this->assertNull($obj->quoteFunction());
173
    }
174
175
    /**
176
     * Test __construct()
177
     *
178
     * @return void
179
     */
180
    public function test__construct(): void {
181
182
        $this->assertEquals("wbw.core.twig.extension.quote", QuoteTwigExtension::SERVICE_NAME);
183
184
        $obj = new QuoteTwigExtension($this->twigEnvironment, $this->quoteManager);
185
186
        $this->assertInstanceOf(ExtensionInterface::class, $obj);
187
188
        $this->assertSame($this->twigEnvironment, $obj->getTwigEnvironment());
189
        $this->assertSame($this->quoteManager, $obj->getQuoteManager());
190
    }
191
}
192