Completed
Push — master ( 600451...ae1e12 )
by
unknown
13s
created

AddElementToAreaMutationTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testAddingBlocksInOrder() 0 22 1
A testOrderingByWrongElementalArea() 0 8 1
A assertIdsInOrder() 0 6 1
A runMutation() 0 16 2
A testBadElementalArea() 0 4 1
1
<?php
2
3
namespace DNADesign\Elemental\Tests\GraphQL;
4
5
use DNADesign\Elemental\GraphQL\AddElementToAreaMutation;
6
use DNADesign\Elemental\Models\ElementalArea;
7
use DNADesign\Elemental\Tests\Src\TestElement;
8
use GraphQL\Type\Definition\ResolveInfo;
0 ignored issues
show
Bug introduced by Dylan Wagstaff
The type GraphQL\Type\Definition\ResolveInfo 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
use InvalidArgumentException;
10
use SilverStripe\Dev\SapphireTest;
11
use SilverStripe\Security\Security;
12
13
class AddElementToAreaMutationTest extends SapphireTest
14
{
15
    protected static $fixture_file = 'AddElementToAreaMutationTest.yml';
16
17
    protected static $extra_dataobjects = [
18
        TestElement::class,
19
    ];
20
21
    public function testAddingBlocksInOrder()
22
    {
23
        $className = TestElement::class;
24
        $areaID = $this->objFromFixture(ElementalArea::class, 'one')->ID;
25
26
        $one = $this->runMutation($className, $areaID)->ID;
27
        $this->assertIdsInOrder([$one], 'The first element is added');
28
29
        $two = $this->runMutation($className, $areaID, $one)->ID;
30
        $this->assertIdsInOrder([$one, $two], 'The second element is added after the first');
31
32
        $three = $this->runMutation($className, $areaID, $one)->ID;
33
        $this->assertIdsInOrder([$one, $three, $two], 'The third element is added after the first');
34
35
        $four = $this->runMutation($className, $areaID)->ID;
36
        $this->assertIdsInOrder(
37
            [$one, $three, $two, $four],
38
            'The fourth element is added last, when no after ID parameter is given'
39
        );
40
41
        $five = $this->runMutation($className, $areaID, 0)->ID;
42
        $this->assertIdsInOrder([$five, $one, $three, $two, $four], 'The fifth element is added first, after ID 0');
43
    }
44
45
    /**
46
     * @expectedException InvalidArgumentException
47
     */
48
    public function testBadElementalArea()
49
    {
50
        $areaID = $this->objFromFixture(ElementalArea::class, 'one')->ID;
51
        $this->runMutation(TestElement::class, $areaID + 1);
52
    }
53
54
    /**
55
     * @expectedException InvalidArgumentException
56
     */
57
    public function testOrderingByWrongElementalArea()
58
    {
59
        $firstArea = ElementalArea::get()->first();
60
        $elementInFirstArea = TestElement::create();
61
        $firstArea->Elements()->add($elementInFirstArea);
62
63
        ElementalArea::create()->write();
64
        $this->runMutation(TestElement::class, 2, $elementInFirstArea->ID);
65
    }
66
67
    protected function assertIdsInOrder($ids, $message = null)
68
    {
69
        $actualIDs = TestElement::get()->sort('Sort')->map()->keys();
70
        // Ideally this should be assertSame, but sometimes the database
71
        // returns IDs as strings instead of integers (e.g. "1" instead of 1).
72
        $this->assertEquals($ids, $actualIDs, $message);
73
    }
74
75
    protected function runMutation($className, $elementalAreaID, $afterElementId = null)
76
    {
77
        $mutation = new AddElementToAreaMutation();
78
        $context = ['currentUser' => Security::getCurrentUser()];
79
        $resolveInfo = new ResolveInfo([]);
80
81
        $args = [
82
            'ClassName' => $className,
83
            'ElementalAreaID' => $elementalAreaID,
84
        ];
85
86
        if (!is_null($afterElementId)) {
87
            $args['AfterElementID'] = $afterElementId;
88
        }
89
90
        return $mutation->resolve(null, $args, $context, $resolveInfo);
91
    }
92
}
93