From 054c74b79470ffb574f54bbeeb65cff340a0f178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Jou=C3=9Fen?= Date: Wed, 28 Feb 2024 16:19:32 +0100 Subject: [PATCH] Adjusted integration for Ilias release_8 --- .github/workflows/ci.yaml | 34 ++++++--- .php-cs-fixer.php | 4 +- phpunit.xml | 4 +- src/DI/PluginContainerException.php | 2 +- src/DI/PluginContainerNotFoundException.php | 3 +- src/DI/PluginContainerTrait.php | 2 - src/DI/PluginContainerWrapper.php | 2 +- src/Test/TestCase/AbstractGUITestCase.php | 2 - src/Test/TestCase/AbstractPluginTestCase.php | 2 - src/Test/Traits/ContainerMockHelperTrait.php | 1 - tests/DI/PluginContainerTest.php | 28 +++++++ tests/DI/PluginContainerWrapperTest.php | 73 +++++++++++++++++++ .../Traits/ContainerMockHelperTraitTest.php | 1 - 13 files changed, 135 insertions(+), 23 deletions(-) create mode 100644 tests/DI/PluginContainerWrapperTest.php diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index b427dae..063ab3f 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -1,26 +1,42 @@ name: CI +env: + BRANCH_NAME: ${{ github.head_ref || github.ref_name }} + PLUGIN_PATH: Customizing/global/libs/ilias-plugin-utils + MATRIX: | + { + "release/7": {"php": ["7.4"], "ilias": ["release_7"]}, + "release/8": {"php": ["8.0"], "ilias": ["release_8"]} + } + on: push: + branches: + - release/* pull_request: jobs: + build-matrix: + if: "!contains(github.event.head_commit.message, '[CI SKIP]')" + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.set-matrix.outputs.matrix }} + steps: + - id: set-matrix + run: echo "$(echo $MATRIX | jq -c --arg branch $BRANCH_NAME '.[$branch]')" >> $GITHUB_OUTPUT + PHPUnit: - name: PHPUnit (PHP ${{ matrix.php }}) if: "!contains(github.event.head_commit.message, '[CI SKIP]')" - runs-on: ubuntu-22.04 - env: - PLUGIN_PATH: Customizing/global/libs/ilias-plugin-utils + needs: build-matrix + name: Check (ILIAS ${{ matrix.ilias }} PHP ${{ matrix.php }}) + runs-on: ubuntu-latest strategy: - matrix: - php: - - 8.0 - - 7.4 + matrix: ${{ fromJson(needs.build-matrix.outputs.matrix) }} steps: - uses: actions/checkout@v4 with: repository: ILIAS-eLearning/ILIAS - ref: release_8 + ref: ${{ matrix.ilias }} - uses: actions/checkout@v4 with: path: ${{ env.PLUGIN_PATH }} diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index b41c323..43ec486 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -10,8 +10,8 @@ TXT; $finder = PhpCsFixer\Finder::create() - ->exclude('vendor') - ->in(__DIR__); + ->in(__DIR__) + ->exclude('vendor'); return (new PhpCsFixer\Config()) ->setRules([ diff --git a/phpunit.xml b/phpunit.xml index d371035..d99a973 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -2,14 +2,16 @@ diff --git a/src/DI/PluginContainerException.php b/src/DI/PluginContainerException.php index 36e2170..c6377f2 100644 --- a/src/DI/PluginContainerException.php +++ b/src/DI/PluginContainerException.php @@ -24,6 +24,6 @@ * @author Thomas Joußen * @see https://www.php-fig.org/psr/psr-11/#32-psrcontainercontainerexceptioninterface */ -class PluginContainerException extends \RuntimeException implements ContainerExceptionInterface +class PluginContainerException extends Exception implements ContainerExceptionInterface { } diff --git a/src/DI/PluginContainerNotFoundException.php b/src/DI/PluginContainerNotFoundException.php index 6426da5..f786a03 100644 --- a/src/DI/PluginContainerNotFoundException.php +++ b/src/DI/PluginContainerNotFoundException.php @@ -13,6 +13,7 @@ namespace Thojou\Ilias\Plugin\Utils\DI; +use Exception; use Psr\Container\NotFoundExceptionInterface; /** @@ -23,6 +24,6 @@ * @author Thomas Joußen * @see https://www.php-fig.org/psr/psr-11/#33-psrcontainernotfoundexceptioninterface */ -class PluginContainerNotFoundException extends \Exception implements NotFoundExceptionInterface +class PluginContainerNotFoundException extends Exception implements NotFoundExceptionInterface { } diff --git a/src/DI/PluginContainerTrait.php b/src/DI/PluginContainerTrait.php index 476f20d..f58d670 100644 --- a/src/DI/PluginContainerTrait.php +++ b/src/DI/PluginContainerTrait.php @@ -17,8 +17,6 @@ use Psr\Container\ContainerInterface; use RuntimeException; -use function is_object; - /** * PluginContainerTrait * diff --git a/src/DI/PluginContainerWrapper.php b/src/DI/PluginContainerWrapper.php index ca6b871..1bcb3f1 100644 --- a/src/DI/PluginContainerWrapper.php +++ b/src/DI/PluginContainerWrapper.php @@ -65,7 +65,7 @@ public function get(string $id) { $id = $this->resolveId($id); - if(!$this->has($id)) { + if(!isset($this->container[$id])) { throw new PluginContainerNotFoundException("Service with id '$id' not found in PluginContainer."); } diff --git a/src/Test/TestCase/AbstractGUITestCase.php b/src/Test/TestCase/AbstractGUITestCase.php index 8bc1599..af912d0 100644 --- a/src/Test/TestCase/AbstractGUITestCase.php +++ b/src/Test/TestCase/AbstractGUITestCase.php @@ -14,8 +14,6 @@ namespace Thojou\Ilias\Plugin\Utils\Test\TestCase; use ILIAS\HTTP\Wrapper\WrapperFactory; -use ILIAS\Refinery\Factory as Refinery; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\Rule\InvocationOrder; use PHPUnit\Framework\TestCase; use Psr\Http\Message\ServerRequestInterface; diff --git a/src/Test/TestCase/AbstractPluginTestCase.php b/src/Test/TestCase/AbstractPluginTestCase.php index 7e8e1b0..8c7c5a1 100644 --- a/src/Test/TestCase/AbstractPluginTestCase.php +++ b/src/Test/TestCase/AbstractPluginTestCase.php @@ -13,8 +13,6 @@ namespace Thojou\Ilias\Plugin\Utils\Test\TestCase; -use ilDBInterface; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Thojou\Ilias\Plugin\Utils\Test\ContainerMockHelperInterface; use Thojou\Ilias\Plugin\Utils\Test\Traits\CommonHelperTrait; diff --git a/src/Test/Traits/ContainerMockHelperTrait.php b/src/Test/Traits/ContainerMockHelperTrait.php index 68b48b6..d4544cc 100644 --- a/src/Test/Traits/ContainerMockHelperTrait.php +++ b/src/Test/Traits/ContainerMockHelperTrait.php @@ -13,7 +13,6 @@ namespace Thojou\Ilias\Plugin\Utils\Test\Traits; -use ilCoSourceCodePlugin; use ILIAS\DI\Container; use Thojou\Ilias\Plugin\Utils\Test\ContainerMockHelperInterface; diff --git a/tests/DI/PluginContainerTest.php b/tests/DI/PluginContainerTest.php index ea6c638..dfc73d9 100644 --- a/tests/DI/PluginContainerTest.php +++ b/tests/DI/PluginContainerTest.php @@ -17,6 +17,7 @@ use ILIAS\DI\Container; use PHPUnit\Framework\TestCase; use RuntimeException; +use Thojou\Ilias\Plugin\Utils\DI\PluginContainerException; use Thojou\Ilias\Plugin\Utils\DI\PluginContainerNotFoundException; use Thojou\Ilias\Plugin\Utils\DI\PluginContainerWrapper; use Thojou\Ilias\Plugin\Utils\Tests\DI\Fixtures\TestPluginContainer; @@ -72,6 +73,11 @@ public function testGetService(): void { $service = $this->createMock(stdClass::class); $DIC = $this->createMock(Container::class); + $DIC + ->expects($this->once()) + ->method('offsetExists') + ->with($this->equalTo('plugin_id'.'.' . stdClass::class)) + ->willReturn(true); $DIC ->expects($this->once()) ->method('offsetGet') @@ -82,4 +88,26 @@ public function testGetService(): void $this->assertSame($service, TestPluginContainer::get()->plugin()->get(stdClass::class)); } + public function testPluginContainerException() + { + $this->expectException(PluginContainerException::class); + + $DIC = $this->createMock(Container::class); + $DIC + ->expects($this->once()) + ->method('offsetExists') + ->with($this->equalTo('plugin_id'.'.' . stdClass::class)) + ->willReturn(true); + + $DIC + ->expects($this->once()) + ->method('offsetGet') + ->willReturnCallback(function () { + throw new RuntimeException('Test'); + }); + + TestPluginContainer::init($DIC, 'plugin_id'); + TestPluginContainer::get()->plugin()->get(stdClass::class); + } + } diff --git a/tests/DI/PluginContainerWrapperTest.php b/tests/DI/PluginContainerWrapperTest.php new file mode 100644 index 0000000..0ce404d --- /dev/null +++ b/tests/DI/PluginContainerWrapperTest.php @@ -0,0 +1,73 @@ +createMock(Container::class); + $container->method('offsetExists')->willReturn(true); + $container->method('offsetGet')->willReturn('service'); + $pluginContainerWrapper = new PluginContainerWrapper($container); + + $this->assertEquals('service', $pluginContainerWrapper->get('service')); + } + + public function testGetWithPrefix() + { + $container = $this->createMock(Container::class); + $container->method('offsetExists')->willReturn(true); + $container->method('offsetGet')->willReturn('service'); + $pluginContainerWrapper = new PluginContainerWrapper($container, 'prefix_'); + + $this->assertEquals('service', $pluginContainerWrapper->get('service')); + } + + public function testHas() + { + $container = $this->createMock(Container::class); + $container->method('offsetExists')->willReturn(true); + $pluginContainerWrapper = new PluginContainerWrapper($container); + + $this->assertTrue($pluginContainerWrapper->has('service')); + } + + public function testHasReturnsFalse() + { + $container = $this->createMock(Container::class); + $container->method('offsetExists')->willReturn(false); + $pluginContainerWrapper = new PluginContainerWrapper($container); + + $this->assertFalse($pluginContainerWrapper->has('nonexistent_service')); + } + + public function testGetThrowsNotFoundException() + { + $this->expectException(PluginContainerNotFoundException::class); + + $container = $this->createMock(Container::class); + $container->method('offsetExists')->willReturn(false); + $pluginContainerWrapper = new PluginContainerWrapper($container); + + $pluginContainerWrapper->get('nonexistent_service'); + } + + public function testGetThrowsContainerException() + { + $container = $this->createMock(Container::class); + $container->method('offsetExists')->willReturn(true); + $container->method('offsetGet')->will($this->throwException(new Exception())); + $pluginContainerWrapper = new PluginContainerWrapper($container); + + $this->expectException(PluginContainerException::class); + $pluginContainerWrapper->get('service_causing_exception'); + } +} \ No newline at end of file diff --git a/tests/Test/Traits/ContainerMockHelperTraitTest.php b/tests/Test/Traits/ContainerMockHelperTraitTest.php index 1029fb9..d3d7ce0 100644 --- a/tests/Test/Traits/ContainerMockHelperTraitTest.php +++ b/tests/Test/Traits/ContainerMockHelperTraitTest.php @@ -15,7 +15,6 @@ use ilComponentRepositoryWrite; use ILIAS\DI\Container; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use stdClass; use Thojou\Ilias\Plugin\Utils\Test\ContainerMockHelperInterface;