|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Test\Unit\Plugin; |
| 9 | + |
| 10 | +use Magento\Framework\App\State; |
| 11 | +use Magento\Framework\Exception\LocalizedException; |
| 12 | +use Magento\Framework\Session\SessionStartChecker; |
| 13 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 14 | +use Magento\GraphQl\Model\Config\DisableSession; |
| 15 | +use Magento\GraphQl\Plugin\DisableSession as DisableSessionPlugin; |
| 16 | +use PHPUnit\Framework\MockObject\MockObject; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +/** |
| 20 | + * Test for DisableSession plugin. |
| 21 | + */ |
| 22 | +class DisableSessionTest extends TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var DisableSession|MockObject |
| 26 | + */ |
| 27 | + private $disableSessionConfigMock; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var State|MockObject |
| 31 | + */ |
| 32 | + private $appStateMock; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var DisableSessionPlugin |
| 36 | + */ |
| 37 | + private $model; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var SessionStartChecker|MockObject |
| 41 | + */ |
| 42 | + private $sessionStartCheckerMock; |
| 43 | + |
| 44 | + public function setUp(): void |
| 45 | + { |
| 46 | + $this->disableSessionConfigMock = $this->createMock(DisableSession::class); |
| 47 | + $this->appStateMock = $this->createMock(State::class); |
| 48 | + $this->sessionStartCheckerMock = $this->createMock(SessionStartChecker::class); |
| 49 | + $this->model = (new ObjectManager($this))->getObject( |
| 50 | + DisableSessionPlugin::class, |
| 51 | + [ |
| 52 | + 'disableSessionConfig' => $this->disableSessionConfigMock, |
| 53 | + 'appState' => $this->appStateMock |
| 54 | + ] |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + /** |
| 60 | + * Test afterCheck plugin result over original method result. |
| 61 | + * |
| 62 | + * @param string $area |
| 63 | + * @param bool $config |
| 64 | + * @param bool $methodResult |
| 65 | + * @param bool $expectedResult |
| 66 | + * @return void |
| 67 | + * @dataProvider testAfterCheckDataProvider |
| 68 | + */ |
| 69 | + public function testAfterCheck(string $area, bool $config, bool $methodResult, bool $expectedResult) |
| 70 | + { |
| 71 | + $this->disableSessionConfigMock->expects($this->any())->method('isDisabled')->willReturn($config); |
| 72 | + $this->appStateMock->expects($this->any())->method('getAreaCode')->willReturn($area); |
| 73 | + $this->assertEquals($expectedResult, $this->model->afterCheck($this->sessionStartCheckerMock, $methodResult)); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Data provider for testAfterCheck. |
| 78 | + * |
| 79 | + * @return array[] |
| 80 | + */ |
| 81 | + public function testAfterCheckDataProvider() |
| 82 | + { |
| 83 | + return [ |
| 84 | + ['area' => 'graphql', 'config' => true, 'methodResult' => false, 'expected' => false], |
| 85 | + ['area' => 'graphql', 'config' => true, 'methodResult' => true, 'expected' => false], |
| 86 | + ['area' => 'graphql', 'config' => false, 'methodResult' => true, 'expected' => true], |
| 87 | + ['area' => 'graphql', 'config' => false, 'methodResult' => false, 'expected' => false], |
| 88 | + ['area' => 'other', 'config' => false, 'methodResult' => false, 'expected' => false], |
| 89 | + ['area' => 'other', 'config' => true, 'methodResult' => false, 'expected' => false], |
| 90 | + ['area' => 'other', 'config' => true, 'methodResult' => true, 'expected' => true], |
| 91 | + ['area' => 'other', 'config' => false, 'methodResult' => true, 'expected' => true], |
| 92 | + ]; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Test afterCheck plugin result over original method result when no area code set. |
| 97 | + * |
| 98 | + * @param bool $config |
| 99 | + * @param bool $methodResult |
| 100 | + * @param bool $expectedResult |
| 101 | + * @return void |
| 102 | + * @dataProvider testAfterCheckDataProviderNoAreaCode |
| 103 | + */ |
| 104 | + public function testAfterCheckNoArea(bool $config, bool $methodResult, bool $expectedResult) |
| 105 | + { |
| 106 | + $this->appStateMock->expects($this->any()) |
| 107 | + ->method('getAreaCode') |
| 108 | + ->willThrowException(new LocalizedException(__('Are code not set'))); |
| 109 | + $this->assertEquals($expectedResult, $this->model->afterCheck($this->sessionStartCheckerMock, $methodResult)); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Data provider for testAfterCheck. |
| 114 | + * |
| 115 | + * @return array[] |
| 116 | + */ |
| 117 | + public function testAfterCheckDataProviderNoAreaCode() |
| 118 | + { |
| 119 | + return [ |
| 120 | + ['config' => true, 'methodResult' => true, 'expected' => false], |
| 121 | + ['config' => true, 'methodResult' => false, 'expected' => false], |
| 122 | + ['config' => false, 'methodResult' => true, 'expected' => false], |
| 123 | + ['config' => false, 'methodResult' => false, 'expected' => false], |
| 124 | + ]; |
| 125 | + } |
| 126 | +} |
0 commit comments