-
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: improve unit tests #219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenForgeProject\MageForge\Test\Unit\Exception; | ||
|
|
||
| use OpenForgeProject\MageForge\Exception\FetchLatestVersionException; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class FetchLatestVersionExceptionTest extends TestCase | ||
| { | ||
| public function testIsRuntimeException(): void | ||
| { | ||
| $exception = new FetchLatestVersionException('fetch failed'); | ||
|
|
||
| $this->assertInstanceOf(\RuntimeException::class, $exception); | ||
| $this->assertSame('fetch failed', $exception->getMessage()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenForgeProject\MageForge\Test\Unit\Model\Config\Source; | ||
|
|
||
| use Magento\Framework\Data\OptionSourceInterface; | ||
| use OpenForgeProject\MageForge\Model\Config\Source\InspectorTheme; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class InspectorThemeTest extends TestCase | ||
| { | ||
| public function testImplementsOptionSourceInterface(): void | ||
| { | ||
| $this->assertInstanceOf(OptionSourceInterface::class, new InspectorTheme()); | ||
| } | ||
|
|
||
| public function testReturnsAllInspectorThemes(): void | ||
| { | ||
| $options = (new InspectorTheme())->toOptionArray(); | ||
|
|
||
| $this->assertSame(['dark', 'light', 'auto'], array_column($options, 'value')); | ||
| } | ||
|
|
||
| public function testEveryOptionHasNonEmptyLabel(): void | ||
| { | ||
| foreach ((new InspectorTheme())->toOptionArray() as $option) { | ||
| $this->assertArrayHasKey('label', $option); | ||
| $this->assertIsString($option['label']); | ||
| $this->assertNotSame('', $option['label']); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenForgeProject\MageForge\Test\Unit\Model\Config\Source; | ||
|
|
||
| use Magento\Framework\Data\OptionSourceInterface; | ||
| use OpenForgeProject\MageForge\Model\Config\Source\ToolbarPosition; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class ToolbarPositionTest extends TestCase | ||
| { | ||
| public function testImplementsOptionSourceInterface(): void | ||
| { | ||
| $this->assertInstanceOf(OptionSourceInterface::class, new ToolbarPosition()); | ||
| } | ||
|
|
||
| public function testReturnsAllToolbarPositions(): void | ||
| { | ||
| $options = (new ToolbarPosition())->toOptionArray(); | ||
|
|
||
| $this->assertSame( | ||
| ['bottom-left', 'bottom-right', 'top-left', 'top-right'], | ||
| array_column($options, 'value'), | ||
| ); | ||
| } | ||
|
|
||
| public function testEveryOptionHasNonEmptyLabel(): void | ||
| { | ||
| foreach ((new ToolbarPosition())->toOptionArray() as $option) { | ||
| $this->assertArrayHasKey('label', $option); | ||
| $this->assertIsString($option['label']); | ||
| $this->assertNotSame('', $option['label']); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenForgeProject\MageForge\Test\Unit\Model; | ||
|
|
||
| use Magento\Framework\View\Design\Theme\ThemeList as MagentoThemeList; | ||
| use Magento\Framework\View\Design\ThemeInterface; | ||
| use OpenForgeProject\MageForge\Model\ThemeList; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class ThemeListTest extends TestCase | ||
| { | ||
| public function testReturnsAllThemesFromMagentoThemeList(): void | ||
| { | ||
| $themeOne = $this->createMock(ThemeInterface::class); | ||
| $themeTwo = $this->createMock(ThemeInterface::class); | ||
|
|
||
| $magentoThemeList = $this->createMock(MagentoThemeList::class); | ||
| $magentoThemeList | ||
| ->method('getItems') | ||
| ->willReturn(['frontend/Vendor/one' => $themeOne, 'frontend/Vendor/two' => $themeTwo]); | ||
|
|
||
| $themeList = new ThemeList($magentoThemeList); | ||
|
|
||
| $this->assertSame( | ||
| ['frontend/Vendor/one' => $themeOne, 'frontend/Vendor/two' => $themeTwo], | ||
| $themeList->getAllThemes(), | ||
| ); | ||
| } | ||
|
|
||
| public function testReturnsEmptyArrayWhenNoThemesExist(): void | ||
| { | ||
| $magentoThemeList = $this->createMock(MagentoThemeList::class); | ||
| $magentoThemeList->method('getItems')->willReturn([]); | ||
|
|
||
| $themeList = new ThemeList($magentoThemeList); | ||
|
|
||
| $this->assertSame([], $themeList->getAllThemes()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenForgeProject\MageForge\Test\Unit\Model; | ||
|
|
||
| use Magento\Framework\Component\ComponentRegistrar; | ||
| use Magento\Framework\Component\ComponentRegistrarInterface; | ||
| use OpenForgeProject\MageForge\Model\ThemePath; | ||
| use PHPUnit\Framework\MockObject\MockObject; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class ThemePathTest extends TestCase | ||
| { | ||
| private ComponentRegistrarInterface&MockObject $componentRegistrar; | ||
| private ThemePath $themePath; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->componentRegistrar = $this->createMock(ComponentRegistrarInterface::class); | ||
| $this->themePath = new ThemePath($this->componentRegistrar); | ||
| } | ||
|
|
||
| public function testReturnsFrontendThemePath(): void | ||
| { | ||
| $this->componentRegistrar | ||
| ->method('getPaths') | ||
| ->with(ComponentRegistrar::THEME) | ||
| ->willReturn([ | ||
| 'frontend/Vendor/theme' => '/app/design/frontend/Vendor/theme', | ||
| 'adminhtml/Vendor/theme' => '/app/design/adminhtml/Vendor/theme', | ||
| ]); | ||
|
|
||
| $this->assertSame('/app/design/frontend/Vendor/theme', $this->themePath->getPath('Vendor/theme')); | ||
| } | ||
|
|
||
| public function testFallsBackToAdminhtmlThemePath(): void | ||
| { | ||
| $this->componentRegistrar | ||
| ->method('getPaths') | ||
| ->willReturn([ | ||
| 'adminhtml/Vendor/backend' => '/app/design/adminhtml/Vendor/backend', | ||
| ]); | ||
|
|
||
| $this->assertSame('/app/design/adminhtml/Vendor/backend', $this->themePath->getPath('Vendor/backend')); | ||
| } | ||
|
|
||
| public function testReturnsNullForUnknownTheme(): void | ||
| { | ||
| $this->componentRegistrar | ||
| ->method('getPaths') | ||
| ->willReturn([ | ||
| 'frontend/Other/theme' => '/app/design/frontend/Other/theme', | ||
| ]); | ||
|
|
||
| $this->assertNull($this->themePath->getPath('Vendor/unknown')); | ||
| } | ||
|
|
||
| public function testReturnsNullWhenNoThemesRegistered(): void | ||
| { | ||
| $this->componentRegistrar->method('getPaths')->willReturn([]); | ||
|
|
||
| $this->assertNull($this->themePath->getPath('Vendor/theme')); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenForgeProject\MageForge\Test\Unit\Service; | ||
|
|
||
| use Magento\Framework\Shell; | ||
| use OpenForgeProject\MageForge\Service\CacheCleaner; | ||
| use PHPUnit\Framework\MockObject\MockObject; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
|
||
| class CacheCleanerTest extends TestCase | ||
| { | ||
| private Shell&MockObject $shell; | ||
| private SymfonyStyle&MockObject $io; | ||
| private CacheCleaner $cacheCleaner; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->shell = $this->createMock(Shell::class); | ||
| $this->io = $this->createMock(SymfonyStyle::class); | ||
| $this->cacheCleaner = new CacheCleaner($this->shell); | ||
| } | ||
|
|
||
| public function testCleansFrontendCacheTypes(): void | ||
| { | ||
| $this->shell | ||
| ->expects($this->once()) | ||
| ->method('execute') | ||
| ->with('bin/magento cache:clean full_page block_html layout translate'); | ||
|
|
||
| $this->assertTrue($this->cacheCleaner->clean($this->io, false)); | ||
| } | ||
|
|
||
| public function testPrintsProgressInVerboseMode(): void | ||
| { | ||
| $this->io->expects($this->once())->method('text')->with('Cleaning cache...'); | ||
| $this->io->expects($this->once())->method('success')->with('Cache cleaned successfully.'); | ||
|
|
||
| $this->assertTrue($this->cacheCleaner->clean($this->io, true)); | ||
| } | ||
|
|
||
| public function testStaysQuietWhenNotVerbose(): void | ||
| { | ||
| $this->io->expects($this->never())->method('text'); | ||
| $this->io->expects($this->never())->method('success'); | ||
|
|
||
| $this->assertTrue($this->cacheCleaner->clean($this->io, false)); | ||
| } | ||
|
|
||
| public function testReturnsFalseAndPrintsErrorWhenShellFails(): void | ||
| { | ||
| $this->shell->method('execute')->willThrowException(new \RuntimeException('cache backend gone')); | ||
| $this->io->expects($this->once())->method('error')->with('Failed to clean cache: cache backend gone'); | ||
|
|
||
| $this->assertFalse($this->cacheCleaner->clean($this->io, true)); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.