Skip to content

Commit

Permalink
Merge pull request #41 from sequra/feature/PAR-372-Implement-E2E-test…
Browse files Browse the repository at this point in the history
…s-for-Magento

Implement E2E tests for Magento
  • Loading branch information
mescalantea authored Feb 5, 2025
2 parents 158ea76 + d56cba3 commit 5e2d8bb
Show file tree
Hide file tree
Showing 21 changed files with 1,030 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Sequra\Helper\Api;

interface WebhooksInterface
{
/**
* @return mixed
*/
public function execute();
}
46 changes: 46 additions & 0 deletions .docker/magento/HelperModule/Sequra/Helper/Model/Api/Webhooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace Sequra\Helper\Model\Api;

use Sequra\Helper\Api\WebhooksInterface;
use Magento\Framework\App\ResourceConnection;
use Sequra\Helper\Model\Task\ClearConfigurationTask;
use Sequra\Helper\Model\Task\ConfigureDummyTask;
use Sequra\Helper\Model\Task\Task;

class Webhooks implements WebhooksInterface
{
protected $request;

/**
* Resource connection
*/
protected $conn;

/**
* Constructor
*/
public function __construct(ResourceConnection $resourceConnection) {
$this->conn = $resourceConnection;
}

public function execute()
{
return $this->getTaskForWebhook((string)($_GET['sq-webhook'] ?? null))->execute();
}


/**
* Get task for webhook
*/
private function getTaskForWebhook( $webhook ): Task {
$map = array(
// 'dummy_services_config' => ConfigureDummy_Service_Task::class,
'dummy_config' => ConfigureDummyTask::class,
'clear_config' => ClearConfigurationTask::class,
// 'remove_db_tables' => RemoveDbTablesTask::class

);
return ! isset( $map[ $webhook ] ) ? new Task($this->conn) : new $map[ $webhook ]($this->conn);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Task class
*
* @package SeQura/Helper
*/

namespace Sequra\Helper\Model\Task;

/**
* Task class
*/
class ClearConfigurationTask extends Task {

/**
* Execute the task
*
* @throws \Exception If the task fails
*/
public function execute( array $args = array() ) {
$this->removeStoreDataFromEntityTable();
return $this->httpSuccessResponse();
}
}

Large diffs are not rendered by default.

79 changes: 79 additions & 0 deletions .docker/magento/HelperModule/Sequra/Helper/Model/Task/Task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Task class
*
* @package SeQura/Helper
*/

namespace Sequra\Helper\Model\Task;
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Webapi\Exception;
use Magento\Framework\Phrase;
use Sequra\Core\Setup\DatabaseHandler;

/**
* Task class
*/
class Task {

/**
* Resource connection
*/
protected $conn;
protected $dbHandler;

/**
* Constructor
*/
public function __construct(ResourceConnection $resourceConnection) {
$this->conn = $resourceConnection;
$this->dbHandler = new DatabaseHandler($resourceConnection);
}

/**
* Execute the task
*
* @throws \Exception If the task fails
*/
public function execute( array $args = array() ) {
throw new \Exception( 'Task not implemented', 500 );
}

/**
* Recreate tables in the database
*/
protected function removeStoreDataFromEntityTable(): void {
$connection = $this->conn->getConnection();
// DELETE all rows not having type in ('Configuration', 'Process')
$connection->delete(
DatabaseHandler::SEQURA_ENTITY_TABLE,
[
'type NOT IN (?)' => ['Configuration', 'Process']
]
);
}

protected function timeToString(int $timestamp): string {
$time = (string) $timestamp;
// append 0s to the left until reach 11 characters
while (strlen($time) < 11) {
$time = '0' . $time;
}
return $time;
}

/**
* Response with an error message
*/
public function httpErrorResponse( string $message, int $error_code ) {
throw new Exception(new Phrase($message), $error_code, $error_code);
}

/**
* Response with an error message
*/
public function httpSuccessResponse() {
return ['success' => true, 'data' => ['message' => 'Task executed']];
}
}
1 change: 1 addition & 0 deletions .docker/magento/HelperModule/Sequra/Helper/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
</argument>
</arguments>
</type>
<preference for="Sequra\Helper\Api\WebhooksInterface" type="Sequra\Helper\Model\Api\Webhooks"/>
</config>
9 changes: 9 additions & 0 deletions .docker/magento/HelperModule/Sequra/Helper/etc/webapi.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/sequrahelper/webhook" method="POST">
<service class="Sequra\Helper\Api\WebhooksInterface" method="execute"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
docker-compose.yml export-ignore
docker-compose.override.sample.yml export-ignore
README.md export-ignore
package.json export-ignore
package-lock.json export-ignore
playwright.config.js export-ignore

# Exclude specific directories
.github export-ignore
Expand All @@ -14,6 +17,7 @@ bin export-ignore
docker-entrypoint-init.d export-ignore
.docker export-ignore
.github export-ignore
tests-e2e export-ignore

# Exclude scripts
*.sh export-ignore
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ docker-compose.override.yml
.env
default.vcl
id_rsa
.magento-src
.magento-src

# Playwright
node_modules/
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
41 changes: 41 additions & 0 deletions bin/playwright
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash
BASEDIR="$(dirname $(realpath $0))/.."

cd "${BASEDIR}" || exit
set -o allexport
source .env
set +o allexport

if [ -z "$PUBLIC_URL" ]; then
PUBLIC_URL="$M2_URL"
fi

wait_for() {
local retry=60
local timeout=1
local start=$(date +%s)

while [ $(($(date +%s) - $start)) -lt $retry ]; do
if "$@" > /dev/null 2>&1; then
return 0
fi
sleep $timeout
done
return 1
}
echo "🚀 Waiting for ngrok tunnel to be ready..."
result=$(wait_for curl -H "ngrok-skip-browser-warning: 1" -s -o /dev/null --head --fail "${PUBLIC_URL}")
if [ "$result" == "1" ]; then
echo "❌ Magento is not available at: ${PUBLIC_URL}"
exit 1
fi
echo "✅ Magento is available at: ${PUBLIC_URL}"

# Check if --headed is passed
if [[ "$@" == *"--headed"* || "$@" == *"--ui"* ]]; then
npx playwright test $@
else
docker run \
--env-file "${BASEDIR}"/.env \
-it --rm -v "${BASEDIR}":/app -w /app mcr.microsoft.com/playwright:v1.49.1-jammy bash -c "npx playwright test $@"
fi
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@
"docker-compose.yml",
"README.md",
"setup.sh",
"teardown.sh"
"teardown.sh",
"tests-e2e",
"package.json",
"package-lock.json",
"playwright.config.js"
]
},
"require-dev": {
Expand Down
97 changes: 97 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "magento2-core",
"version": "1.0.0",
"type": "module",
"description": "1. [About seQura](#about-sequra) 2. [Installation guide](https://sequra.atlassian.net/wiki/spaces/DOC/pages/1377304583/MAGENTO+2) 3. [Sign-up](#sign-up) 4. [For developers](#for-developers)",
"main": "index.js",
"scripts": {},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@playwright/test": "^1.49.1",
"@types/node": "^22.10.7",
"playwright-fixture-for-plugins": "github:sequra/playwright-fixture-for-plugins"
}
}
Loading

0 comments on commit 5e2d8bb

Please sign in to comment.