Completed
Push — develop ( 722f70...af048b )
by Jaap
15:12 queued 05:04
created

Classes/HasPackageWithSubpackageValidatorTest.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @author    Mike van Riel <[email protected]>
8
 * @copyright 2010-2012 Mike van Riel / Naenius (http://www.naenius.com)
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Plugin\Core\Descriptor\Validator\Constraints\Classes;
14
15
use Mockery\MockInterface;
16
use Mockery as m;
17
use phpDocumentor\Descriptor\FileDescriptor;
18
use phpDocumentor\Descriptor\Collection;
19
use Symfony\Component\Validator\ExecutionContextInterface;
20
21
/**
22
 * Test class for \phpDocumentor\Plugin\Core\Descriptor\Validator\Constraints\Classes\HasPackageWithSubpackageValidator.
23
 */
24
class HasPackageWithSubpackageValidatorTest extends \PHPUnit_Framework_TestCase
25
{
26
    /** @var HasPackageWithSubpackageValidator */
27
    protected $validator;
28
29
    /** @var HasPackageWithSubpackage */
30
    protected $constraint;
31
32
    /** @var MockInterface|FileDescriptor */
33
    protected $fileDescriptor;
34
35
    /** @var MockInterface|ExecutionContextInterface */
36
    protected $context;
37
38
    /**
39
     * Initializes the fixture and dependencies for this testcase.
40
     */
41
    public function setUp()
42
    {
43
        $this->constraint = new HasPackageWithSubpackage();
44
        $this->fileDescriptor = m::mock('phpDocumentor\Descriptor\FileDescriptor');
45
        $this->context = m::mock('Symfony\Component\Validator\ExecutionContextInterface');
46
47
        $this->validator = new HasPackageWithSubpackageValidator();
48
        $this->validator->initialize($this->context);
49
    }
50
51
    /**
52
     * @codingStandardsIgnoreStart
53
     * @expectedException Symfony\Component\Validator\Exception\ConstraintDefinitionException
54
     * @covers phpDocumentor\Plugin\Core\Descriptor\Validator\Constraints\Classes\HasPackageWithSubpackageValidator::validate
55
     * @codingStandardsIgnoreEnd
56
     */
57
    public function testValidateWithBadInput()
58
    {
59
        $this->validator->validate(new \stdClass(), $this->constraint);
0 ignored issues
show
new \stdClass() is of type object<stdClass>, but the function expects a object<phpDocumentor\Des...riptor\TraitDescriptor>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
    }
61
62
    /**
63
     * @codingStandardsIgnoreStart
64
     * @covers phpDocumentor\Plugin\Core\Descriptor\Validator\Constraints\Classes\HasPackageWithSubpackageValidator::validate
65
     * @codingStandardsIgnoreEnd
66
     */
67
    public function testValidateHappyPath()
68
    {
69
        $packageCollection = new Collection(array());
70
        $subpackageCollection = new Collection(array('x'));
71
        $tagPackageCollection = new Collection(
72
            array('package' => $packageCollection, 'subpackage' => $subpackageCollection)
73
        );
74
75
        $this->fileDescriptor->shouldReceive('getTags')->andReturn($tagPackageCollection)->twice();
0 ignored issues
show
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Descriptor\FileDescriptor.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
76
77
        $this->context->shouldReceive('addViolationAt')
78
		    ->once()
79
			->with('package', $this->constraint->message, array(), null, null, $this->constraint->code);
80
81
        $this->validator->validate($this->fileDescriptor, $this->constraint);
82
83
        $this->assertTrue(true);
84
    }
85
86
    /**
87
     * @codingStandardsIgnoreStart
88
     * @covers phpDocumentor\Plugin\Core\Descriptor\Validator\Constraints\Classes\HasPackageWithSubpackageValidator::validate
89
     * @codingStandardsIgnoreEnd
90
     */
91
    public function testValidateSubpackageWithPackage()
92
    {
93
        $packageCollection = new Collection(array('x'));
94
        $subpackageCollection = new Collection(array('y'));
95
        $tagPackageCollection = new Collection(
96
            array('package' => $packageCollection, 'subpackage' => $subpackageCollection)
97
        );
98
99
        $this->fileDescriptor->shouldReceive('getTags')->andReturn($tagPackageCollection)->twice();
0 ignored issues
show
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Descriptor\FileDescriptor.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
100
101
        $this->context->shouldReceive('addViolationAt')->never();
0 ignored issues
show
The method shouldReceive does only exist in Mockery\MockInterface, but not in Symfony\Component\Valida...ecutionContextInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
102
103
        $this->validator->validate($this->fileDescriptor, $this->constraint);
104
105
        $this->assertTrue(true);
106
    }
107
}
108