|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * This file is part of the phpBB Forum Software package. |
| 5 | + * |
| 6 | + * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | + * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | + * |
| 9 | + * For full copyright and license information, please see |
| 10 | + * the docs/CREDITS.txt file. |
| 11 | + * |
| 12 | + */ |
| 13 | + |
| 14 | +namespace phpbb\skeleton\tests; |
| 15 | + |
| 16 | +use phpbb\db\migrator; |
| 17 | +use phpbb\finder; |
| 18 | +use phpbb\language\language; |
| 19 | +use phpbb\skeleton\ext; |
| 20 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 21 | + |
| 22 | +class ext_test extends \phpbb_test_case |
| 23 | +{ |
| 24 | + protected $ext; |
| 25 | + |
| 26 | + protected function setUp(): void |
| 27 | + { |
| 28 | + $this->ext = new ext( |
| 29 | + $this->createMock(ContainerInterface::class), |
| 30 | + $this->createMock(finder::class), |
| 31 | + $this->createMock(migrator::class), |
| 32 | + 'phpbb/skeleton', |
| 33 | + '' |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + public function test_ext_is_instance_of_base() |
| 38 | + { |
| 39 | + $this->assertInstanceOf(ext::class, $this->ext); |
| 40 | + } |
| 41 | + |
| 42 | + public function test_is_enableable_true() |
| 43 | + { |
| 44 | + $ext = $this->getMockBuilder(ext::class) |
| 45 | + ->disableOriginalConstructor() |
| 46 | + ->setMethods(['ziparchive_exists', 'phpbb_requirement', 'php_requirement']) |
| 47 | + ->getMock(); |
| 48 | + |
| 49 | + $ext->method('ziparchive_exists')->willReturn(null); |
| 50 | + $ext->method('phpbb_requirement')->willReturn(null); |
| 51 | + $ext->method('php_requirement')->willReturn(null); |
| 52 | + |
| 53 | + $this->setExtErrors($ext, []); |
| 54 | + $this->assertTrue($ext->is_enableable()); |
| 55 | + } |
| 56 | + |
| 57 | + public function test_is_enableable_with_errors() |
| 58 | + { |
| 59 | + $ext = $this->getMockBuilder(ext::class) |
| 60 | + ->disableOriginalConstructor() |
| 61 | + ->setMethods(['ziparchive_exists', 'phpbb_requirement', 'php_requirement', 'enable_failed']) |
| 62 | + ->getMock(); |
| 63 | + |
| 64 | + $ext->method('ziparchive_exists')->willReturnCallback(function () use ($ext) { |
| 65 | + $this->appendExtError($ext, 'NO_ZIPARCHIVE_ERROR'); |
| 66 | + }); |
| 67 | + $ext->method('phpbb_requirement')->willReturn(null); |
| 68 | + $ext->method('php_requirement')->willReturn(null); |
| 69 | + $ext->method('enable_failed')->willReturn(['NO_ZIPARCHIVE_ERROR']); |
| 70 | + |
| 71 | + $this->setExtErrors($ext, ['NO_ZIPARCHIVE_ERROR']); |
| 72 | + $this->assertEquals(['NO_ZIPARCHIVE_ERROR'], $ext->is_enableable()); |
| 73 | + } |
| 74 | + |
| 75 | + public function test_enable_failed_returns_expected() |
| 76 | + { |
| 77 | + $ext = $this->getMockBuilder(ext::class) |
| 78 | + ->disableOriginalConstructor() |
| 79 | + ->getMock(); |
| 80 | + |
| 81 | + $this->setExtErrors($ext, ['SOME_ERROR']); |
| 82 | + |
| 83 | + $languageMock = $this->createMock(language::class); |
| 84 | + $languageMock->method('add_lang')->willReturn(null); |
| 85 | + $languageMock->method('lang')->willReturnCallback(function ($msg) { |
| 86 | + return "LANG: $msg"; |
| 87 | + }); |
| 88 | + |
| 89 | + $containerMock = $this->createMock(ContainerInterface::class); |
| 90 | + $containerMock->method('get')->with('language')->willReturn($languageMock); |
| 91 | + |
| 92 | + $this->setProperty($ext, 'container', $containerMock); |
| 93 | + |
| 94 | + $method = (new \ReflectionClass($ext))->getMethod('enable_failed'); |
| 95 | + $method->setAccessible(true); |
| 96 | + |
| 97 | + $this->assertEquals(['LANG: SOME_ERROR'], $method->invoke($ext)); |
| 98 | + } |
| 99 | + |
| 100 | + public function test_phpbb_requirement_min_error() |
| 101 | + { |
| 102 | + $this->setExtErrors($this->ext, []); |
| 103 | + $this->invokeProtectedMethod($this->ext, 'phpbb_requirement', ['3.2.2']); |
| 104 | + $this->assertContains('PHPBB_VERSION_MIN_ERROR', $this->getExtErrors($this->ext)); |
| 105 | + } |
| 106 | + |
| 107 | + public function test_phpbb_requirement_max_error() |
| 108 | + { |
| 109 | + $this->setExtErrors($this->ext, []); |
| 110 | + $this->invokeProtectedMethod($this->ext, 'phpbb_requirement', ['4.0.0-dev']); |
| 111 | + $this->assertContains('PHPBB_VERSION_MAX_ERROR', $this->getExtErrors($this->ext)); |
| 112 | + } |
| 113 | + |
| 114 | + public function test_php_requirement_error() |
| 115 | + { |
| 116 | + $this->setExtErrors($this->ext, []); |
| 117 | + $this->invokeProtectedMethod($this->ext, 'php_requirement', [50500]); |
| 118 | + $this->assertContains('PHP_VERSION_ERROR', $this->getExtErrors($this->ext)); |
| 119 | + } |
| 120 | + |
| 121 | + public function test_ziparchive_exists_error() |
| 122 | + { |
| 123 | + $this->setExtErrors($this->ext, []); |
| 124 | + $this->invokeProtectedMethod($this->ext, 'ziparchive_exists', ['NotZipArchive']); |
| 125 | + $this->assertContains('NO_ZIPARCHIVE_ERROR', $this->getExtErrors($this->ext)); |
| 126 | + } |
| 127 | + |
| 128 | + // --- Helpers --- |
| 129 | + |
| 130 | + protected function invokeProtectedMethod($object, string $methodName, array $args = []) |
| 131 | + { |
| 132 | + $method = (new \ReflectionClass($object))->getMethod($methodName); |
| 133 | + $method->setAccessible(true); |
| 134 | + return $method->invokeArgs($object, $args); |
| 135 | + } |
| 136 | + |
| 137 | + protected function getExtErrors($ext): array |
| 138 | + { |
| 139 | + $prop = (new \ReflectionClass($ext))->getProperty('errors'); |
| 140 | + $prop->setAccessible(true); |
| 141 | + return $prop->getValue($ext); |
| 142 | + } |
| 143 | + |
| 144 | + protected function setExtErrors($ext, array $errors): void |
| 145 | + { |
| 146 | + $prop = (new \ReflectionClass($ext))->getProperty('errors'); |
| 147 | + $prop->setAccessible(true); |
| 148 | + $prop->setValue($ext, $errors); |
| 149 | + } |
| 150 | + |
| 151 | + protected function appendExtError($ext, string $error): void |
| 152 | + { |
| 153 | + $errors = $this->getExtErrors($ext); |
| 154 | + $errors[] = $error; |
| 155 | + $this->setExtErrors($ext, $errors); |
| 156 | + } |
| 157 | + |
| 158 | + protected function setProperty($object, string $property, $value): void |
| 159 | + { |
| 160 | + $prop = (new \ReflectionClass($object))->getProperty($property); |
| 161 | + $prop->setAccessible(true); |
| 162 | + $prop->setValue($object, $value); |
| 163 | + } |
| 164 | +} |
0 commit comments