Passed
Push — master ( 5114b2...365bb8 )
by Jitendra
01:28
created

Asserts::assertAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the ASSERTS package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace Ahc\Asserts;
13
14
trait Asserts
15
{
16
    const DEFAULT_PRECISION = 6;
17
18
    public function assertJsonSubset($expected, $actual, string $message = null)
19
    {
20
        $actual   = \json_encode($actual);
21
        $expected = \json_encode($expected);
22
        $expected = \trim($expected, '{}');
23
24
        $this->assertContains($expected, $actual, "$message$actual doesnot contain $expected");
0 ignored issues
show
Bug introduced by
It seems like assertContains() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

24
        $this->/** @scrutinizer ignore-call */ 
25
               assertContains($expected, $actual, "$message$actual doesnot contain $expected");
Loading history...
25
    }
26
27
    public function assertJsonSubsets($expected, /* more expected ...*/ $actual)
28
    {
29
        $expected = \func_get_args();
30
        $actual   = \array_pop($expected);
31
32
        foreach ($expected as $i => $expect) {
33
            $this->assertJsonSubset($expect, $actual, "Data set #$i: ");
34
        }
35
    }
36
37
    public function assertFloatEquals(float $expected, float $actual, int $precision = null, string $message = null)
38
    {
39
        $precision = $precision ?? static::DEFAULT_PRECISION;
0 ignored issues
show
Bug introduced by
The constant Ahc\Asserts\Asserts::DEFAULT_PRECISION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
40
41
        if (\function_exists('bccomp')) {
42
            $this->assertSame(0, \bccomp($expected, $actual, $precision), $message);
0 ignored issues
show
Bug introduced by
The method assertSame() does not exist on Ahc\Asserts\Asserts. Did you maybe mean assertAll()? ( Ignorable by Annotation )

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

42
            $this->/** @scrutinizer ignore-call */ 
43
                   assertSame(0, \bccomp($expected, $actual, $precision), $message);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
44
            return;
45
        }
46
47
        $expected  = \round($expected, $precision);
48
        $actual    = \round($actual, $precision);
49
50
        $this->assertEquals($expected, $actual, $message, \pow(10, 0 - \abs($precision)));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not exist on Ahc\Asserts\Asserts. Did you maybe mean assertFloatEquals()? ( Ignorable by Annotation )

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

50
        $this->/** @scrutinizer ignore-call */ 
51
               assertEquals($expected, $actual, $message, \pow(10, 0 - \abs($precision)));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
    }
52
53
    public function assertArrayHasKeys(array $expectedKeys, array $actualArray, string $message = null)
54
    {
55
        foreach ($expectedKeys as $key) {
56
            $this->assertArrayHasKey($key, $actualArray, $message);
0 ignored issues
show
Bug introduced by
The method assertArrayHasKey() does not exist on Ahc\Asserts\Asserts. Did you maybe mean assertArrayHasKeys()? ( Ignorable by Annotation )

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

56
            $this->/** @scrutinizer ignore-call */ 
57
                   assertArrayHasKey($key, $actualArray, $message);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
        }
58
    }
59
60
    public function assertAll($actual, array $assertions, array $messages = [])
61
    {
62
        foreach ($assertions as $assert => $expected) {
63
            $this->{$assert}($expected, $actual, $messages[$assert] ?? null);
64
        }
65
    }
66
}
67