From 92413f6597e760c2c312619ea73ac6b4046d29be Mon Sep 17 00:00:00 2001
From: Josh Pollock <josh@calderawp.com>
Date: Tue, 3 Apr 2018 17:18:14 -0400
Subject: [PATCH] get integration tests working with Caldera Forms

---
 .gitignore                                |   3 +-
 Tests/Integration/IntegrationTestCase.php |   2 +-
 Tests/Integration/RestAPITestCase.php     |   2 +-
 Tests/Integration/RestRequestTest.php     |  61 ---------
 Tests/Integration/TestsTest.php           |  24 ++++
 Tests/Mock/.gitkeep                       |   1 +
 Tests/Mock/wpdb.php                       |   3 +
 Tests/bootstrap-integration.php           |   3 +-
 Tests/bootstrap.php                       |   5 +-
 Tests/plugins/.gitkeep                    |   1 +
 Tests/plugins/caldera-forms               |   1 +
 caldera-forms-query.php                   |  14 --
 composer.json                             |  20 ++-
 composer.lock                             | 154 +++++++++++++++++++++-
 14 files changed, 206 insertions(+), 88 deletions(-)
 delete mode 100755 Tests/Integration/RestRequestTest.php
 create mode 100644 Tests/Integration/TestsTest.php
 create mode 100644 Tests/plugins/.gitkeep
 create mode 160000 Tests/plugins/caldera-forms

diff --git a/.gitignore b/.gitignore
index 3b6e017..0e6ecb9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,4 +6,5 @@ vendor/
 *.sql
 *.tar.gz
 *.zip
-.php_cs.cache
\ No newline at end of file
+.php_cs.cache
+Tests/plugins/
\ No newline at end of file
diff --git a/Tests/Integration/IntegrationTestCase.php b/Tests/Integration/IntegrationTestCase.php
index 8a5c71d..0a47f4a 100755
--- a/Tests/Integration/IntegrationTestCase.php
+++ b/Tests/Integration/IntegrationTestCase.php
@@ -1,7 +1,7 @@
 <?php
 
 
-namespace CalderaLearn\RestSearch\Tests\Integration;
+namespace calderawp\CalderaFormsQuery\Tests\Integration;
 
 /**
  * Class IntegrationTestCase
diff --git a/Tests/Integration/RestAPITestCase.php b/Tests/Integration/RestAPITestCase.php
index 8903dbc..5088ec9 100755
--- a/Tests/Integration/RestAPITestCase.php
+++ b/Tests/Integration/RestAPITestCase.php
@@ -1,6 +1,6 @@
 <?php
 
-namespace CalderaLearn\RestSearch\Tests\Integration;
+namespace calderawp\CalderaFormsQuery\Tests\Integration;
 
 // phpcs:disable
 /**
diff --git a/Tests/Integration/RestRequestTest.php b/Tests/Integration/RestRequestTest.php
deleted file mode 100755
index 184d488..0000000
--- a/Tests/Integration/RestRequestTest.php
+++ /dev/null
@@ -1,61 +0,0 @@
-<?php
-
-
-namespace CalderaLearn\RestSearch\Tests\Integration;
-
-use CalderaLearn\RestSearch\FilterWPQuery;
-use CalderaLearn\RestSearch\Tests\Mock\AlwaysFilterWPQuery;
-
-class RestRequestTest extends RestAPITestCase
-{
-
-	/**
-	 * Ensures that REST API requests will be filtered
-	 *
-	 * @covers FilterWPQuery::callback()
-	 */
-	public function testShouldFilter()
-	{
-		//Create a request
-		$request = new \WP_REST_Request('GET', '/wp/v2/posts');
-		rest_api_loaded();
-		//Make sure the method returns true
-		$this->assertTrue(FilterWPQuery::shouldFilter());
-	}
-
-
-	/**
-	 * Ensure that REST API response data was correctly altered
-	 *
-	 * @covers FilterWPQuery::shouldFilter();
-	 * @covers FilterWPQuery::callback()
-	 */
-	public function testFilteringRESTRequest()
-	{
-		//Setup filter
-		AlwaysFilterWPQuery::addFilter();
-		$this->assertTrue(AlwaysFilterWPQuery::shouldFilter());
-
-		//Create a request
-		$request = new \WP_REST_Request('GET', '/wp/v2/posts');
-		//Dispatch request
-		$response = rest_get_server()->dispatch($request);
-
-
-		//Test response status
-		$this->assertSame(200, $response->get_status());
-
-		//Test the response data
-		//Use the mock data we have in our mock class as the expected values
-		$expected = FilterWPQuery::getPosts();
-
-		//Test that the expected results and the actual results are the same
-		$responseData = $response->get_data();
-		$this->assertTrue(is_array($responseData));
-		$this->assertSame(count($expected), count($responseData));
-		foreach ($responseData as $i => $responsePost) {
-			$this->assertTrue(isset($expected[$i]));
-			$this->assertSame($expected[$i]->post_title, $responsePost[ 'title' ][ 'rendered' ]);
-		}
-	}
-}
diff --git a/Tests/Integration/TestsTest.php b/Tests/Integration/TestsTest.php
new file mode 100644
index 0000000..de82c30
--- /dev/null
+++ b/Tests/Integration/TestsTest.php
@@ -0,0 +1,24 @@
+<?php
+
+
+namespace calderawp\CalderaFormsQuery\Tests\Integration;
+
+/**
+ * Class TestsTest
+ *
+ * Tests to ensure integration test environment is working
+ * @package calderawp\CalderaFormsQuery\Tests\Integration
+ */
+class TestsTest extends IntegrationTestCase
+{
+
+	/**
+	 * Check that Caldera Forms is usable
+	 */
+	public function testCalderaFormsIsInstalled()
+	{
+		$this->assertTrue( defined( 'CFCORE_VER' ) );
+		$this->assertTrue( class_exists( '\Caldera_Forms' ) );
+	}
+
+}
\ No newline at end of file
diff --git a/Tests/Mock/.gitkeep b/Tests/Mock/.gitkeep
index e69de29..4287ca8 100644
--- a/Tests/Mock/.gitkeep
+++ b/Tests/Mock/.gitkeep
@@ -0,0 +1 @@
+#
\ No newline at end of file
diff --git a/Tests/Mock/wpdb.php b/Tests/Mock/wpdb.php
index 78c027a..d2a09e0 100644
--- a/Tests/Mock/wpdb.php
+++ b/Tests/Mock/wpdb.php
@@ -1,4 +1,7 @@
 <?php
