This question comes as a complement from issue already posted in Magento 2 and TDD good practices to implement tests
After I implemented my own understanding of the solution provided.
I’m getting the error :
vendor/bin/phpunit -v -c dev/tests/integration/datasolution_phpunit.xml
Exception: Could not connect to the Amqp Server.
In InstallCommand.php line 282:
[InvalidArgumentException]
Parameter validation failed
Exception trace:
at /var/www/magento/setup/src/Magento/Setup/Console/Command/InstallCommand.php:282
MagentoSetupConsoleCommandInstallCommand->initialize() at /var/www/magento/vendor/symfony/console/Command/Command.php:264
SymfonyComponentConsoleCommandCommand->run() at /var/www/magento/vendor/symfony/console/Application.php:1040
SymfonyComponentConsoleApplication->doRunCommand() at /var/www/magento/vendor/symfony/console/Application.php:301
SymfonyComponentConsoleApplication->doRun() at /var/www/magento/vendor/magento/framework/Console/Cli.php:116
MagentoFrameworkConsoleCli->doRun() at /var/www/magento/vendor/symfony/console/Application.php:171
SymfonyComponentConsoleApplication->run() at /var/www/magento/bin/magento:23
I’m not sure to understand where the error comes from…I’m also wondering if it’s really normal that it’s playing the setup script; nut i guess it’s kinda installing magento on the new database…so I guess it is ? Unsure though. Thanks for any kind of assistance !
Anyway, here is the detail of what i did.
As a start, I created a bunch of shell script setting ma database and configuration files automatically so as I can use the sample example on any docker project…but I’ll pass the details and show the results.
As a resume I have my custom file xml unit file automatically generated
dev/tests/integration/aims_phpunit.xml
With his custom test suites
<testsuite name="Your test suite">
<directory>../../../app/code/Aims/Testing/Test/Integration</directory>
</testsuite>
I also created a custom database with users and filled these data inside.
dev/tests/integration/etc/install-config-mysql.php
return [
'db-host' => 'db.nes.internal',
'db-user' => 'magento_test_user',
'db-password' => 'magento_test_pwd',
'db-name' => 'magento_test',
'db-prefix' => '',
'backend-frontname' => 'backend',
'search-engine' => 'opensearch',
'opensearch-host' => 'localhost',
'opensearch-port' => 9200,
'admin-user' => MagentoTestFrameworkBootstrap::ADMIN_NAME,
'admin-password' => MagentoTestFrameworkBootstrap::ADMIN_PASSWORD,
'admin-email' => MagentoTestFrameworkBootstrap::ADMIN_EMAIL,
'admin-firstname' => MagentoTestFrameworkBootstrap::ADMIN_FIRSTNAME,
'admin-lastname' => MagentoTestFrameworkBootstrap::ADMIN_LASTNAME,
'amqp-host' => 'localhost',
'amqp-port' => '5672',
'amqp-user' => 'guest',
'amqp-password' => 'guest',
'consumers-wait-for-messages' => '0',
];
Db and user being created by following script (but right now witht the test i’m doing there is still no interaction with data base)
#!/bin/bash
DB_NAME="magento_test"
DB_USER="magento_test_user"
DB_PASSWORD="magento_test_pwd"
echo "Configure database test";
echo "Checking if database '$DB_NAME' exists..."
if mysql -u root -p"$MYSQL_ROOT_PASSWORD" -e "USE $DB_NAME"; then
echo "Database '$DB_NAME' already exists. Skipping creation."
else
echo "Database '$DB_NAME' does not exist. Creating database..."
mysql -u root -p"$MYSQL_ROOT_PASSWORD" -e "CREATE DATABASE $DB_NAME;"
echo "Database '$DB_NAME' created."
fi
echo "Checking if user '$DB_USER' exists..."
result=$(mysql -u root -p"$MYSQL_ROOT_PASSWORD" -N -s -e "SELECT 1 FROM mysql.user WHERE user = '$DB_USER'")
if [ "$result" = "1" ]; then
echo "User '$DB_USER' already exists. Skipping user creation."
else
echo "User '$DB_USER' does not exist. Creating user..."
mysql -u root -p"$MYSQL_ROOT_PASSWORD" -e "CREATE USER '$DB_USER'@'%' IDENTIFIED BY '$DB_PASSWORD';"
echo "User '$DB_USER' created."
fi
echo "Granting privileges to user '$DB_USER' on database '$DB_NAME'..."
mysql -u root -p"$MYSQL_ROOT_PASSWORD" -e "GRANT ALL ON $DB_NAME.* TO '$DB_USER'@'%';"
echo "Privileges granted."
echo "End configuration database test";
Then I have the class subsject to the test which is quite simple as you will see
<?php
namespace AimsTestingHelper;
use MagentoFrameworkAppHelperAbstractHelper;
use MagentoFrameworkAppHelperContext;
class TestMethodsClass extends AbstractHelper
{
protected $value;
public function __construct(Context $context)
{
parent::__construct($context);
$this->value = 0;
}
/**
* @return int
*/
public function add()
{
$this->value++;
return $this->value;
}
/**
* @return int
*/
public function sub()
{
$this->value--;
return $this->value;
}
}
And finally; the test class itself
<?php
namespace AimsTestingTestIntegration;
use MagentoTestFrameworkHelperBootstrap;
use AimsTestingHelperTestMethodsClass;
use PHPUnitFrameworkTestCase;
class PocIntegrationTest extends TestCase
{
private $helperClass;
protected function setUp(): void
{
$this->helperClass = Bootstrap::getObjectManager()->get(TestMethodsClass::class);
}
public function testAddAndSub()
{
$resultOne = $this->helperClass->add();
$expectedValue = 1;
$resultTwo = $this->helperClass->add();
$expectedValueTwo = 2;
$resultThree = $this->helperClass->sub();
$expectedValueThree = 1;
$this->assertEquals($expectedValue, $resultOne);
$this->assertEquals($expectedValueTwo, $resultTwo);
$this->assertEquals($expectedValueThree, $resultThree);
}
}
EDIT : If I remove the amqp config form the file the magento 2 install in starting before failign due to the table not being there…which is logic as it’s a new database and we are running the install…makes no sense to me
Starting Magento installation:
File permissions check...
[Progress: 1 / 1655]
Required extensions check...
[Progress: 2 / 1655]
Enabling Maintenance Mode...
[Progress: 3 / 1655]
Installing deployment configuration...
[Progress: 4 / 1655]
Installing database schema:
In Mysql.php line 603:
[MagentoFrameworkDBAdapterTableNotFoundException (1146)]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'magento_test.eav_entity_type' doesn't exist, query was: SELECT `main_table`.* FROM `eav_entity_type` AS `main_table`