|
| 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\helper; |
| 15 | + |
| 16 | +use phpbb\skeleton\helper\packager; |
| 17 | +use phpbb\di\service_collection; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 20 | + |
| 21 | +class packager_test extends \phpbb_test_case |
| 22 | +{ |
| 23 | + /** @var ContainerInterface|MockObject */ |
| 24 | + protected $container; |
| 25 | + |
| 26 | + /** @var service_collection|MockObject */ |
| 27 | + protected $collection; |
| 28 | + |
| 29 | + /** @var packager */ |
| 30 | + protected $packager; |
| 31 | + |
| 32 | + /** @var string */ |
| 33 | + protected $root_path = '/tmp/phpbb/'; |
| 34 | + |
| 35 | + /** |
| 36 | + * @return void |
| 37 | + */ |
| 38 | + public function setUpTheCollectionToReturnAFakeSkeletonClass(): void |
| 39 | + { |
| 40 | + // Set up the collection to return a fake skeleton class |
| 41 | + $skeleton = $this->getMockBuilder(\phpbb\skeleton\skeleton::class) |
| 42 | + ->disableOriginalConstructor() |
| 43 | + ->getMock(); |
| 44 | + $skeleton->method('get_name')->willReturn('phplistener'); |
| 45 | + $skeleton->method('get_default')->willReturn(false); |
| 46 | + $skeleton->method('get_dependencies')->willReturn([]); |
| 47 | + $skeleton->method('get_files')->willReturn(['test.php']); |
| 48 | + $skeleton->method('get_group')->willReturn('BACK_END'); |
| 49 | + |
| 50 | + $this->collection->method('getIterator')->willReturn(new \ArrayIterator([$skeleton])); |
| 51 | + } |
| 52 | + |
| 53 | + protected function setUp(): void |
| 54 | + { |
| 55 | + $this->container = $this->createMock(ContainerInterface::class); |
| 56 | + $this->collection = $this->createMock(service_collection::class); |
| 57 | + |
| 58 | + $this->packager = new packager($this->container, $this->collection, $this->root_path); |
| 59 | + } |
| 60 | + |
| 61 | + public function test_get_composer_dialog_values_returns_expected_structure() |
| 62 | + { |
| 63 | + $result = $this->packager->get_composer_dialog_values(); |
| 64 | + $this->assertArrayHasKey('author', $result); |
| 65 | + $this->assertArrayHasKey('extension', $result); |
| 66 | + $this->assertArrayHasKey('requirements', $result); |
| 67 | + } |
| 68 | + |
| 69 | + public function test_get_component_dialog_values_calls_collection() |
| 70 | + { |
| 71 | + $this->setUpTheCollectionToReturnAFakeSkeletonClass(); |
| 72 | + |
| 73 | + $result = $this->packager->get_component_dialog_values(); |
| 74 | + $this->assertArrayHasKey('phplistener', $result); |
| 75 | + $this->assertSame([ |
| 76 | + 'default' => false, |
| 77 | + 'dependencies' => [], |
| 78 | + 'files' => ['test.php'], |
| 79 | + 'group' => 'BACK_END' |
| 80 | + ], $result['phplistener']); |
| 81 | + } |
| 82 | + |
| 83 | + public function test_create_extension_runs_without_exception() |
| 84 | + { |
| 85 | + // Create a partial mock for packager, stubbing get_template_engine |
| 86 | + $packager = $this->getMockBuilder(packager::class) |
| 87 | + ->setConstructorArgs([$this->container, $this->collection, $this->root_path]) |
| 88 | + ->setMethods(['get_template_engine']) |
| 89 | + ->getMock(); |
| 90 | + |
| 91 | + // Mock the template engine |
| 92 | + $templateMock = $this->createMock(\phpbb\template\twig\twig::class); |
| 93 | + $templateMock->method('set_custom_style')->willReturnSelf(); |
| 94 | + $templateMock->method('assign_vars')->willReturnSelf(); |
| 95 | + $templateMock->method('set_filenames')->willReturnSelf(); |
| 96 | + $templateMock->method('assign_display')->willReturn('dummy'); |
| 97 | + $packager->method('get_template_engine')->willReturn($templateMock); |
| 98 | + |
| 99 | + $this->setUpTheCollectionToReturnAFakeSkeletonClass(); |
| 100 | + |
| 101 | + // You can mock dependencies further as needed |
| 102 | + $data = [ |
| 103 | + 'extension' => [ |
| 104 | + 'vendor_name' => 'testvendor', |
| 105 | + 'extension_name' => 'testext', |
| 106 | + 'extension_display_name' => 'Test Ext', |
| 107 | + 'extension_version' => '1.0.0', |
| 108 | + ], |
| 109 | + 'requirements' => [ |
| 110 | + 'phpbb_version_min' => '3.2.0', |
| 111 | + ], |
| 112 | + 'components' => [], |
| 113 | + 'authors' => [], |
| 114 | + ]; |
| 115 | + // No assertions, just ensure no exceptions |
| 116 | + $packager->create_extension($data); |
| 117 | + $this->assertTrue(true); |
| 118 | + } |
| 119 | + |
| 120 | + public function test_create_zip_creates_zip_file() |
| 121 | + { |
| 122 | + $data = [ |
| 123 | + 'extension' => [ |
| 124 | + 'vendor_name' => 'testvendor', |
| 125 | + 'extension_name' => 'testext', |
| 126 | + 'extension_version' => '1.0.0', |
| 127 | + ], |
| 128 | + ]; |
| 129 | + // You might want to mock filesystem interactions or use vfsStream |
| 130 | + $zipPath = $this->packager->create_zip($data); |
| 131 | + $this->assertIsString($zipPath); |
| 132 | + // Clean up or further assertions here |
| 133 | + } |
| 134 | + |
| 135 | + public function test_get_language_version_data_returns_expected() |
| 136 | + { |
| 137 | + $data_31 = $this->invokeMethod($this->packager, 'get_language_version_data', [true]); |
| 138 | + $data_32 = $this->invokeMethod($this->packager, 'get_language_version_data', [false]); |
| 139 | + $this->assertArrayHasKey('class', $data_31); |
| 140 | + $this->assertArrayHasKey('class', $data_32); |
| 141 | + } |
| 142 | + |
| 143 | + public function test_get_template_engine_returns_twig_instance() |
| 144 | + { |
| 145 | + // Mock all required dependencies for the container |
| 146 | + $filesystem = $this->createMock(\phpbb\filesystem\filesystem::class); |
| 147 | + $path_helper = $this->createMock(\phpbb\path_helper::class); |
| 148 | + $ext_manager = $this->createMock(\phpbb\extension\manager::class); |
| 149 | + $user = $this->createMock(\phpbb\user::class); |
| 150 | + |
| 151 | + $this->container->expects($this->exactly(4)) |
| 152 | + ->method('get') |
| 153 | + ->withConsecutive( |
| 154 | + ['path_helper'], |
| 155 | + ['filesystem'], |
| 156 | + ['ext.manager'], |
| 157 | + ['user'] |
| 158 | + ) |
| 159 | + ->willReturnOnConsecutiveCalls( |
| 160 | + $path_helper, |
| 161 | + $filesystem, |
| 162 | + $ext_manager, |
| 163 | + $user |
| 164 | + ); |
| 165 | + |
| 166 | + $this->container->expects($this->exactly(2)) |
| 167 | + ->method('getParameter') |
| 168 | + ->with('core.cache_dir') |
| 169 | + ->willReturn(false); |
| 170 | + |
| 171 | + // Call the protected method using your invokeMethod helper |
| 172 | + $twig = $this->invokeMethod($this->packager, 'get_template_engine'); |
| 173 | + $this->assertInstanceOf(\phpbb\template\twig\twig::class, $twig); |
| 174 | + } |
| 175 | + |
| 176 | + // Helper for protected/private method invocation |
| 177 | + protected function invokeMethod($object, $methodName, array $parameters = []) |
| 178 | + { |
| 179 | + $reflection = new \ReflectionClass(get_class($object)); |
| 180 | + $method = $reflection->getMethod($methodName); |
| 181 | + $method->setAccessible(true); |
| 182 | + return $method->invokeArgs($object, $parameters); |
| 183 | + } |
| 184 | +} |
0 commit comments