FunctionTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 93
rs 10
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testNonCallableFunction() 0 4 1
A testCallableFunctionJsName() 0 9 1
A tearDown() 0 4 1
A testCallableFunctionName() 0 9 1
A testCallableFunctionOptions() 0 9 1
A testPluginName() 0 3 1
A testPHPFunction() 0 4 1
A testCallableFunctionClass() 0 9 1
A testCallableFunctionJsCode() 0 6 1
1
<?php
2
3
namespace Jaxon\Tests\TestRegistrationApp;
4
5
require_once __DIR__ . '/../src/functions.php';
6
7
use Jaxon\Exception\SetupException;
8
use Jaxon\Jaxon;
9
use Jaxon\Plugin\Request\CallableFunction\CallableFunction;
10
use Jaxon\Plugin\Request\CallableFunction\CallableFunctionPlugin;
11
use PHPUnit\Framework\TestCase;
12
use function strlen;
13
14
final class FunctionTest extends TestCase
15
{
16
    /**
17
     * @var CallableFunctionPlugin
18
     */
19
    protected $xPlugin;
20
21
    /**
22
     * @throws SetupException
23
     */
24
    public function setUp(): void
25
    {
26
        jaxon()->app()->setup(__DIR__ . '/../config/app/functions.php');
27
28
        $this->xPlugin = jaxon()->di()->getCallableFunctionPlugin();
29
    }
30
31
    /**
32
     * @throws SetupException
33
     */
34
    public function tearDown(): void
35
    {
36
        jaxon()->reset();
37
        parent::tearDown();
38
    }
39
40
    public function testPluginName()
41
    {
42
        $this->assertEquals(Jaxon::CALLABLE_FUNCTION, $this->xPlugin->getName());
43
    }
44
45
    public function testPHPFunction()
46
    {
47
        // No callable for standard PHP functions.
48
        $this->assertEquals(null, $this->xPlugin->getCallable('file_get_contents'));
49
    }
50
51
    public function testNonCallableFunction()
52
    {
53
        // No callable for aliased functions.
54
        $this->assertEquals(null, $this->xPlugin->getCallable('my_second_function'));
55
    }
56
57
    public function testCallableFunctionClass()
58
    {
59
        $xFirstCallable = $this->xPlugin->getCallable('my_first_function');
60
        $xAliasCallable = $this->xPlugin->getCallable('my_alias_function');
61
        $xThirdCallable = $this->xPlugin->getCallable('my_third_function');
62
        // Test callables classes
63
        $this->assertEquals(CallableFunction::class, get_class($xFirstCallable));
0 ignored issues
show
Bug introduced by
It seems like $xFirstCallable can also be of type null; however, parameter $object of get_class() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
        $this->assertEquals(CallableFunction::class, get_class(/** @scrutinizer ignore-type */ $xFirstCallable));
Loading history...
64
        $this->assertEquals(CallableFunction::class, get_class($xAliasCallable));
65
        $this->assertEquals(CallableFunction::class, get_class($xThirdCallable));
66
    }
67
68
    public function testCallableFunctionName()
69
    {
70
        $xFirstCallable = $this->xPlugin->getCallable('my_first_function');
71
        $xAliasCallable = $this->xPlugin->getCallable('my_alias_function');
72
        $xThirdCallable = $this->xPlugin->getCallable('my_third_function');
73
        // Test callables classes
74
        $this->assertEquals('my_first_function', $xFirstCallable->getName());
75
        $this->assertEquals('my_alias_function', $xAliasCallable->getName());
76
        $this->assertEquals('my_third_function', $xThirdCallable->getName());
77
    }
78
79
    public function testCallableFunctionJsName()
80
    {
81
        $xFirstCallable = $this->xPlugin->getCallable('my_first_function');
82
        $xAliasCallable = $this->xPlugin->getCallable('my_alias_function');
83
        $xThirdCallable = $this->xPlugin->getCallable('my_third_function');
84
        // Test callables classes
85
        $this->assertEquals('jxn_my_first_function', $xFirstCallable->getJsName());
86
        $this->assertEquals('jxn_my_alias_function', $xAliasCallable->getJsName());
87
        $this->assertEquals('jxn_my_third_function', $xThirdCallable->getJsName());
88
    }
89
90
    public function testCallableFunctionOptions()
91
    {
92
        $xFirstCallable = $this->xPlugin->getCallable('my_first_function');
93
        $xAliasCallable = $this->xPlugin->getCallable('my_alias_function');
94
        $xThirdCallable = $this->xPlugin->getCallable('my_third_function');
95
        // Test callables classes
96
        $this->assertCount(0, $xFirstCallable->getOptions());
97
        $this->assertCount(1, $xAliasCallable->getOptions());
98
        $this->assertCount(0, $xThirdCallable->getOptions());
99
    }
100
101
    public function testCallableFunctionJsCode()
102
    {
103
        $this->assertEquals(32, strlen($this->xPlugin->getHash()));
104
        // $this->assertEquals('34608e208fda374f8761041969acf96e', $this->xPlugin->getHash());
105
        // file_put_contents(__DIR__ . '/../src/js/function.js', $this->xPlugin->getScript());
106
        $this->assertEquals(file_get_contents(__DIR__ . '/../src/js/function.js'), $this->xPlugin->getScript());
107
    }
108
}
109