Skip to content

Commit 3bda7c7

Browse files
authored
Merge pull request #9 from rocketweb/ICP-11-integration-tests
ICP-11 added unit tests
2 parents 5dc5d4d + 6de5bff commit 3bda7c7

14 files changed

+860
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

.github/scripts/pre-install-script.sh

Whitespace-only changes.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: RW Integration Tests
2+
on:
3+
pull_request:
4+
types:
5+
- opened
6+
- reopened
7+
- synchronize
8+
- ready_for_review
9+
10+
jobs:
11+
integration-tests:
12+
name: Magento 2 Integration Tests
13+
runs-on: ubuntu-latest
14+
services:
15+
mysql:
16+
image: mysql:5.7
17+
env:
18+
MYSQL_ROOT_PASSWORD: root
19+
MYSQL_SQL_TO_RUN: 'GRANT ALL ON *.* TO "root"@"%";'
20+
ports:
21+
- 3306:3306
22+
options: --tmpfs /tmp:rw --tmpfs /var/lib/mysql:rw --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
23+
es:
24+
image: docker.io/wardenenv/elasticsearch:7.8
25+
ports:
26+
- 9200:9200
27+
env:
28+
'discovery.type': single-node
29+
'xpack.security.enabled': false
30+
ES_JAVA_OPTS: "-Xms64m -Xmx512m"
31+
options: --health-cmd="curl localhost:9200/_cluster/health?wait_for_status=yellow&timeout=60s" --health-interval=10s --health-timeout=5s --health-retries=3
32+
steps:
33+
- uses: actions/checkout@v4
34+
- name: M2 Integration Tests with Magento 2 (Php8.1)
35+
uses: extdn/github-actions-m2/magento-integration-tests/8.1@master
36+
with:
37+
MAGENTO_PRE_INSTALL_SCRIPT: ./.github/scripts/pre-install-script.sh
38+
MAGENTO_POST_INSTALL_SCRIPT: ./.github/scripts/post-install-script.sh
39+
MODULE_NAME: RocketWeb_CmsImportExport
40+
COMPOSER_NAME: rocketweb/module-cms-import-export
41+
MAGENTO_VERSION: '2.4.5-p5'
42+
PHPUNIT_FILE: './phpunit.rw.xml'
43+
COMPOSER_VERSION: 2
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RocketWeb\CmsImportExport\Test\Integration;
6+
7+
use Magento\Framework\Exception\FileSystemException;
8+
use Magento\Store\Model\StoreManagerInterface;
9+
use Magento\TestFramework\App\Filesystem;
10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\Filesystem\Directory\WriteInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use PHPUnit\Framework\TestCase;
14+
use RocketWeb\CmsImportExport\Model\Service\DumpCmsDataService;
15+
16+
/**
17+
* @magentoAppIsolation disabled
18+
* @magentoDbIsolation disabled
19+
* @magentoAppArea adminhtml
20+
*/
21+
class ExportByStoreScopeEntitiesTest extends TestCase
22+
{
23+
protected ?DumpCmsDataService $exporter;
24+
protected ?string $exportDirPath;
25+
protected ?WriteInterface $varDirectory;
26+
27+
/**
28+
* @return void
29+
* @throws FileSystemException
30+
*/
31+
protected function setUp(): void
32+
{
33+
$objectManager = Bootstrap::getObjectManager();
34+
$fileSystem = $objectManager->create(Filesystem::class);
35+
$this->exporter = $objectManager->create(DumpCmsDataService::class);
36+
$this->varDirectory = $fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR);
37+
$this->exportDirPath = $this->varDirectory->getAbsolutePath() . 'sync_cms_data';
38+
$storeManager = $objectManager->create(StoreManagerInterface::class);
39+
if ($storeManager->getStore()->getId() != '0') {
40+
$storeManager->setCurrentStore(0);
41+
}
42+
}
43+
44+
public function getExecuteCases(): array
45+
{
46+
return [
47+
['block', 'imported_cms_block_multistore', ['default', 'second_store_view']],
48+
['page', 'imported_cms_page_multistore', ['default', 'second_store_view']],
49+
];
50+
}
51+
52+
/**
53+
* @param string $type
54+
* @param string $identifier
55+
* @param array $scopes
56+
* @return void
57+
* @throws FileSystemException
58+
* @magentoDataFixture RocketWeb_CmsImportExport::_files/multiple_websites_with_store_groups_stores.php
59+
* @dataProvider getExecuteCases
60+
* @magentoDataFixture RocketWeb_CmsImportExport::_files/multi_store_block.php
61+
* @magentoDataFixture RocketWeb_CmsImportExport::_files/multi_store_page.php
62+
*/
63+
public function testCmsExportedCorrectlyByScope(
64+
string $type,
65+
string $identifier,
66+
array $scopes
67+
) {
68+
$this->exporter->execute([$type], [$identifier], false);
69+
//validate that the export folder exists
70+
self::assertTrue(
71+
$this->varDirectory->isExist($this->exportDirPath),
72+
__CLASS__ . ' Export directory does not exist'
73+
);
74+
75+
$filename = sprintf(
76+
"$identifier---%s.json",
77+
implode('---', $scopes)
78+
);
79+
$filepath = sprintf(
80+
'%s/%s/%s',
81+
$this->exportDirPath,
82+
$type === 'block' ? 'cms/blocks' : 'cms/pages',
83+
$filename
84+
);
85+
//validate file was created successfully
86+
self::assertTrue(
87+
$this->varDirectory->isFile($filepath),
88+
__CLASS__ . " $filename does not exist"
89+
);
90+
91+
$decoded = json_decode(
92+
$this->varDirectory->readFile($filepath),
93+
true
94+
);
95+
//validate file structure was created successfully
96+
self::assertArrayHasKey(
97+
'identifier',
98+
$decoded,
99+
__CLASS__ . " $filename does not have the correct structure"
100+
);
101+
self::assertTrue(
102+
$decoded['identifier'] === $identifier,
103+
__CLASS__ . " Invalid identifiers, file: {$decoded['identifier']}, provided $identifier"
104+
);
105+
}
106+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RocketWeb\CmsImportExport\Test\Integration;
6+
7+
use Magento\Framework\Exception\FileSystemException;
8+
use Magento\TestFramework\App\Filesystem;
9+
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\Framework\Filesystem\Directory\WriteInterface;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use PHPUnit\Framework\TestCase;
13+
use RocketWeb\CmsImportExport\Model\Service\DumpCmsDataService;
14+
15+
/**
16+
* @magentoAppIsolation enabled
17+
* @magentoDbIsolation enabled
18+
*/
19+
class ExportEntitiesTest extends TestCase
20+
{
21+
protected ?DumpCmsDataService $exporter;
22+
protected ?string $exportDirPath;
23+
protected ?WriteInterface $varDirectory;
24+
25+
protected function setUp(): void
26+
{
27+
$objectManager = Bootstrap::getObjectManager();
28+
$fileSystem = $objectManager->create(Filesystem::class);
29+
$this->exporter = $objectManager->create(DumpCmsDataService::class);
30+
$this->varDirectory = $fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR);
31+
$this->exportDirPath = $this->varDirectory->getAbsolutePath() . 'sync_cms_data';
32+
}
33+
34+
public function getExecuteCases(): array
35+
{
36+
return [
37+
['block'],
38+
['page'],
39+
];
40+
}
41+
42+
/**
43+
* @return void
44+
* @throws FileSystemException
45+
*/
46+
protected function tearDown(): void
47+
{
48+
if ($this->varDirectory->isExist($this->exportDirPath))
49+
$this->varDirectory->delete($this->exportDirPath);
50+
}
51+
52+
/**
53+
* @param string $type
54+
* @return void
55+
* @throws FileSystemException
56+
* @dataProvider getExecuteCases
57+
* @magentoDataFixture Magento/Cms/_files/block.php
58+
* @magentoDataFixture Magento/Cms/_files/noroute.php
59+
*/
60+
public function testCmsExportedCorrectly(string $type)
61+
{
62+
$this->exporter->execute([$type], null, false);
63+
//validate that the export folder exists
64+
self::assertTrue(
65+
$this->varDirectory->isExist($this->exportDirPath)
66+
);
67+
if ($type === 'block') {
68+
$filepath = sprintf(
69+
'%s/%s/%s',
70+
$this->exportDirPath,
71+
'cms/blocks',
72+
'fixture_block---default.json'
73+
);
74+
//validate file was created successfully
75+
self::assertTrue(
76+
$this->varDirectory->isFile($filepath)
77+
);
78+
$decoded = json_decode(
79+
$this->varDirectory->readFile($filepath),
80+
true
81+
);
82+
//validate file structure was created successfully
83+
self::assertArrayHasKey('identifier', $decoded);
84+
self::assertTrue($decoded['identifier'] === 'fixture_block');
85+
}
86+
87+
if ($type === 'page') {
88+
$filepath = sprintf(
89+
'%s/%s/%s',
90+
$this->exportDirPath,
91+
'cms/pages',
92+
'no-route---_all_.json'
93+
);
94+
//validate file was created successfully
95+
self::assertTrue(
96+
$this->varDirectory->isFile($filepath)
97+
);
98+
$decoded = json_decode(
99+
$this->varDirectory->readFile($filepath),
100+
true
101+
);
102+
//validate file structure was created successfully
103+
self::assertArrayHasKey('identifier', $decoded);
104+
self::assertTrue($decoded['identifier'] === 'no-route');
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)