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

FormTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 33
dl 0
loc 51
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A restoreFields() 0 4 2
A build() 0 10 1
A removeFields() 0 4 1
A test_condition_contains() 0 21 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Tests;
4
5
use GeminiLabs\SiteReviews\Modules\Html\Form;
6
7
/**
8
 * Test case for the Plugin.
9
 *
10
 * @group plugin
11
 */
12
class FormTest extends \WP_UnitTestCase
0 ignored issues
show
Bug introduced by
The type WP_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...
13
{
14
    protected $fields;
15
16
    public function test_condition_contains()
17
    {
18
        $this->assertEquals(
19
            '<div class="glsr-form-wrap">'.
20
                '<form class="glsr-form test-form" method="post" enctype="multipart/form-data">'.
21
                    '<div class="glsr-form-message"></div>'.
22
                    '<div data-field="submit-button">'.
23
                        '<div class="wp-block-buttons is-layout-flex">'.
24
                            '<div class="wp-block-button">'.
25
                                '<button type="submit" class="glsr-button wp-block-button__link wp-element-button" aria-busy="false" data-loading="Please wait">'.
26
                                    'Test'.
27
                                '</button>'.
28
                            '</div>'.
29
                        '</div>'.
30
                    '</div>'.
31
                '</form>'.
32
            '</div>',
33
            $this->build([
34
                'button_text' => 'Test',
35
                'button_text_loading' => 'Please wait',
36
                'class' => 'test-form',
37
            ])
38
        );
39
    }
40
41
    protected function build(array $args = []): string
42
    {
43
        $this->removeFields();
44
        $form = new Form($args);
45
        $html = $form->build();
46
        $this->restoreFields();
47
        $parts = preg_split('/\R/', $html);
48
        $parts = array_map('trim', $parts);
49
        $html = implode('', $parts);
50
        return $html;
51
    }
52
53
    protected function removeFields(): void
54
    {
55
        $this->fields = fn () => '';
56
        add_filter('site-reviews/form/build/fields', $this->fields, 10);
57
    }
58
59
    protected function restoreFields(): void
60
    {
61
        if ($this->fields) {
62
            remove_filter('site-reviews/form/build/fields', $this->fields, 10);
63
        }
64
    }
65
}
66