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
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
21
|
|
|
|
22
|
|
|
class ContextTest extends \PHPUnit\Framework\TestCase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Context |
26
|
|
|
*/ |
27
|
|
|
protected $context; |
28
|
|
|
|
29
|
|
|
public function testInitialization() |
30
|
|
|
{ |
31
|
|
|
$this->context = new Context(new EventDispatcher(), new ArrayCache()); |
32
|
|
|
self::assertInstanceOf(Context::class, $this->context); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testInitializationWithDefaultConfigurations() |
36
|
|
|
{ |
37
|
|
|
$this->context = new Context(new EventDispatcher(), new ArrayCache(), __DIR__.'/../../Twig/Node/Resources/meta/'); |
38
|
|
|
self::assertCount(1, $this->context->getAvailableConfigs()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function testAddingNewConfiguration() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$this->context = new Context(new EventDispatcher(), new ArrayCache()); |
44
|
|
|
$configuration = $this->context->addNewConfig(__DIR__.'/../../Twig/Node/Resources/meta/article.yml'); |
45
|
|
|
|
46
|
|
|
self::assertCount(1, $this->context->getAvailableConfigs()); |
47
|
|
|
self::assertEquals($this->context->getAvailableConfigs()[Article::class], $configuration); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
public function testAddingNewMeta() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$this->context = new Context(new EventDispatcher(), new ArrayCache(), __DIR__.'/../../Twig/Node/Resources/meta/'); |
53
|
|
|
$meta = $this->context->getMetaForValue(new Article()); |
54
|
|
|
$this->context->registerMeta($meta); |
55
|
|
|
|
56
|
|
|
self::assertInstanceOf(Meta::class, $this->context->article); |
|
|
|
|
57
|
|
|
self::assertCount(1, $this->context->getRegisteredMeta()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testIfIsSupported() |
61
|
|
|
{ |
62
|
|
|
$this->context = new Context(new EventDispatcher(), new ArrayCache()); |
63
|
|
|
self::assertFalse($this->context->isSupported(new Article())); |
64
|
|
|
|
65
|
|
|
$this->context->addNewConfig(__DIR__.'/../../Twig/Node/Resources/meta/article.yml'); |
66
|
|
|
self::assertTrue($this->context->isSupported(new Article())); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testTemporaryUnsetAndRestore() |
70
|
|
|
{ |
71
|
|
|
$this->context = new Context(new EventDispatcher(), new ArrayCache(), __DIR__.'/../../Twig/Node/Resources/meta/'); |
72
|
|
|
$meta = $this->context->getMetaForValue(new Article()); |
73
|
|
|
$this->context->registerMeta($meta); |
74
|
|
|
|
75
|
|
|
self::assertInstanceOf(Meta::class, $this->context->article); |
|
|
|
|
76
|
|
|
self::assertCount(1, $this->context->getRegisteredMeta()); |
77
|
|
|
|
78
|
|
|
$key = $this->context->temporaryUnset(['article']); |
79
|
|
|
self::assertTrue(is_string($key)); |
80
|
|
|
self::assertTrue(!isset($this->context['article'])); |
81
|
|
|
|
82
|
|
|
$this->context->restoreTemporaryUnset($key); |
83
|
|
|
self::assertTrue(isset($this->context->article)); |
84
|
|
|
self::assertInstanceOf(Meta::class, $this->context->article); |
85
|
|
|
self::assertCount(1, $this->context->getRegisteredMeta()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
protected function tearDown() |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$reflection = new \ReflectionObject($this); |
91
|
|
|
foreach ($reflection->getProperties() as $prop) { |
92
|
|
|
if (!$prop->isStatic() && 0 !== strpos($prop->getDeclaringClass()->getName(), 'PHPUnit_')) { |
93
|
|
|
$prop->setAccessible(true); |
94
|
|
|
$prop->setValue($this, null); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
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.