Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Bridge / ZfExtensionManagerSfContainerTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Bridge;
4
5 use Drupal\Component\Bridge\ZfExtensionManagerSfContainer;
6 use PHPUnit\Framework\TestCase;
7 use Symfony\Component\DependencyInjection\ContainerBuilder;
8 use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
9 use Zend\Feed\Reader\Extension\Atom\Entry;
10 use Zend\Feed\Reader\StandaloneExtensionManager;
11
12 /**
13  * @coversDefaultClass \Drupal\Component\Bridge\ZfExtensionManagerSfContainer
14  * @group Bridge
15  */
16 class ZfExtensionManagerSfContainerTest extends TestCase {
17
18   /**
19    * @covers ::setContainer
20    * @covers ::setStandalone
21    * @covers ::get
22    */
23   public function testGet() {
24     $service = new \stdClass();
25     $service->value = 'myvalue';
26     $container = new ContainerBuilder();
27     $container->set('foo', $service);
28     $bridge = new ZfExtensionManagerSfContainer();
29     $bridge->setContainer($container);
30     $this->assertEquals($service, $bridge->get('foo'));
31     $bridge->setStandalone(StandaloneExtensionManager::class);
32     $this->assertInstanceOf(Entry::class, $bridge->get('Atom\Entry'));
33     // Ensure that the container is checked first.
34     $container->set('atomentry', $service);
35     $this->assertEquals($service, $bridge->get('Atom\Entry'));
36   }
37
38   /**
39    * @covers ::setContainer
40    * @covers ::setStandalone
41    * @covers ::has
42    */
43   public function testHas() {
44     $service = new \stdClass();
45     $service->value = 'myvalue';
46     $container = new ContainerBuilder();
47     $container->set('foo', $service);
48     $bridge = new ZfExtensionManagerSfContainer();
49     $bridge->setContainer($container);
50     $this->assertTrue($bridge->has('foo'));
51     $this->assertFalse($bridge->has('bar'));
52     $this->assertFalse($bridge->has('Atom\Entry'));
53     $bridge->setStandalone(StandaloneExtensionManager::class);
54     $this->assertTrue($bridge->has('Atom\Entry'));
55   }
56
57   /**
58    * @covers ::setStandalone
59    */
60   public function testSetStandaloneException() {
61     if (method_exists($this, 'expectException')) {
62       $this->expectException(\RuntimeException::class);
63       $this->expectExceptionMessage('Drupal\Tests\Component\Bridge\ZfExtensionManagerSfContainerTest must implement Zend\Feed\Reader\ExtensionManagerInterface or Zend\Feed\Writer\ExtensionManagerInterface');
64     }
65     else {
66       $this->setExpectedException(\RuntimeException::class, 'Drupal\Tests\Component\Bridge\ZfExtensionManagerSfContainerTest must implement Zend\Feed\Reader\ExtensionManagerInterface or Zend\Feed\Writer\ExtensionManagerInterface');
67     }
68     $bridge = new ZfExtensionManagerSfContainer();
69     $bridge->setStandalone(static::class);
70   }
71
72   /**
73    * @covers ::get
74    */
75   public function testGetContainerException() {
76     if (method_exists($this, 'expectException')) {
77       $this->expectException(ServiceNotFoundException::class);
78       $this->expectExceptionMessage('You have requested a non-existent service "test.foo".');
79     }
80     else {
81       $this->setExpectedException(ServiceNotFoundException::class, 'You have requested a non-existent service "test.foo".');
82     }
83     $container = new ContainerBuilder();
84     $bridge = new ZfExtensionManagerSfContainer('test.');
85     $bridge->setContainer($container);
86     $bridge->setStandalone(StandaloneExtensionManager::class);
87     $bridge->get('foo');
88   }
89
90   /**
91    * @covers ::__construct
92    * @covers ::has
93    * @covers ::get
94    */
95   public function testPrefix() {
96     $service = new \stdClass();
97     $service->value = 'myvalue';
98     $container = new ContainerBuilder();
99     $container->set('foo.bar', $service);
100     $bridge = new ZfExtensionManagerSfContainer('foo.');
101     $bridge->setContainer($container);
102     $this->assertTrue($bridge->has('bar'));
103     $this->assertFalse($bridge->has('baz'));
104     $this->assertEquals($service, $bridge->get('bar'));
105   }
106
107   /**
108    * @covers ::canonicalizeName
109    * @dataProvider canonicalizeNameProvider
110    */
111   public function testCanonicalizeName($name, $canonical_name) {
112     $service = new \stdClass();
113     $service->value = 'myvalue';
114     $container = new ContainerBuilder();
115     $container->set($canonical_name, $service);
116     $bridge = new ZfExtensionManagerSfContainer();
117     $bridge->setContainer($container);
118     $this->assertTrue($bridge->has($name));
119     $this->assertEquals($service, $bridge->get($name));
120   }
121
122   /**
123    * Data provider for testReverseProxyEnabled.
124    *
125    * Replacements:
126    *   array('-' => '', '_' => '', ' ' => '', '\\' => '', '/' => '')
127    */
128   public function canonicalizeNameProvider() {
129     return [
130       [
131         'foobar',
132         'foobar',
133       ],
134       [
135         'foo-bar',
136         'foobar',
137       ],
138       [
139         'foo_bar',
140         'foobar',
141       ],
142       [
143         'foo bar',
144         'foobar',
145       ],
146       [
147         'foo\\bar',
148         'foobar',
149       ],
150       [
151         'foo/bar',
152         'foobar',
153       ],
154       // There is also a strtolower in canonicalizeName.
155       [
156         'Foo/bAr',
157         'foobar',
158       ],
159       [
160         'foo/-_\\ bar',
161         'foobar',
162       ],
163     ];
164   }
165
166 }