Passed
Branch develop (bae466)
by Paul
06:12
created

SqlTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 68
dl 0
loc 101
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A test_esc_values_for_insert() 0 5 1
A test_sql_where() 0 8 1
A test_esc_fields_for_insert() 0 5 1
A test_sql_join() 0 21 1
A test_sql_offset() 0 15 1
A test_sql_limit() 0 9 1
A test_sql_order_by() 0 21 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Tests;
4
5
use GeminiLabs\SiteReviews\Database\Query;
6
use GeminiLabs\SiteReviews\Database\Sql;
7
use GeminiLabs\SiteReviews\Defaults\ReviewsDefaults;
8
use WP_Ajax_UnitTestCase;
0 ignored issues
show
Bug introduced by
The type WP_Ajax_UnitTestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
class SqlTest extends WP_Ajax_UnitTestCase
11
{
12
    use Sql;
13
14
    public function test_esc_fields_for_insert()
15
    {
16
        $fields = ['field1', 'field2', 'field3'];
17
        $result = $this->escFieldsForInsert($fields);
18
        $this->assertSame("(`field1`,`field2`,`field3`)", $result);
19
    }
20
21
    public function test_esc_values_for_insert()
22
    {
23
        $values = ['value1', 'value2', "value'3"];
24
        $result = $this->escValuesForInsert($values);
25
        $this->assertSame("('value1','value2','value\\'3')", $result);
26
    }
27
28
    public function test_sql_join()
29
    {
30
        $query = glsr(Query::class);
31
        $postId = self::factory()->post->create();
32
        $termId = self::factory()->term->create(['taxonomy' => glsr()->taxonomy]);
33
        $userId = self::factory()->user->create();
34
        $query->setArgs([
35
            'assigned_posts' => $postId,
36
            'assigned_terms' => $termId,
37
            'assigned_users' => $userId,
38
        ]);
39
        $clauses = array_unique(array_filter($query->clauses('join')));
40
        $invoke = function (Query $obj, string $key) {
41
            $fn = fn () => $this->join($key, 'INNER JOIN');
42
            return $fn->bindTo($obj, $obj)();
43
        };
44
        $this->assertCount(4, $clauses);
45
        $this->assertContains($invoke($query, 'assigned_posts'), $clauses);
46
        $this->assertContains($invoke($query, 'assigned_terms'), $clauses);
47
        $this->assertContains($invoke($query, 'assigned_users'), $clauses);
48
        $this->assertContains($invoke($query, 'posts'), $clauses);
49
    }
50
51
    public function test_sql_limit()
52
    {
53
        $query = glsr(Query::class);
54
        $query->setArgs(['per_page' => -1]);
55
        $this->assertSame($query->sqlLimit(), '');
56
        $query->setArgs(['per_page' => 0]);
57
        $this->assertSame($query->sqlLimit(), '');
58
        $query->setArgs(['per_page' => 1]);
59
        $this->assertSame($query->sqlLimit(), 'LIMIT 1');
60
    }
61
62
    public function test_sql_offset()
63
    {
64
        $query = glsr(Query::class);
65
        $query->setArgs(['offset' => 3]);
66
        $this->assertSame($query->sqlOffset(), 'OFFSET 3');
67
        $query->setArgs(['page' => 2, 'per_page' => -1]);
68
        $this->assertSame($query->sqlOffset(), '');
69
        $query->setArgs(['page' => 2, 'per_page' => 0]);
70
        $this->assertSame($query->sqlOffset(), '');
71
        $query->setArgs(['page' => 1, 'per_page' => 5]);
72
        $this->assertSame($query->sqlOffset(), '');
73
        $query->setArgs(['page' => 2, 'per_page' => 5]);
74
        $this->assertSame($query->sqlOffset(), 'OFFSET 5');
75
        $query->setArgs(['page' => 2, 'per_page' => 5, 'offset' => 3]);
76
        $this->assertSame($query->sqlOffset(), 'OFFSET 8');
77
    }
78
79
    public function test_sql_order_by()
80
    {
81
        $query = glsr(Query::class);
82
        $expected = [
83
            'author' => 'ORDER BY r.is_pinned DESC, p.post_author DESC',
84
            'comment_count' => 'ORDER BY r.is_pinned DESC, p.comment_count DESC',
85
            'date' => 'ORDER BY r.is_pinned DESC, p.post_date DESC',
86
            'date_gmt' => 'ORDER BY r.is_pinned DESC, p.post_date_gmt DESC',
87
            'id' => 'ORDER BY r.is_pinned DESC, p.ID DESC',
88
            'menu_order' => 'ORDER BY r.is_pinned DESC, p.menu_order DESC',
89
            'none' => '',
90
            'random' => 'ORDER BY RAND()',
91
            'rating' => 'ORDER BY r.is_pinned DESC, r.rating DESC',
92
        ];
93
        foreach ($expected as $key => $value) {
94
            $query->setArgs(['orderby' => $key]);
95
            $this->assertSame($query->sqlOrderBy(), $value);
96
        }
97
        $enums = glsr(ReviewsDefaults::class)->enums['orderby'];
98
        $this->assertEmpty(
99
            array_filter($enums, fn ($key) => !array_key_exists($key, $expected))
100
        );
101
    }
102
103
    public function test_sql_where()
104
    {
105
        $query = glsr(Query::class);
106
        $this->assertSame($query->sqlWhere(), "WHERE 1=1");
107
        $query->setArgs([]);
108
        $this->assertSame($query->sqlWhere(), "WHERE 1=1 AND r.is_approved = 1");
109
        $query->setArgs(['status' => 'all']);
110
        $this->assertSame($query->sqlWhere(), "WHERE 1=1 AND p.post_status IN ('pending','publish')");
111
    }
112
}
113