ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
IdentificationFactoryTest.php
Go to the documentation of this file.
1<?php
2
4
12use ILIAS\GlobalScreen\Provider\StaticProvider\StaticMainMenuProvider;
13use ilPlugin;
14use InvalidArgumentException;
15use Mockery;
16use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
17use PHPUnit\Framework\TestCase;
18use ReflectionClass;
19use ReflectionMethod;
20use Serializable;
21
22require_once('./libs/composer/vendor/autoload.php');
23
34class IdentificationFactoryTest extends TestCase
35{
36 use MockeryPHPUnitIntegration;
37 const MOCKED_PROVIDER_CLASSNAME = 'Mockery_1_ILIAS_GlobalScreen_Provider_Provider';
45 protected $provider_mock;
49 protected $plugin_mock;
53 protected $identification;
54
55
59 protected function setUp() : void
60 {
61 parent::setUp();
62
63 $this->plugin_mock = Mockery::mock(ilPlugin::class);
64
65 $this->provider_mock = Mockery::mock(Provider::class);
66 $this->provider_mock->shouldReceive('getProviderNameForPresentation')->andReturn('Provider')->byDefault();
67
68 $this->provider_factory = Mockery::mock(ProviderFactory::class);
69 $this->provider_factory->shouldReceive('getProviderByClassName')->with(self::MOCKED_PROVIDER_CLASSNAME)->andReturn($this->provider_mock);
70 $this->provider_factory->shouldReceive('isInstanceCreationPossible')->andReturn(true);
71 $this->provider_factory->shouldReceive('isRegistered')->andReturn(true);
72
73 $this->identification = new IdentificationFactory($this->provider_factory);
74 }
75
76
77 public function testAvailableMethods()
78 {
79 $r = new ReflectionClass($this->identification);
80
81 $methods = [];
82 foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
83 $methods[] = $method->getName();
84 }
85 sort($methods);
86 $this->assertEquals(
87 $methods,
88 [
89 0 => '__construct',
90 1 => 'core',
91 2 => 'fromSerializedIdentification',
92 3 => 'plugin',
93 4 => 'tool'
94 ]
95 );
96 }
97
98
99 public function testCore()
100 {
101 $this->assertInstanceOf(IdentificationProviderInterface::class, $this->identification->core($this->provider_mock));
102 $this->assertInstanceOf(IdentificationInterface::class, $this->identification->core($this->provider_mock)->identifier('dummy'));
103 }
104
105
106 public function testPlugin()
107 {
108 $this->plugin_mock->shouldReceive('getId')->once()->andReturn('xdemo');
109 $identification_provider = $this->identification->plugin($this->plugin_mock->getId(), $this->provider_mock);
110 $this->assertInstanceOf(IdentificationProviderInterface::class, $identification_provider);
111 $identification = $identification_provider->identifier('dummy');
112 $this->assertInstanceOf(IdentificationInterface::class, $identification);
113 }
114
115
116 public function testSerializingCore()
117 {
118 $identification = $this->identification->core($this->provider_mock)->identifier('dummy');
119 $this->assertInstanceOf(Serializable::class, $identification);
120 $this->assertEquals($identification->serialize(), get_class($this->provider_mock) . "|dummy");
121 }
122
123
124 public function testUnserializingCore()
125 {
126 $identification = $this->identification->core($this->provider_mock)->identifier('dummy');
127 $serialized_identification = $identification->serialize();
128
129 $new_identification = $this->identification->fromSerializedIdentification($serialized_identification);
130 $this->assertEquals($identification, $new_identification);
131 }
132
133
134 public function testUnserializingPlugin()
135 {
136 $this->plugin_mock->shouldReceive('getId')->once()->andReturn('xdemo');
137 $identification = $this->identification->plugin($this->plugin_mock->getId(), $this->provider_mock)->identifier('dummy');
138 $serialized_identification = $identification->serialize();
139 $this->provider_mock->shouldReceive('getProviderNameForPresentation')->andReturn('Provider');
140 $new_identification = $this->identification->fromSerializedIdentification($serialized_identification);
141 $this->assertEquals($identification, $new_identification);
142 }
143
144
146 {
147 $this->expectException(InvalidArgumentException::class);
148 $this->expectExceptionMessage("Nobody can handle serialized identification 'ThisStringWillNobodyHandle'.");
149 $this->identification->fromSerializedIdentification("ThisStringWillNobodyHandle");
150 }
151
152
154 {
156 $internal_identifier = "internal_identifier";
157
158 $string_core = "{$class_name}|{$internal_identifier}";
159 $identification = $this->identification->fromSerializedIdentification($string_core);
160
161 $this->assertInstanceOf(CoreIdentification::class, $identification);
162 $this->assertEquals($identification->getClassName(), $class_name);
163 $this->assertEquals($identification->getInternalIdentifier(), $internal_identifier);
164 }
165
166
168 {
170 $internal_identifier = "internal_identifier";
171
172 // $this->markTestSkipped('I currently have absolutely no idea why this test does not work since this seems to be identical zo the test testUnserializingCore :(');
173 $string_plugin = "xdemo|{$class_name}|{$internal_identifier}";
174 $identification = $this->identification->fromSerializedIdentification($string_plugin);
175
176 $this->assertInstanceOf(PluginIdentification::class, $identification);
177 $this->assertEquals($identification->getClassName(), $class_name);
178 $this->assertEquals($identification->getInternalIdentifier(), $internal_identifier);
179 }
180}
An exception for terminatinating execution or to throw for unit testing.
Abstract Class ilPlugin.