+if( class_exists( 'wpdb')){
+	return;
+}
 //phpcs:disable
 class wpdb
 {
diff --git a/Tests/bootstrap-integration.php b/Tests/bootstrap-integration.php
index 2474e35..b8d9207 100755
--- a/Tests/bootstrap-integration.php
+++ b/Tests/bootstrap-integration.php
@@ -13,11 +13,12 @@ if (! $_tests_dir) {
 require_once $_tests_dir . '/includes/functions.php';
 
 /**
- * Manually load the plugin being tested.
+ * Manually load the plugin being tested and plugins it depends on
  */
 function _manually_load_plugin()
 {
 	require dirname(dirname(__FILE__)) . '/caldera-forms-query.php';
+	require __DIR__ . '/plugins/caldera-forms/caldera-core.php';
 }
 tests_add_filter('muplugins_loaded', '_manually_load_plugin');
 
diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php
index 8c20631..6e9dc42 100755
--- a/Tests/bootstrap.php
+++ b/Tests/bootstrap.php
@@ -31,8 +31,9 @@ if (! function_exists('__')) {
 		return translate($text, $domain);
 	}
 }
-
-
+//Mock WPDB (not using autoloader so it doesn't load during integration tests
+include_once __DIR__ . '/Mock/wpdb.php';
+//WordPress WPDB constants
 if ( ! defined( 'ARRAY_A')) {
 	define('OBJECT', 'OBJECT');
 	define('object', 'OBJECT');
diff --git a/Tests/plugins/.gitkeep b/Tests/plugins/.gitkeep
new file mode 100644
index 0000000..4287ca8
--- /dev/null
+++ b/Tests/plugins/.gitkeep
@@ -0,0 +1 @@
+#
\ No newline at end of file
diff --git a/Tests/plugins/caldera-forms b/Tests/plugins/caldera-forms
new file mode 160000
index 0000000..3b0a81f
--- /dev/null
+++ b/Tests/plugins/caldera-forms
@@ -0,0 +1 @@
+Subproject commit 3b0a81ff69ed4aaaaa92987c0fff4520e7252f25
diff --git a/caldera-forms-query.php b/caldera-forms-query.php
index bb61928..9efda7e 100644
--- a/caldera-forms-query.php
+++ b/caldera-forms-query.php
@@ -11,24 +11,10 @@
  *
  */
 
-
 include_once __DIR__ .'/vendor/autoload.php';
 
 if( function_exists( 'add_action' ) ){
 	add_action( 'caldera_forms_includes_complete', function(){
-		$x=  1;
-
-		$entry = new \calderawp\CalderaFormsQuery\Select\Entry(
-			new \calderawp\CalderaFormsQuery\MySqlBuilder(),
-			'wp_cf_form_entries'
-		);
-		$entry->queryByFormsId( 'cf12345' );
-		$entry->addOrderBy( 'form_id', false );
-		$sql = $entry->getPreparedSql();
-		$x= 1;
-
-
-
 
 	});
 }
diff --git a/composer.json b/composer.json
index dc74d2e..3e7ae9f 100644
--- a/composer.json
+++ b/composer.json
@@ -9,6 +9,12 @@
             "email": "josh@calderawp.com"
         }
     ],
