Passed
Pull Request — master (#7641)
by
unknown
12:21
created

SelectSqlMappingTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testTypeFromMathematicNodeFunction() 0 16 1
A setUp() 0 3 1
A assertScalarTypes() 0 25 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Query;
6
7
use Doctrine\DBAL\Types\DateTimeType;
0 ignored issues
show
introduced by
Type Doctrine\DBAL\Types\DateTimeType is not used in this file.
Loading history...
8
use Doctrine\DBAL\Types\FloatType;
9
use Doctrine\DBAL\Types\IntegerType;
10
use Doctrine\ORM\Query as ORMQuery;
11
use Doctrine\Tests\Models\CMS\CmsProduct;
12
use Doctrine\Tests\OrmTestCase;
13
use Exception;
14
use function get_class;
0 ignored issues
show
introduced by
Type get_class is not used in this file.
Loading history...
15
16
class SelectSqlMappingTest extends OrmTestCase
17
{
18
    private $em;
19
20
    protected function setUp() : void
21
    {
22
        $this->em = $this->getTestEntityManager();
23
    }
24
25
    /**
26
     * Assert a valid SQL generation.
27
     *
28
     * @param string $dqlToBeTested
29
     * @param array $types
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
30
     */
31
    public function assertScalarTypes($dqlToBeTested, array $types)
32
    {
33
        try {
34
            $query = $this->em->createQuery($dqlToBeTested);
35
36
            $query
37
                ->setHint(ORMQuery::HINT_FORCE_PARTIAL_LOAD, true)
38
                ->useQueryCache(false);
39
40
            $r = new \ReflectionObject($query);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
introduced by
Class \ReflectionObject should not be referenced via a fully qualified name, but via a use statement.
Loading history...
41
            $method = $r->getMethod('getResultSetMapping');
42
            $method->setAccessible(true);
43
            /** @var ORMQuery\ResultSetMapping $mapping */
44
            $mapping = $method->invoke($query);
45
            foreach($types as $key => $expectedType) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after FOREACH keyword; 0 found
Loading history...
46
                $alias = array_search($key, $mapping->scalarMappings);
0 ignored issues
show
introduced by
Function array_search() should not be referenced via a fallback global name, but via a use statement.
Loading history...
47
                self::assertInstanceOf(
48
                    $expectedType,
49
                    $mapping->typeMappings[$alias],
50
                    "The field \"$key\" was expected as a $expectedType"
0 ignored issues
show
Coding Style Best Practice introduced by
Variable "%s" not allowed in double quoted string; use sprintf() or concatenation instead

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
51
                );
52
            }
53
            $query->free();
54
        } catch (Exception $e) {
55
            $this->fail($e->getMessage() . "\n" . $e->getTraceAsString());
56
        }
57
58
    }
0 ignored issues
show
Coding Style introduced by
Function closing brace must go on the next line following the body; found 1 blank lines before brace
Loading history...
59
60
    /**
61
     * @group DDC-2235
62
     */
63
    public function testTypeFromMathematicNodeFunction() : void
64
    {
65
        $entity = CmsProduct::class;
66
        $this->assertScalarTypes("SELECT p, 
0 ignored issues
show
Coding Style Best Practice introduced by
Variable "%s" not allowed in double quoted string; use sprintf() or concatenation instead

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
67
          count(p.id) as count, 
68
          SUM(p.price) as sales, 
69
          AVG(p.price) as average, 
70
          ABS(p.price) as absolute,
71
          LENGTH(p.name) as length
72
          FROM {$entity} p",
73
            [
74
                'count' => IntegerType::class,
75
                'sales' => FloatType::class,
76
                'average' => FloatType::class,
77
                'absolute' => FloatType::class,
78
                'length' => IntegerType::class
0 ignored issues
show
introduced by
MultiLine arrays must have a trailing comma after the last element.
Loading history...
79
            ]
80
        );
81
    }
82
}
83
84