Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#28)
by Jérémiah
13:39
created

TypeResolverTest::testResolveUsingSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the OverblogGraphQLBundle package.
5
 *
6
 * (c) Overblog <http://github.com/overblog/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Overblog\GraphQLBundle\Tests\Resolver;
13
14
use GraphQL\Type\Definition\ObjectType;
15
use Overblog\GraphQLBundle\Resolver\TypeResolver;
16
use Overblog\GraphQLBundle\Tests\Request\Validator\Rule\Schema;
17
18
class TypeResolverTest extends AbstractResolverTest
19
{
20
    protected function createResolver()
21
    {
22
        return new TypeResolver();
23
    }
24
25
    protected function getResolverSolutionsMapping()
26
    {
27
        return [
28
            'Toto' => ['solution' => new ObjectType(['name' => 'Toto'])],
29
            'Tata' => ['solution' => new ObjectType(['name' => 'Tata'])],
30
        ];
31
    }
32
33
    /**
34
     * @expectedException \Overblog\GraphQLBundle\Resolver\UnsupportedResolverException
35
     * @expectedExceptionMessage Resolver "not-supported" must be "GraphQL\Type\Definition\Type" "stdClass" given.
36
     */
37
    public function testAddNotSupportedSolution()
38
    {
39
        $this->resolver->addSolution('not-supported', new \stdClass());
40
    }
41
42
    public function testResolveKnownType()
43
    {
44
        $type = $this->resolver->resolve('Toto');
45
46
        $this->assertInstanceOf('GraphQL\Type\Definition\ObjectType', $type);
47
        $this->assertEquals('Toto', $type->name);
48
    }
49
50
    /**
51
     * @expectedException \Overblog\GraphQLBundle\Resolver\UnresolvableException
52
     */
53
    public function testResolveUnknownType()
54
    {
55
        $this->resolver->resolve('Fake');
56
    }
57
58
    /**
59
     * @expectedException \Overblog\GraphQLBundle\Resolver\UnresolvableException
60
     */
61
    public function testWrongListOfWrappingType()
62
    {
63
        $this->resolver->resolve('[Tata');
64
    }
65
66
    public function testResolveWithListOfWrapper()
67
    {
68
        /** @var \GraphQL\Type\Definition\WrappingType $type */
69
        $type = $this->resolver->resolve('[Tata]');
70
71
        $this->assertInstanceOf('GraphQL\Type\Definition\ListOfType', $type);
72
        $this->assertEquals('Tata', $type->getWrappedType());
73
    }
74
75
    public function testResolveWithNonNullWrapper()
76
    {
77
        /** @var \GraphQL\Type\Definition\WrappingType $type */
78
        $type = $this->resolver->resolve('Toto!');
79
80
        $this->assertInstanceOf('GraphQL\Type\Definition\NonNull', $type);
81
        $this->assertEquals('Toto', $type->getWrappedType());
82
    }
83
84 View Code Duplication
    public function testResolveWithNonNullListOfWrapper()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        /** @var \GraphQL\Type\Definition\WrappingType $type */
87
        $type = $this->resolver->resolve('[Toto]!');
88
89
        $this->assertInstanceOf('GraphQL\Type\Definition\NonNull', $type);
90
        $this->assertInstanceOf('GraphQL\Type\Definition\ListOfType', $type->getWrappedType());
91
        $this->assertEquals('Toto', $type->getWrappedType()->getWrappedType());
92
    }
93
94 View Code Duplication
    public function testResolveWitListOfNonNullWrapper()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
    {
96
        /** @var \GraphQL\Type\Definition\WrappingType $type */
97
        $type = $this->resolver->resolve('[Toto!]');
98
99
        $this->assertInstanceOf('GraphQL\Type\Definition\ListOfType', $type);
100
        $this->assertInstanceOf('GraphQL\Type\Definition\NonNull', $type->getWrappedType());
101
        $this->assertEquals('Toto', $type->getWrappedType()->getWrappedType());
102
    }
103
104
    public function testResolveWitNonNullListOfNonNullWrapper()
105
    {
106
        /** @var \GraphQL\Type\Definition\WrappingType $type */
107
        $type = $this->resolver->resolve('[Toto!]!');
108
109
        $this->assertInstanceOf('GraphQL\Type\Definition\NonNull', $type);
110
        $this->assertInstanceOf('GraphQL\Type\Definition\ListOfType', $type->getWrappedType());
111
        $this->assertInstanceOf('GraphQL\Type\Definition\NonNull', $type->getWrappedType()->getWrappedType());
112
        $this->assertEquals('Toto', $type->getWrappedType()->getWrappedType()->getWrappedType());
113
    }
114
115 View Code Duplication
    public function testResolveWitListOfListOfWrapper()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
    {
117
        /** @var \GraphQL\Type\Definition\WrappingType $type */
118
        $type = $this->resolver->resolve('[[Toto]]');
119
120
        $this->assertInstanceOf('GraphQL\Type\Definition\ListOfType', $type);
121
        $this->assertInstanceOf('GraphQL\Type\Definition\ListOfType', $type->getWrappedType());
122
        $this->assertEquals('Toto', $type->getWrappedType()->getWrappedType());
123
    }
124
125
    public function testResolveUsingSchema()
126
    {
127
        $schema = Schema::buildSchema();
128
129
        $this->resolver->setSchema($schema);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Overblog\GraphQLBundle\Resolver\AbstractResolver as the method setSchema() does only exist in the following sub-classes of Overblog\GraphQLBundle\Resolver\AbstractResolver: Overblog\GraphQLBundle\Resolver\TypeResolver. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
130
131
        $this->assertEquals($schema->getType('Human'), $this->resolver->resolve('Human'));
132
    }
133
}
134