Completed
Push — master ( f036f0...b950dc )
by Rafał
02:16
created

ContextTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 12.99 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 10
loc 77
rs 10
c 1
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testInitialization() 0 5 1
A testAddingNewConfiguration() 0 8 1
A testAddingNewMeta() 0 9 1
A testIfIsSupported() 0 8 1
A testInitializationWithDefaultConfigurations() 0 5 1
A testTemporaryUnsetAndRestore() 0 18 1
A tearDown() 10 10 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * This file is part of the Superdesk Web Publisher Templates System.
4
 *
5
 * Copyright 2015 Sourcefabric z.ú. and contributors.
6
 *
7
 * For the full copyright and license information, please see the
8
 * AUTHORS and LICENSE files distributed with this source code.
9
 *
10
 * @copyright 2015 Sourcefabric z.ú
11
 * @license http://www.superdesk.org/license
12
 */
13
14
namespace SWP\Component\TemplatesSystem\Tests\Gimme\Context;
15
16
use Doctrine\Common\Cache\ArrayCache;
17
use SWP\Component\TemplatesSystem\Tests\Article;
18
use SWP\Component\TemplatesSystem\Gimme\Context\Context;
19
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta;
20
21
class ContextTest extends \PHPUnit_Framework_TestCase
22
{
23
    /**
24
     * @var Context
25
     */
26
    protected $context;
27
28
    public function testInitialization()
29
    {
30
        $this->context = new Context(new ArrayCache());
31
        self::assertInstanceOf(Context::class, $this->context);
32
    }
33
34
    public function testInitializationWithDefaultConfigurations()
35
    {
36
        $this->context = new Context(new ArrayCache(), __DIR__.'/../../Twig/Node/Resources/meta/');
37
        self::assertCount(1, $this->context->getAvailableConfigs());
38
    }
39
40
    public function testAddingNewConfiguration()
41
    {
42
        $this->context = new Context(new ArrayCache());
43
        $configuration = $this->context->addNewConfig(__DIR__.'/../../Twig/Node/Resources/meta/article.yml');
44
45
        self::assertCount(1, $this->context->getAvailableConfigs());
46
        self::assertEquals($this->context->getAvailableConfigs()[Article::class], $configuration);
47
    }
48
49
    public function testAddingNewMeta()
50
    {
51
        $this->context = new Context(new ArrayCache(), __DIR__.'/../../Twig/Node/Resources/meta/');
52
        $meta = $this->context->getMetaForValue(new Article());
53
        $this->context->registerMeta($meta);
54
55
        self::assertInstanceOf(Meta::class, $this->context->article);
0 ignored issues
show
Bug introduced by
The property article does not seem to exist in SWP\Component\TemplatesS...m\Gimme\Context\Context.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
56
        self::assertCount(1, $this->context->getRegisteredMeta());
57
    }
58
59
    public function testIfIsSupported()
60
    {
61
        $this->context = new Context(new ArrayCache());
62
        self::assertFalse($this->context->isSupported(new Article()));
63
64
        $this->context->addNewConfig(__DIR__.'/../../Twig/Node/Resources/meta/article.yml');
65
        self::assertTrue($this->context->isSupported(new Article()));
66
    }
67
68
    public function testTemporaryUnsetAndRestore()
69
    {
70
        $this->context = new Context(new ArrayCache(), __DIR__.'/../../Twig/Node/Resources/meta/');
71
        $meta = $this->context->getMetaForValue(new Article());
72
        $this->context->registerMeta($meta);
73
74
        self::assertInstanceOf(Meta::class, $this->context->article);
0 ignored issues
show
Bug introduced by
The property article does not seem to exist in SWP\Component\TemplatesS...m\Gimme\Context\Context.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
75
        self::assertCount(1, $this->context->getRegisteredMeta());
76
77
        $key = $this->context->temporaryUnset(['article']);
78
        self::assertTrue(is_string($key));
79
        self::assertTrue(!isset($this->context['article']));
80
81
        $this->context->restoreTemporaryUnset($key);
82
        self::assertTrue(isset($this->context->article));
83
        self::assertInstanceOf(Meta::class, $this->context->article);
84
        self::assertCount(1, $this->context->getRegisteredMeta());
85
    }
86
87 View Code Duplication
    protected function tearDown()
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...
88
    {
89
        $reflection = new \ReflectionObject($this);
90
        foreach ($reflection->getProperties() as $prop) {
91
            if (!$prop->isStatic() && 0 !== strpos($prop->getDeclaringClass()->getName(), 'PHPUnit_')) {
0 ignored issues
show
introduced by
Consider using $prop->class. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
92
                $prop->setAccessible(true);
93
                $prop->setValue($this, null);
94
            }
95
        }
96
    }
97
}
98