+    "repositories" : [
+        {
+            "type": "git",
+            "url" : "git@github.com:CalderaWP/Caldera-Forms.git"
+        }
+    ],
     "require": {
         "php": ">=5.6",
         "nilportugues/sql-query-builder": "^1.5"
@@ -33,15 +39,19 @@
         "psr-4": {
             "calderawp\\CalderaFormsQuery\\Tests\\": "Tests/"
 
-        },
-        "files" : [
-            "./Tests/Mock/wpdb.php"
-        ]
+        }
     },
     "require-dev": {
         "php": "^7.1",
         "phpunit/phpunit": "^7.0",
         "squizlabs/php_codesniffer": "^3.2",
-        "jakub-onderka/php-parallel-lint": "^1.0"
+        "jakub-onderka/php-parallel-lint": "^1.0",
+        "Desertsnowman/caldera-forms": "dev-develop",
+        "composer/installers": "~1.0"
+    },
+    "extra" : {
+        "installer-paths" : {
+            "Tests/plugins/{$name}/" : ["type:wordpress-plugin"]
+        }
     }
 }
diff --git a/composer.lock b/composer.lock
index e8c1188..d5f6230 100644
--- a/composer.lock
+++ b/composer.lock
@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
         "This file is @generated automatically"
     ],
-    "content-hash": "db6310436913680c3c5b50bdff00c88c",
+    "content-hash": "a6eee6d6f3062af42dc7b830f7971dac",
     "packages": [
         {
             "name": "nilportugues/sql-query-builder",
@@ -127,6 +127,154 @@
         }
     ],
     "packages-dev": [
+        {
+            "name": "Desertsnowman/caldera-forms",
+            "version": "dev-develop",
+            "source": {
+                "type": "git",
+                "url": "git@github.com:CalderaWP/Caldera-Forms.git",
+                "reference": "3b0a81ff69ed4aaaaa92987c0fff4520e7252f25"
+            },
+            "type": "wordpress-plugin",
+            "license": [
+                "GPL-2.0+"
+            ],
+            "authors": [
+                {
+                    "name": "David Cramer",
+                    "homepage": "http://cramer.co.za",
+                    "role": "Lead Developer"
+                }
+            ],
+            "description": "Create complex grid based, responsive forms easily with an easy to use drag and drop layout builder",
+            "homepage": "http://calderaforms.com",
+            "keywords": [
+                "caldera",
+                "forms",
+                "wordpress"
+            ],
+            "time": "2018-04-03T13:13:16+00:00"
+        },
+        {
+            "name": "composer/installers",
+            "version": "v1.5.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/composer/installers.git",
+                "reference": "049797d727261bf27f2690430d935067710049c2"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/composer/installers/zipball/049797d727261bf27f2690430d935067710049c2",
+                "reference": "049797d727261bf27f2690430d935067710049c2",
+                "shasum": ""
+            },
+            "require": {
+                "composer-plugin-api": "^1.0"
+            },
+            "replace": {
+                "roundcube/plugin-installer": "*",
+                "shama/baton": "*"
+            },
+            "require-dev": {
+                "composer/composer": "1.0.*@dev",
+                "phpunit/phpunit": "^4.8.36"
+            },
+            "type": "composer-plugin",
+            "extra": {
+                "class": "Composer\\Installers\\Plugin",
+                "branch-alias": {
+                    "dev-master": "1.0-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Composer\\Installers\\": "src/Composer/Installers"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Kyle Robinson Young",
+                    "email": "kyle@dontkry.com",
+                    "homepage": "https://github.com/shama"
+                }
+            ],
+            "description": "A multi-framework Composer library installer",
+            "homepage": "https://composer.github.io/installers/",
+            "keywords": [
+                "Craft",
+                "Dolibarr",
+                "Eliasis",
+                "Hurad",
+                "ImageCMS",
+                "Kanboard",
+                "Lan Management System",
+                "MODX Evo",
+                "Mautic",
+                "Maya",
+                "OXID",
+                "Plentymarkets",
+                "Porto",
+                "RadPHP",
+                "SMF",
+                "Thelia",
+                "WolfCMS",
+                "agl",
+                "aimeos",
+                "annotatecms",
+                "attogram",
+                "bitrix",
+                "cakephp",
+                "chef",
+                "cockpit",
+                "codeigniter",
+                "concrete5",
+                "croogo",
+                "dokuwiki",
+                "drupal",
+                "eZ Platform",
+                "elgg",
+                "expressionengine",
+                "fuelphp",
+                "grav",
+                "installer",
+                "itop",
+                "joomla",
+                "kohana",
+                "laravel",
+                "lavalite",
+                "lithium",
+                "magento",
+                "majima",
+                "mako",
+                "mediawiki",
+                "modulework",
+                "modx",
+                "moodle",
+                "osclass",
+                "phpbb",
+                "piwik",
+                "ppi",
+                "puppet",
+                "pxcms",
+                "reindex",
+                "roundcube",
+                "shopware",
+                "silverstripe",
+                "sydes",
+                "symfony",
+                "typo3",
+                "wordpress",
+                "yawik",
+                "zend",
+                "zikula"
+            ],
+            "time": "2017-12-29T09:13:20+00:00"
+        },
         {
             "name": "doctrine/instantiator",
             "version": "1.1.0",
@@ -1683,7 +1831,9 @@
     ],
     "aliases": [],
     "minimum-stability": "stable",
-    "stability-flags": [],
+    "stability-flags": {
+        "desertsnowman/caldera-forms": 20
+    },
     "prefer-stable": false,
     "prefer-lowest": false,
     "platform": {
-- 
GitLab