From 489a3e59ab7784a05f494e5ef7a07e2110642d76 Mon Sep 17 00:00:00 2001
From: Vadim Justus <v.justus@techdivision.com>
Date: Fri, 8 Nov 2019 17:25:30 +0100
Subject: [PATCH] Add example integration test with fixtures

---
 Test/Integration/Model/ExampleTest.php        | 44 +++++++++
 Test/Integration/_file/some-fixture.php       | 96 +++++++++++++++++++
 .../_file/some-fixture_rollback.php           | 42 ++++++++
 composer.json                                 | 17 ++++
 etc/module.xml                                | 24 +++++
 registration.php                              | 16 ++++
 6 files changed, 239 insertions(+)
 create mode 100644 Test/Integration/Model/ExampleTest.php
 create mode 100644 Test/Integration/_file/some-fixture.php
 create mode 100644 Test/Integration/_file/some-fixture_rollback.php
 create mode 100644 composer.json
 create mode 100644 etc/module.xml
 create mode 100644 registration.php

diff --git a/Test/Integration/Model/ExampleTest.php b/Test/Integration/Model/ExampleTest.php
new file mode 100644
index 0000000..b917162
--- /dev/null
+++ b/Test/Integration/Model/ExampleTest.php
@@ -0,0 +1,44 @@
+<?php
+/**
+ * Copyright (c) 2019 TechDivision GmbH
+ * All rights reserved
+ *
+ * This product includes proprietary software developed at TechDivision GmbH, Germany
+ * For more information see http://www.techdivision.com/
+ *
+ * To obtain a valid license for using this software please contact us at
+ * license@techdivision.com
+ */
+declare(strict_types=1);
+
+namespace Met\ExampleMagento2ModuleTesting\Test\Integration\Model;
+
+use Magento\Framework\Api\SearchCriteriaBuilder;
+use Magento\Sales\Api\OrderRepositoryInterface;
+use Magento\TestFramework\Helper\Bootstrap;
+use PHPUnit\Framework\TestCase;
+
+/**
+ * @copyright  Copyright (c) 2019 TechDivision GmbH (http://www.techdivision.com)
+ * @link       http://www.techdivision.com/
+ * @author     Vadim Justus <v.justus@techdivision.com>
+ */
+class PipelineInitializer extends TestCase
+{
+    /**
+     * @magentoDataFixtures Met_ExampleMagento2ModuleTesting::Test/Integration/_files/some-fixture.php
+     */
+    public function testFoobar()
+    {
+        /** @var SearchCriteriaBuilder $searchCriteriaBuilder */
+        $searchCriteriaBuilder = Bootstrap::getObjectManager()->create(SearchCriteriaBuilder::class);
+
+        /** @var OrderRepositoryInterface $orderRepository */
+        $orderRepository = Bootstrap::getObjectManager()->get(OrderRepositoryInterface::class);
+
+        $searchCriteria = $searchCriteriaBuilder->create();
+        $orders = $orderRepository->getList($searchCriteria)->getItems();
+
+        $this->assertCount(6, $orders);
+    }
+}
diff --git a/Test/Integration/_file/some-fixture.php b/Test/Integration/_file/some-fixture.php
new file mode 100644
index 0000000..28b16fa
--- /dev/null
+++ b/Test/Integration/_file/some-fixture.php
@@ -0,0 +1,96 @@
+<?php
+/**
+ * Copyright (c) 2019 TechDivision GmbH
+ * All rights reserved
+ *
+ * This product includes proprietary software developed at TechDivision GmbH, Germany
+ * For more information see http://www.techdivision.com/
+ *
+ * To obtain a valid license for using this software please contact us at
+ * license@techdivision.com
+ */
+declare(strict_types=1);
+
+use Magento\Catalog\Model\Product\Type;
+use Magento\Sales\Api\OrderRepositoryInterface;
+use Magento\Sales\Model\Order;
+use Magento\Sales\Model\Order\Address;
+use Magento\Sales\Model\Order\Item;
+use Magento\Sales\Model\Order\Payment;
+use Magento\Store\Model\StoreManagerInterface;
+use Magento\TestFramework\Helper\Bootstrap;
+
+$addressData = [
+    'firstname' => 'guest',
+    'lastname' => 'guest',
+    'email' => 'customer@example.com',
+    'street' => 'street',
+    'city' => 'Los Angeles',
+    'region' => 'CA',
+    'postcode' => '1',
+    'country_id' => 'US',
+    'telephone' => '1'
+];
+
+$paymentMethods = [
+    0 => 'checkmo',
+    1 => 'test_cc',
+    2 => 'test_cc',
+    3 => 'invoice',
+    4 => 'paypal_expr',
+    5 => 'paypal_expr',
+];
+
+$orderStatus = [
+    0 => 'pending',
+    1 => 'pending',
+    2 => 'processing',
+    3 => 'pending',
+    4 => 'processing',
+    5 => 'processing',
+];
+
+for ($counter = 0; $counter <= 5; $counter++) {
+    /** @var $billingAddress Address */
+    $billingAddress = Bootstrap::getObjectManager()->create(Address::class, ['data' => $addressData]);
+    $billingAddress->setAddressType('billing');
+
+    $shippingAddress = clone $billingAddress;
+    $shippingAddress->setId(null)->setAddressType('shipping');
+
+    /** @var $payment Payment */
+    $payment = Bootstrap::getObjectManager()->create(Payment::class);
+    $payment->setMethod($paymentMethods[$counter]);
+
+    /** @var $orderItem Item */
+    $orderItem = Bootstrap::getObjectManager()->create(Item::class);
+    $orderItem->setProductId($counter)
+        ->setProductType(Type::TYPE_SIMPLE)
+        ->setName('product name')
+        ->setSku('MET0000' . $counter)
+        ->setBasePrice(100 + $counter)
+        ->setQtyOrdered(1)
+        ->setQtyShipped(1)
+        ->setIsQtyDecimal(true);
+
+    /** @var StoreManagerInterface $storeManager */
+    $storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
+
+    /** @var $order Order */
+    $order = Bootstrap::getObjectManager()->create(Order::class);
+    $order->addItem($orderItem)
+        ->setIncrementId('METEXAMPLE-10' . $counter)
+        ->setSubtotal(100 + $counter)
+        ->setBaseSubtotal(100 + $counter)
+        ->setCustomerIsGuest(true)
+        ->setCustomerEmail('admin@example.com')
+        ->setStatus($orderStatus[$counter])
+        ->setBillingAddress($billingAddress)
+        ->setShippingAddress($shippingAddress)
+        ->setStoreId($storeManager->getStore()->getId())
+        ->setPayment($payment);
+
+    /** @var OrderRepositoryInterface $orderRepository */
+    $orderRepository = Bootstrap::getObjectManager()->get(OrderRepositoryInterface::class);
+    $orderRepository->save($order);
+}
diff --git a/Test/Integration/_file/some-fixture_rollback.php b/Test/Integration/_file/some-fixture_rollback.php
new file mode 100644
index 0000000..2d37fe7
--- /dev/null
+++ b/Test/Integration/_file/some-fixture_rollback.php
@@ -0,0 +1,42 @@
+<?php
+/**
+ * Copyright (c) 2019 TechDivision GmbH
+ * All rights reserved
+ *
+ * This product includes proprietary software developed at TechDivision GmbH, Germany
+ * For more information see http://www.techdivision.com/
+ *
+ * To obtain a valid license for using this software please contact us at
+ * license@techdivision.com
+ */
+declare(strict_types=1);
+
+use Magento\Framework\Api\SearchCriteriaBuilder;
+use Magento\Framework\Registry;
+use Magento\Sales\Api\OrderRepositoryInterface;
+use Magento\TestFramework\Helper\Bootstrap;
+
+/** @var Registry $registry */
+$registry = Bootstrap::getObjectManager()->get(Registry::class);
+$registry->unregister('isSecureArea');
+$registry->register('isSecureArea', true);
+
+/** @var OrderRepositoryInterface $orderRepository */
+$orderRepository = Bootstrap::getObjectManager()->get(OrderRepositoryInterface::class);
+
+/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
+$searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
+
+$searchCriteria = $searchCriteriaBuilder->addFilter(
+    'increment_id',
+    'METEXAMPLE-%',
+    'like'
+)->create();
+
+$orders = $orderRepository->getList($searchCriteria);
+foreach ($orders->getItems() as $order) {
+    $orderRepository->delete($order);
+}
+
+$registry->unregister('isSecureArea');
+$registry->register('isSecureArea', false);
diff --git a/composer.json b/composer.json
new file mode 100644
index 0000000..00a3fd1
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,17 @@
+{
+  "name": "met/example-magento2-module-testing",
+  "description": "Example module for Magento 2 automated testing on MET CI",
+  "require": {
+    "magento/framework": "*",
+    "magento/module-sales": "*"
+  },
+  "type": "magento2-module",
+  "autoload": {
+    "files": [
+      "registration.php"
+    ],
+    "psr-4": {
+      "Met\\ExampleMagento2ModuleTesting\\": ""
+    }
+  }
+}
\ No newline at end of file
diff --git a/etc/module.xml b/etc/module.xml
new file mode 100644
index 0000000..8de246f
--- /dev/null
+++ b/etc/module.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0"?>
+<!--
+/**
+ * Copyright (c) 2019 TechDivision GmbH
+ * All rights reserved
+ *
+ * This product includes proprietary software developed at TechDivision GmbH, Germany
+ * For more information see http://www.techdivision.com/
+ *
+ * To obtain a valid license for using this software please contact us at
+ * license@techdivision.com
+ *
+ * @copyright  Copyright (c) 2019 TechDivision GmbH (http://www.techdivision.com)
+ * @author     Vadim Justus <v.justus@techdivision.com>
+ * @link       http://www.techdivision.com/
+ */
+ -->
+<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
+    <module name="Met_ExampleMagento2ModuleTesting" setup_version="1.0.0">
+        <sequence>
+            <module name="Magento_Sales"/>
+        </sequence>
+    </module>
+</config>
diff --git a/registration.php b/registration.php
new file mode 100644
index 0000000..3c6132b
--- /dev/null
+++ b/registration.php
@@ -0,0 +1,16 @@
+<?php
+/**
+ * Copyright (c) 2019 TechDivision GmbH
+ * All rights reserved
+ *
+ * This product includes proprietary software developed at TechDivision GmbH, Germany
+ * For more information see http://www.techdivision.com/
+ *
+ * To obtain a valid license for using this software please contact us at
+ * license@techdivision.com
+ */
+\Magento\Framework\Component\ComponentRegistrar::register(
+    \Magento\Framework\Component\ComponentRegistrar::MODULE,
+    'Met_ExampleMagento2ModuleTesting',
+    __DIR__
+);
-- 
GitLab