Passed
Pull Request — master (#429)
by
unknown
03:35
created

AddElementToAreaMutationTest::runMutation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 3
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
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
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
12
class AddElementToAreaMutationTest extends SapphireTest
13
{
14
    protected static $fixture_file = 'AddElementToAreaMutationTest.yml';
15
16
    protected static $extra_dataobjects = [
17
        TestElement::class,
18
    ];
19
20
    public function testAddingBlocksInOrder()
21
    {
22
        $className = TestElement::class;
23
24
        $this->runMutation($className, 1);
25
        $this->assertIdsInOrder([1], 'The first element is added');
26
27
        $this->runMutation($className, 1, 1);
28
        $this->assertIdsInOrder([1, 2], 'The second element is added after the first');
29
30
        $this->runMutation($className, 1, 1);
31
        $this->assertIdsInOrder([1, 3, 2], 'The third element is added after the first');
32
33
        $this->runMutation($className, 1);
34
        $this->assertIdsInOrder([1, 3, 2, 4], 'The fourth element is added last, when no after ID parameter is given');
35
36
        $this->runMutation($className, 1, 0);
37
        $this->assertIdsInOrder([5, 1, 3, 2, 4], 'The fifth element is added first, after ID 0');
38
    }
39
40
    /**
41
     * @expectException InvalidArgumentException
42
     */
43
    public function testBadElementalArea()
44
    {
45
        $this->runMutation(TestElement::class, 2);
46
    }
47
48
    /**
49
     * @expectException InvalidArgumentException
50
     */
51
    public function testOrderingByWrongElementalArea()
52
    {
53
        $firstArea = ElementalArea::get()->first();
54
        $elementInFirstArea = TestElement::create();
55
        $firstArea->Elements()->add($elementInFirstArea);
56
57
        ElementalArea::create()->write();
58
        $this->expectException(InvalidArgumentException::class);
59
        $this->runMutation(TestElement::class, 2, $elementInFirstArea->ID);
60
    }
61
62
    protected function assertIdsInOrder($ids, $message = null)
63
    {
64
        $actualIDs = TestElement::get()->sort('Sort')->map()->keys();
65
66
        $this->assertSame($ids, $actualIDs, $message);
67
    }
68
69
    protected function runMutation($className, $elementalAreaID, $afterElementId = null)
70
    {
71
        $mutation = new AddElementToAreaMutation();
72
        $context = ['currentUser' => Security::getCurrentUser()];
0 ignored issues
show
Bug introduced by
The type DNADesign\Elemental\Tests\GraphQL\Security was not found. Did you mean Security? If so, make sure to prefix the type with \.
Loading history...
73
        $resolveInfo = new ResolveInfo([]);
74
75
        $args = [
76
            'ClassName' => $className,
77
            'ElementalAreaID' => $elementalAreaID,
78
        ];
79
80
        if (!is_null($afterElementId)) {
81
            $args['AfterElementID'] = $afterElementId;
82
        }
83
84
        $mutation->resolve(null, $args, $context, $resolveInfo);
85
    }
86
}
87