Skip to content

Commit

Permalink
Icecat connector PHPUnit tests (#26)
Browse files Browse the repository at this point in the history
* removes unused service
* prepares CI environment for integration tests
* adds integration tests
* Icecat downloader now uncompresses the downloaded file in the same directory as the compressed file
  • Loading branch information
mmetayer authored and jmleroux committed Sep 14, 2017
1 parent 5223eca commit 7ecf291
Show file tree
Hide file tree
Showing 17 changed files with 559 additions and 72 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.1.8
## Improvements
- MKP-478: Icecat connector integration tests

# 1.1.7
## Improvements
- MKP-477: Icecat connector PHPSpec tests
Expand Down
17 changes: 11 additions & 6 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!groovy

def launchUnitTests = "yes"
def launchIntegrationTests = "no"

class Globals {
Expand All @@ -12,11 +11,9 @@ class Globals {
stage("Checkout") {
milestone 1
if (env.BRANCH_NAME =~ /^PR-/) {
userInput = input(message: 'Launch tests?', parameters: [
launchIntegrationTests = input(message: 'Launch tests?', parameters: [
choice(choices: 'yes\nno', description: 'Run integration tests', name: 'launchIntegrationTests'),
])

launchIntegrationTests = userInput['launchIntegrationTests']
}

milestone 2
Expand All @@ -37,7 +34,7 @@ if (launchIntegrationTests.equals("yes")) {
stage("Integration tests") {
def tasks = [:]

tasks["phpunit-5.6-ce"] = {runIntegrationTest("5.6", "${Globals.mysqlVersion}")}
tasks["phpunit-5.6-ee"] = {runIntegrationTest("5.6", "${Globals.mysqlVersion}")}

parallel tasks
}
Expand Down Expand Up @@ -79,7 +76,7 @@ def runIntegrationTest(phpVersion, mysqlVersion) {
sh """
docker exec akeneo composer config repositories.icecat '{"type": "vcs", "url": "[email protected]:akeneo/icecat-connector.git", "branch": "master"}'
docker exec akeneo composer require --no-update akeneo/icecat-connector:${Globals.extensionBranch}
docker exec akeneo php -d memory_limit=3G /usr/local/bin/composer update --ignore-platform-reqs --optimize-autoloader --no-interaction --no-progress --prefer-dist
docker exec akeneo php -d memory_limit=3G /usr/local/bin/composer update --optimize-autoloader --no-interaction --no-progress --prefer-dist
"""

dir("vendor/akeneo/icecat-connector") {
Expand All @@ -99,10 +96,18 @@ def runIntegrationTest(phpVersion, mysqlVersion) {

sh """
cp vendor/akeneo/icecat-connector/src/Resources/jenkins/parameters_test.yml app/config/parameters_test.yml
cat vendor/akeneo/icecat-connector/src/Resources/jenkins/routing.yml >> app/config/routing.yml
cp vendor/akeneo/icecat-connector/src/Resources/jenkins/phpunit.xml app/phpunit.xml
cat vendor/akeneo/icecat-connector/src/Resources/jenkins/config_test.yml >> app/config/config_test.yml
"""

sh "docker exec akeneo app/console pim:install --force --env=test"

sh 'mkdir -p app/build/logs/'
sh 'docker exec akeneo bin/phpunit -c app/phpunit.xml --log-junit app/build/logs/phpunit.xml'

} finally {
junit "app/build/logs/*.xml"
deleteDir()
}
}
Expand Down

This file was deleted.

3 changes: 0 additions & 3 deletions src/Resources/config/jobs.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
parameters:
pim_icecat_connector.job.job_parameters.form_configuration_provider.simple_csv.class: Pim\Bundle\IcecatConnectorBundle\JobParameters\FormConfigurationProvider\SimpleCsvConfiguration

services:
#--------------------------------------------------------------------------
# prepare features CSV mapping file job
Expand Down
11 changes: 11 additions & 0 deletions src/Resources/jenkins/config_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pim_icecat_connector:
settings:
credentials_username:
value: akeneo-test
scope: app
credentials_password:
value: akeneo-test44
scope: app
ean_attribute:
value: icecat_ean
scope: app
6 changes: 6 additions & 0 deletions src/Resources/jenkins/featuresList.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
feature_id;pim_attribute_code;ignore_flag;feature_type;feature_name;feature_description;feature_unit
1006;icecat_numeric_keypad;0;y_n;Numeric keypad;Small, palm-sized, seventeen key section of a computer keyboard, usually on the very far right. The numeric keypad features digits 0 to 9, addition (+), subtraction (-), multiplication (*) and division (/) symbols, a decimal point (.) and Num Lock and Enter keys. Laptop keyboards often do not have a numpad, but may provide numpad input by holding a modifier key (typically labelled "Fn") and operating keys on the standard keyboard.;
11379;icecat_processor_frequency;0;numerical;Microprocessor frequency;;MHz
3233;icecat_operating_system;0;dropdown;Operating system installed;Type of operating system on a device e.g. IOS on Apple devices, Android for mobile devices.;
11381;icecat_installed_ram;0;numerical;Internal memory;A computer's memory which is directly accessible to the CPU.;
21719;icecat_processor_series;0;dropdown;Processor series;
6 changes: 6 additions & 0 deletions src/Resources/jenkins/mapping.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
feature_id;pim_attribute_code
1006;icecat_numeric_keypad
11379;icecat_processor_frequency
3233;icecat_operating_system
11381;icecat_installed_ram
21719;icecat_processor_series
1 change: 1 addition & 0 deletions src/Resources/jenkins/parameters_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ parameters:
database_password: akeneo_pim
locale: en
secret: ThisTokenIsNotSoSecretChangeIt
installer_data: PimEnterpriseInstallerBundle:minimal
22 changes: 22 additions & 0 deletions src/Resources/jenkins/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
syntaxCheck = "false"
bootstrap = "./autoload.php" >

<testsuites>
<testsuite name="Icecat_Connector_Integration_Tests">
<directory suffix="Test.php">../vendor/akeneo/icecat-connector/src/Tests</directory>
</testsuite>
</testsuites>

</phpunit>
4 changes: 4 additions & 0 deletions src/Resources/jenkins/routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pim_icecat_connector:
resource: "@PimIcecatConnectorBundle/Resources/config/routing.yml"
prefix: /icecat

189 changes: 189 additions & 0 deletions src/Tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php

namespace Pim\Bundle\IcecatConnectorBundle\Tests;

use Akeneo\Bundle\BatchBundle\Command\BatchCommand;
use Akeneo\Bundle\BatchBundle\Command\CreateJobCommand;
use Pim\Component\Catalog\AttributeTypes;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;

/**
* @author Mathias METAYER <[email protected]>
* @copyright 2017 Akeneo SAS (http://www.akeneo.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
abstract class AbstractTestCase extends KernelTestCase
{
/** @var array */
private $credentials = [
'username' => null,
'password' => null,
];

public function setUp()
{
static::bootKernel(['debug' => false]);
$config = $this->get('oro_config.global');
$this->credentials['username'] = $config->get('pim_icecat_connector.credentials_username');
$this->credentials['password'] = $config->get('pim_icecat_connector.credentials_password');
$this->cleanData();
}

/**
* @param $serviceName
* @return object
*/
protected function get($serviceName)
{
return static::$kernel->getContainer()->get($serviceName);
}

/**
* @param string $name
* @return mixed
*/
protected function getParameter($name)
{
return static::$kernel->getContainer()->getParameter($name);
}

/**
* @return array
*/
protected function getCredentials()
{
return $this->credentials;
}

/**
* @param array $input
* @return int
*/
protected function runBatchCommand(array $input = [])
{
$application = new Application(static::$kernel);
$batchCommand = new BatchCommand();
$batchCommand->setContainer(static::$kernel->getContainer());
$application->add($batchCommand);

if (!array_key_exists('--no-log', $input)) {
$input['--no-log'] = true;
}

$batch = $application->find('akeneo:batch:job');
return $batch->run(new ArrayInput($input), new NullOutput());
}

/**
* Creates an import profile
*
* @param string $connector
* @param string $job
*/
protected function createImportProfile($connector, $job)
{
$application = new Application(static::$kernel);
$batchCommand = new CreateJobCommand();
$batchCommand->setContainer(static::$kernel->getContainer());
$application->add($batchCommand);
$cmd = $application->find('akeneo:batch:create-job');
$input = new ArrayInput([
'connector' => $connector,
'job' => $job,
'type' => 'import',
'code' => $job,
]);
$cmd->run($input, new NullOutput());
}

protected function loadData()
{
$attributes = [
[
'code' => 'icecat_ean',
'type' => AttributeTypes::TEXT,
'unique' => true,
],
[
'code' => 'icecat_numeric_keypad',
'type' => AttributeTypes::BOOLEAN,
],
[
'code' => 'icecat_processor_frequency',
'type' => AttributeTypes::METRIC,
'metric_family' => 'Frequency',
'negative_allowed' => false,
'decimals_allowed' => true,
'default_metric_unit' => 'MEGAHERTZ',
],
[
'code' => 'icecat_installed_ram',
'type' => AttributeTypes::METRIC,
'metric_family' => 'Binary',
'negative_allowed' => false,
'decimals_allowed' => true,
'default_metric_unit' => 'GIGABYTE',
],
[
'code' => 'icecat_processor_series',
'type' => AttributeTypes::TEXT,
],
[
'code' => 'icecat_operating_system',
'type' => AttributeTypes::TEXT,
],
];

$attributeFactory = $this->get('pim_catalog.factory.attribute');
$attributeUpdater = $this->get('pim_catalog.updater.attribute');
$attributeSaver = $this->get('pim_catalog.saver.attribute');

foreach ($attributes as $data) {
$attribute = $attributeFactory->create();
$attributeUpdater->update($attribute, $data);
$attributeSaver->save($attribute);
}

$family = $this->get('pim_catalog.factory.family')->create();
$this->get('pim_catalog.updater.family')->update($family, [
'code' => 'icecat_laptop',
'attribute_as_label' => 'sku',
'attributes' => [
'icecat_ean',
'icecat_numeric_keypad',
'icecat_installed_ram',
'icecat_processor_series',
'icecat_processor_frequency',
'icecat_operating_system',
],
]);
$this->get('pim_catalog.saver.family')->save($family);
}

/**
* Removes unneeded data
*/
private function cleanData()
{
// remove existing job instances
$em = $this->get('doctrine.orm.entity_manager');
$jobInstances = $em->getRepository($this->getParameter('akeneo_batch.entity.job_instance.class'))
->findBy(['type' => 'import']);
$this->get('akeneo_batch.remover.job_instance')->removeAll($jobInstances);

// removes products
$products = $this->get('pim_catalog.repository.product')->findAll();
$this->get('pim_catalog.remover.product')->removeAll($products);

// remove non identifiers attributes
$attributes = $this->get('pim_catalog.repository.attribute')->getNonIdentifierAttributes();
$this->get('pim_catalog.remover.attribute')->removeAll($attributes);

// removes families
$families = $this->get('pim_catalog.repository.family')->findAll();
$this->get('pim_catalog.remover.family')->removeAll($families);
}
}
Loading

0 comments on commit 7ecf291

Please sign in to comment.