Completed
Push — master ( 1c697f...6db46d )
by Frédéric G.
03:34
created

IG12913Test::withDicNoException()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
ccs 7
cts 8
cp 0.875
rs 9.4285
cc 2
eloc 8
nc 3
nop 2
crap 2.0078
1
<?php
2
3
/**
4
 * @file
5
 * Grafizzi\Graph\Tests\IG12913Test: a component of the Grafizzi library.
6
 *
7
 * (c) 2012 Frédéric G. MARAND <[email protected]>
8
 *
9
 * Grafizzi is free software: you can redistribute it and/or modify it under the
10
 * terms of the GNU Lesser General Public License as published by the Free
11
 * Software Foundation, either version 3 of the License, or (at your option) any
12
 * later version.
13
 *
14
 * Grafizzi is distributed in the hope that it will be useful, but WITHOUT ANY
15
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16
 * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
17
 * details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with Grafizzi, in the COPYING.LESSER.txt file.  If not, see
21
 * <http://www.gnu.org/licenses/>
22
 */
23
24
namespace Grafizzi\Graph\Tests;
25
26
use Grafizzi\Graph\Attribute;
27
use Grafizzi\Graph\Cluster;
28
use Grafizzi\Graph\Filter\DotFilter;
29
use Grafizzi\Graph\Filter\FilterInterface;
30
use Grafizzi\Graph\Node;
31
use Pimple\Container;
32
33
require 'vendor/autoload.php';
34
35
/**
36
 * A recreation of Image_GraphViz bug_12913.phpt
37
 *
38
 * Image_GraphViz version author: Philippe Jausions <[email protected]>
39
 *
40
 * Request 12913: "PEAR_Error on failure"
41
 *
42
 * Since Grafizzi is not a PEAR component, it can throw exceptions instead of
43
 * using PEAR errors, so the test applies to exceptions.
44
 */
45
class IG12913Test extends BaseGraphTest {
46
47
  /**
48
   *
49
   * @var \Grafizzi\Graph\Graph
50
   */
51
  public $Graph2;
52
53
  /**
54
   * @var \Pimple\Container
55
   */
56
  public $dic2;
57
58 1
  public function setUp($name = 'G', $attributes = array()) {
59 1
    parent::setUp('G');
60 1
    $this->Graph2 = $this->Graph;
61 1
    $this->dic2 = $this->dic;
62 1
    $this->dic2['use_exceptions'] = false;
63 1
    unset($this->dic, $this->Graph);
64
65 1
    parent::setUp('G');
66 1
    $this->dic['use_exceptions'] = true;
67
68 1
    $this->Graph->addChild($cluster1 = new Cluster($this->dic, 1));
69 1
    $cluster1->addChild($node1 = new Node($this->dic, 'Node1', array(
70 1
      new Attribute($this->dic, 'label', 'Node1'),
71 1
    )));
72
73 1
    $this->Graph2->addChild($cluster2 = new Cluster($this->dic2, 2));
74 1
    $cluster2->addChild($node2 = new Node($this->dic2, 'Node2', array(
75 1
      new Attribute($this->dic2, 'label', 'Node2'),
76 1
    )));
77 1
  }
78
79
  /**
80
   * @param FilterInterface $filter
81
   * @param string $format
82
   */
83 1 View Code Duplication
  protected function withDicException(FilterInterface $filter, $format) {
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...
84 1
    $dic = new Container(array('use_exceptions' => true));
85 1
    $filter->setDic($dic);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Grafizzi\Graph\Filter\FilterInterface as the method setDic() does only exist in the following implementations of said interface: Grafizzi\Graph\Filter\DotFilter.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
86
    try {
87 1
      $filter->image($format);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Grafizzi\Graph\Filter\FilterInterface as the method image() does only exist in the following implementations of said interface: Grafizzi\Graph\Filter\DotFilter.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
88
      $this->fail('Invalid format image did not throw an exception.');
89
    }
90 1
    catch (\InvalidArgumentException $e) {
91 1
      $this->assertInstanceOf('InvalidArgumentException', $e, 'Invalid argument for invalid format.');
92
    }
93 1
  }
94
95
  /**
96
   * @param FilterInterface $filter
97
   * @param string $format
98
   */
99 1 View Code Duplication
  protected function withDicNoException(FilterInterface $filter, $format) {
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...
100 1
    $dic = new Container(array('use_exceptions' => false));
101 1
    $filter->setDic($dic);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Grafizzi\Graph\Filter\FilterInterface as the method setDic() does only exist in the following implementations of said interface: Grafizzi\Graph\Filter\DotFilter.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
102
    try {
103 1
      $result = $filter->image($format);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Grafizzi\Graph\Filter\FilterInterface as the method image() does only exist in the following implementations of said interface: Grafizzi\Graph\Filter\DotFilter.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
104 1
      $this->assertFalse($result, 'Unavailable format image.');
105
    }
106 1
    catch (\InvalidArgumentException $e) {
107
      $this->fail('Invalid format image threw an exception.');
108 1
    }  }
109
110
  /**
111
   * Tests Graph->image()
112
   */
113 1
  public function testImage() {
114 1
    $dotFilter = new DotFilter();
115 1
    $format = DotFilterTest::INVALID_FORMAT;
116 1
    $this->withDicException($dotFilter, $format);
117 1
    $this->withDicNoException($dotFilter, $format);
118 1
  }
119
}
120