SoapSearchApiTest::testSearch()   D
last analyzed

Complexity

Conditions 10
Paths 288

Size

Total Lines 50
Code Lines 29

Duplication

Lines 10
Ratio 20 %
Metric Value
dl 10
loc 50
rs 4
cc 10
eloc 29
nc 288
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Oro\Bundle\SearchBundle\Tests\Functional\Controller\Api;
4
5
use Oro\Bundle\TestFrameworkBundle\Test\WebTestCase;
6
7
/**
8
 * @outputBuffering enabled
9
 * @dbIsolation
10
 * @dbReindex
11
 */
12
class SoapSearchApiTest extends WebTestCase
13
{
14
    /** Default value for offset and max_records */
15
    const DEFAULT_VALUE = 0;
16
17
    protected function setUp()
18
    {
19
        $this->initClient([], $this->generateWsseAuthHeader());
20
        $this->initSoapClient();
21
22
        $this->loadFixtures(['Oro\Bundle\SearchBundle\Tests\Functional\Controller\DataFixtures\LoadSearchItemData']);
23
    }
24
25
    /**
26
     * @param array $request
27
     * @param array $response
28
     * @SuppressWarnings(PHPMD.NPathComplexity)
29
     * @dataProvider searchDataProvider
30
     */
31
    public function testSearch(array $request, array $response)
32
    {
33 View Code Duplication
        if (array_key_exists('supported_engines', $request)) {
34
            $engine = $this->getContainer()->getParameter('oro_search.engine');
35
            if (!in_array($engine, $request['supported_engines'])) {
36
                $this->markTestIncomplete('Test should not be executed on this engine');
37
            }
38
            unset($request['supported_engines']);
39
        }
40
41
        if (is_null($request['search'])) {
42
            $request['search'] ='';
43
        }
44
        if (is_null($request['offset'])) {
45
            $request['offset'] = self::DEFAULT_VALUE;
46
        }
47
        if (is_null($request['max_results'])) {
48
            $request['max_results'] = self::DEFAULT_VALUE;
49
        }
50
51
        $result = $this->soapClient->search(
0 ignored issues
show
Documentation Bug introduced by
The method search does not exist on object<Oro\Bundle\TestFr...Bundle\Test\SoapClient>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
52
            $request['search'],
53
            $request['offset'],
54
            $request['max_results']
55
        );
56
        $result = $this->valueToArray($result);
57
58
        $this->assertEquals($response['records_count'], $result['recordsCount']);
59
        $this->assertEquals($response['count'], $result['count']);
60
61
        if (empty($result['elements']['item'])) {
62
            $result['elements']['item'] = [];
63
        }
64
65
        // if only one element
66 View Code Duplication
        if (empty($result['elements']['item'][0])) {
67
            $result['elements']['item'] = [$result['elements']['item']];
68
        }
69
70
        // remove ID references
71
        $recordsRequired = !empty($response['soap']['item'][0]['recordTitle']);
72
        foreach (array_keys($result['elements']['item']) as $key) {
73
            unset($result['elements']['item'][$key]['recordId']);
74
            if (!$recordsRequired) {
75
                unset($result['elements']['item'][$key]['recordTitle']);
76
            }
77
        }
78
79
        $this->assertResultHasItems($response['soap']['item'], $result['elements']['item']);
80
    }
81
82
    /**
83
     * Data provider for SOAP API tests
84
     *
85
     * @return array
86
     */
87
    public function searchDataProvider()
88
    {
89
        return $this->getApiRequestsData(
90
            __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'requests'
91
        );
92
    }
93
94
    /**
95
     * @param array $items
96
     * @param array $result
97
     */
98
    protected function assertResultHasItems(array $items, array $result)
99
    {
100
        foreach ($items as $item) {
101
            $this->assertContains($item, $result);
102
        }
103
    }
104
}
105