File: FinderUtilsTest.php

package info (click to toggle)
php-arthurhoaro-web-thumbnailer 2.1.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 892 kB
  • sloc: php: 3,365; makefile: 19; sh: 9
file content (86 lines) | stat: -rw-r--r-- 2,637 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php

declare(strict_types=1);

namespace WebThumbnailer\Utils;

use WebThumbnailer\TestCase;

class FinderUtilsTest extends TestCase
{
    /**
     * Test buildRegex() function.
     */
    public function testBuildRegex(): void
    {
        $regex = 'regex*';
        $flags = 'im';
        $formatted = '{regex*}';
        $res = FinderUtils::buildRegex($regex, $flags);
        $this->assertEquals($formatted . $flags, $res);
        $res = FinderUtils::buildRegex($regex, '');
        $this->assertEquals($formatted, $res);
        $res = FinderUtils::buildRegex('', $flags);
        $this->assertEquals('{}' . $flags, $res);
    }

    /**
     * Test checkMandatoryRules() with valid data.
     */
    public function testCheckMandatoryRulesSimple(): void
    {
        $this->assertTrue(FinderUtils::checkMandatoryRules([], []));
        $this->assertTrue(FinderUtils::checkMandatoryRules(['data'], []));
        $this->assertTrue(FinderUtils::checkMandatoryRules(['data' => 'value'], ['data']));
        $this->assertTrue(FinderUtils::checkMandatoryRules(['data' => false], ['data']));
        $this->assertTrue(FinderUtils::checkMandatoryRules(['data' => '', 'other' => 'value'], ['data']));
    }

    /**
     * Test checkMandatoryRules() with valid data and nested mandatory rules.
     */
    public function testCheckMandatoryRulesNested(): void
    {
        $rules = [
            'foo' => 'bar',
            'foobar' => [
                'nested' => 'rule',
            ]
        ];
        $mandatory = [
            'foo',
            'foobar' => ['nested']
        ];
        $this->assertTrue(FinderUtils::checkMandatoryRules($rules, $mandatory));
    }

    /**
     * Test checkMandatoryRules() with invalid data.
     */
    public function testCheckMandatoryRulesInvalidSimple(): void
    {
        $this->assertFalse(FinderUtils::checkMandatoryRules([], ['rule']));
        $this->assertFalse(FinderUtils::checkMandatoryRules(['rule' => ''], ['rule', 'other']));
        $this->assertFalse(FinderUtils::checkMandatoryRules(['other' => 'value'], ['rule']));
    }

    /**
     * Test checkMandatoryRules() with invalid data and nested mandatory rules.
     */
    public function testCheckMandatoryRulesInvalidNested(): void
    {
        $rules = [
            'foo' => 'bar',
            'foobar' => [
                'nested' => [
                    'missing' => 'rule',
                ]
            ]
        ];
        $mandatory = [
            'foo',
            'foobar' => ['nested' => ['nope']]
        ];
        $this->assertFalse(FinderUtils::checkMandatoryRules($rules, $mandatory));
    }
}