Skip to content
Snippets Groups Projects
Commit d299b87a authored by Josh Pollock's avatar Josh Pollock
Browse files

Clean up code

parent 730e9c32
No related branches found
No related tags found
No related merge requests found
...@@ -19,19 +19,19 @@ trait CanCreateEntryWithEmailField ...@@ -19,19 +19,19 @@ trait CanCreateEntryWithEmailField
* *
* @return int * @return int
*/ */
protected function createEntryWithEmail( $email = 'hiroy@hiroy.club' ) protected function createEntryWithEmail($email = 'hiroy@hiroy.club')
{ {
$fieldData = []; $fieldData = [];
$emailFieldConfig = $this->getEmailField(); $emailFieldConfig = $this->getEmailField();
foreach( $this->mock_form[ 'fields' ] as $fieldId => $fieldConfig ){ foreach ($this->mock_form[ 'fields' ] as $fieldId => $fieldConfig) {
if ( $fieldId === $emailFieldConfig[ 'ID'] ) { if ($fieldId === $emailFieldConfig[ 'ID']) {
$fieldData[ $fieldId ] = $email; $fieldData[ $fieldId ] = $email;
} else { } else {
$fieldData[ $fieldId ] = rand() . $fieldId; $fieldData[ $fieldId ] = rand() . $fieldId;
} }
} }
return \Caldera_Forms_Save_Final::create_entry( $this->mock_form, $fieldData ); return \Caldera_Forms_Save_Final::create_entry($this->mock_form, $fieldData);
} }
/** /**
...@@ -41,7 +41,7 @@ trait CanCreateEntryWithEmailField ...@@ -41,7 +41,7 @@ trait CanCreateEntryWithEmailField
*/ */
protected function getEmailField() protected function getEmailField()
{ {
return \Caldera_Forms_Field_Util::get_field_by_slug( 'email', $this->mock_form ); return \Caldera_Forms_Field_Util::get_field_by_slug('email', $this->mock_form);
} }
/** /**
...@@ -53,4 +53,4 @@ trait CanCreateEntryWithEmailField ...@@ -53,4 +53,4 @@ trait CanCreateEntryWithEmailField
{ {
return $this->getEmailField()[ 'slug' ]; return $this->getEmailField()[ 'slug' ];
} }
} }
\ No newline at end of file
...@@ -84,7 +84,7 @@ trait HasFactories ...@@ -84,7 +84,7 @@ trait HasFactories
protected function getWPDB() protected function getWPDB()
{ {
global $wpdb; global $wpdb;
if (! class_exists( '\WP_User')) { if (! class_exists('\WP_User')) {
include_once dirname(dirname(__FILE__)) . '/Mock/wpdb.php'; include_once dirname(dirname(__FILE__)) . '/Mock/wpdb.php';
} }
......
...@@ -23,6 +23,4 @@ trait UsersMockFormAsDBForm ...@@ -23,6 +23,4 @@ trait UsersMockFormAsDBForm
$this->mock_form = \Caldera_Forms_Forms::get_form($this->mock_form_id); $this->mock_form = \Caldera_Forms_Forms::get_form($this->mock_form_id);
parent::setUp(); parent::setUp();
} }
} }
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
namespace calderawp\CalderaFormsQuery\Tests\Unit\Delete; namespace calderawp\CalderaFormsQuery\Tests\Unit\Delete;
use calderawp\CalderaFormsQuery\Delete\DeleteQueryBuilder; use calderawp\CalderaFormsQuery\Delete\DeleteQueryBuilder;
use calderawp\CalderaFormsQuery\MySqlBuilder; use calderawp\CalderaFormsQuery\MySqlBuilder;
use calderawp\CalderaFormsQuery\Tests\Unit\TestCase; use calderawp\CalderaFormsQuery\Tests\Unit\TestCase;
...@@ -40,7 +39,6 @@ class DeleteQueryBuilderTest extends TestCase ...@@ -40,7 +39,6 @@ class DeleteQueryBuilderTest extends TestCase
$entryValues = $this->entryValuesDeleteGeneratorFactory(); $entryValues = $this->entryValuesDeleteGeneratorFactory();
$this->assertTrue(is_a($entryValues->getBuilder(), MySqlBuilder::class)); $this->assertTrue(is_a($entryValues->getBuilder(), MySqlBuilder::class));
} }
/** /**
...@@ -56,7 +54,6 @@ class DeleteQueryBuilderTest extends TestCase ...@@ -56,7 +54,6 @@ class DeleteQueryBuilderTest extends TestCase
$entryValues = $this->entryValuesDeleteGeneratorFactory(); $entryValues = $this->entryValuesDeleteGeneratorFactory();
$this->assertTrue(is_a($entryValues->getDeleteQuery(), Delete::class)); $this->assertTrue(is_a($entryValues->getDeleteQuery(), Delete::class));
} }
/** /**
...@@ -68,10 +65,10 @@ class DeleteQueryBuilderTest extends TestCase ...@@ -68,10 +65,10 @@ class DeleteQueryBuilderTest extends TestCase
public function testGetDeleteQueryAndCurrentQueryAreSame() public function testGetDeleteQueryAndCurrentQueryAreSame()
{ {
$entry = $this->entryDeleteGeneratorFactory(); $entry = $this->entryDeleteGeneratorFactory();
$this->assertSame( $entry->getDeleteQuery(), $entry->getCurrentQuery() ); $this->assertSame($entry->getDeleteQuery(), $entry->getCurrentQuery());
$entryValues = $this->entryValuesDeleteGeneratorFactory(); $entryValues = $this->entryValuesDeleteGeneratorFactory();
$this->assertSame( $entryValues->getDeleteQuery(), $entryValues->getCurrentQuery() ); $this->assertSame($entryValues->getDeleteQuery(), $entryValues->getCurrentQuery());
} }
/** /**
...@@ -99,4 +96,4 @@ class DeleteQueryBuilderTest extends TestCase ...@@ -99,4 +96,4 @@ class DeleteQueryBuilderTest extends TestCase
->getName() ->getName()
); );
} }
} }
\ No newline at end of file
...@@ -3,7 +3,12 @@ ...@@ -3,7 +3,12 @@
namespace calderawp\CalderaFormsQuery; namespace calderawp\CalderaFormsQuery;
interface DoesQueries /**
* Interface CreatesSqlQueries
*
* Interface that all classes that create SQL queries MUST impliment
*/
interface CreatesSqlQueries
{ {
/** /**
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
namespace calderawp\CalderaFormsQuery\Delete; namespace calderawp\CalderaFormsQuery\Delete;
use calderawp\CalderaFormsQuery\QueryBuilder; use calderawp\CalderaFormsQuery\QueryBuilder;
use NilPortugues\Sql\QueryBuilder\Manipulation\Delete; use NilPortugues\Sql\QueryBuilder\Manipulation\Delete;
...@@ -20,7 +19,7 @@ abstract class DeleteQueryBuilder extends QueryBuilder implements DoesDeleteQuer ...@@ -20,7 +19,7 @@ abstract class DeleteQueryBuilder extends QueryBuilder implements DoesDeleteQuer
*/ */
public function getDeleteQuery() public function getDeleteQuery()
{ {
if( ! $this->deleteQuery ){ if (! $this->deleteQuery) {
$this->deleteQuery = new Delete($this->getTableName()); $this->deleteQuery = new Delete($this->getTableName());
} }
...@@ -34,4 +33,4 @@ abstract class DeleteQueryBuilder extends QueryBuilder implements DoesDeleteQuer ...@@ -34,4 +33,4 @@ abstract class DeleteQueryBuilder extends QueryBuilder implements DoesDeleteQuer
{ {
return $this->getDeleteQuery(); return $this->getDeleteQuery();
} }
} }
\ No newline at end of file
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
namespace calderawp\CalderaFormsQuery\Delete; namespace calderawp\CalderaFormsQuery\Delete;
use NilPortugues\Sql\QueryBuilder\Manipulation\Delete; use NilPortugues\Sql\QueryBuilder\Manipulation\Delete;
/** /**
...@@ -20,5 +19,4 @@ interface DoesDeleteQuery ...@@ -20,5 +19,4 @@ interface DoesDeleteQuery
* @return Delete * @return Delete
*/ */
public function getCurrentQuery(); public function getCurrentQuery();
}
}
\ No newline at end of file
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
namespace calderawp\CalderaFormsQuery\Delete; namespace calderawp\CalderaFormsQuery\Delete;
class Entry extends DeleteQueryBuilder class Entry extends DeleteQueryBuilder
{ {
...@@ -39,4 +38,4 @@ class Entry extends DeleteQueryBuilder ...@@ -39,4 +38,4 @@ class Entry extends DeleteQueryBuilder
{ {
return $this->is('user_id', $userId); return $this->is('user_id', $userId);
} }
} }
\ No newline at end of file
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
namespace calderawp\CalderaFormsQuery\Delete; namespace calderawp\CalderaFormsQuery\Delete;
class EntryValues extends DeleteQueryBuilder class EntryValues extends DeleteQueryBuilder
{ {
...@@ -18,7 +17,7 @@ class EntryValues extends DeleteQueryBuilder ...@@ -18,7 +17,7 @@ class EntryValues extends DeleteQueryBuilder
$this $this
->getDeleteQuery() ->getDeleteQuery()
->where() ->where()
->equals( 'entry_id', (int)$entryId ) ->equals('entry_id', (int)$entryId)
; ;
return $this; return $this;
} }
...@@ -35,15 +34,14 @@ class EntryValues extends DeleteQueryBuilder ...@@ -35,15 +34,14 @@ class EntryValues extends DeleteQueryBuilder
$this $this
->getDeleteQuery() ->getDeleteQuery()
->where() ->where()
->equals( 'value', $fieldValue ) ->equals('value', $fieldValue)
; ;
$this $this
->getDeleteQuery() ->getDeleteQuery()
->where() ->where()
->equals( 'slug', $fieldSlug ); ->equals('slug', $fieldSlug);
return $this; return $this;
} }
}
}
\ No newline at end of file
...@@ -6,6 +6,11 @@ namespace calderawp\CalderaFormsQuery; ...@@ -6,6 +6,11 @@ namespace calderawp\CalderaFormsQuery;
use calderawp\CalderaFormsQuery\Select\Entry; use calderawp\CalderaFormsQuery\Select\Entry;
use calderawp\CalderaFormsQuery\Select\EntryValues; use calderawp\CalderaFormsQuery\Select\EntryValues;
/**
* Class EntryQueries
*
* Used to query entry data, using SQL created by genrators
*/
class EntryQueries implements QueriesEntries class EntryQueries implements QueriesEntries
{ {
/** /**
......
...@@ -33,8 +33,8 @@ class Escape ...@@ -33,8 +33,8 @@ class Escape
* @return string Text in the form of a LIKE phrase. The output is not SQL safe. Call $wpdb::prepare() * @return string Text in the form of a LIKE phrase. The output is not SQL safe. Call $wpdb::prepare()
* or real_escape next. * or real_escape next.
*/ */
public static function like( $text ) { public static function like($text)
return addcslashes( $text, '_%\\' ); {
return addcslashes($text, '_%\\');
} }
}
}
\ No newline at end of file
...@@ -6,6 +6,11 @@ namespace calderawp\CalderaFormsQuery; ...@@ -6,6 +6,11 @@ namespace calderawp\CalderaFormsQuery;
use calderawp\CalderaFormsQuery\Select\Entry; use calderawp\CalderaFormsQuery\Select\Entry;
use calderawp\CalderaFormsQuery\Select\EntryValues; use calderawp\CalderaFormsQuery\Select\EntryValues;
/**
* Interface QueriesEntries
*
* Interface that all classes that query for entries MUST impliment
*/
interface QueriesEntries interface QueriesEntries
{ {
/** /**
......
...@@ -3,11 +3,16 @@ ...@@ -3,11 +3,16 @@
namespace calderawp\CalderaFormsQuery; namespace calderawp\CalderaFormsQuery;
use calderawp\CalderaFormsQuery\Select\DoesSelectQuery; use calderawp\CalderaFormsQuery\Select\DoesSelectQuery;
use NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery; use NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery;
abstract class QueryBuilder implements DoesQueries /**
* Class QueryBuilder
*
*
* Used to create SQL queries, NOT run SQL queries
*/
abstract class QueryBuilder implements CreatesSqlQueries
{ {
const ASC = 'ASC'; const ASC = 'ASC';
const DESC = 'DESC'; const DESC = 'DESC';
...@@ -64,7 +69,7 @@ abstract class QueryBuilder implements DoesQueries ...@@ -64,7 +69,7 @@ abstract class QueryBuilder implements DoesQueries
* @param string $value * @param string $value
* @return $this * @return $this
*/ */
protected function addWhereEquals( AbstractBaseQuery $queryBuilder, $column, $value ) protected function addWhereEquals(AbstractBaseQuery $queryBuilder, $column, $value)
{ {
$queryBuilder $queryBuilder
->where() ->where()
...@@ -101,7 +106,8 @@ abstract class QueryBuilder implements DoesQueries ...@@ -101,7 +106,8 @@ abstract class QueryBuilder implements DoesQueries
*/ */
protected function getSecondDeliminator() protected function getSecondDeliminator()
{ {
return $this->isLike ? "%'" : "'"; } return $this->isLike ? "%'" : "'";
}
/** /**
* Surround one value with quotes or % * Surround one value with quotes or %
...@@ -112,10 +118,10 @@ abstract class QueryBuilder implements DoesQueries ...@@ -112,10 +118,10 @@ abstract class QueryBuilder implements DoesQueries
protected function surroundValue($value) protected function surroundValue($value)
{ {
$value = "{$this->getFirstDeliminator()}$value{$this->getSecondDeliminator()}"; $value = "{$this->getFirstDeliminator()}$value{$this->getSecondDeliminator()}";
if( ! $this->isLike){ if (! $this->isLike) {
return $value; return $value;
} }
return Escape::like( $value ); return Escape::like($value);
} }
...@@ -128,7 +134,7 @@ abstract class QueryBuilder implements DoesQueries ...@@ -128,7 +134,7 @@ abstract class QueryBuilder implements DoesQueries
*/ */
protected function is($column, $value) protected function is($column, $value)
{ {
return $this->addWhereEquals($this->getCurrentQuery(),$column, $value); return $this->addWhereEquals($this->getCurrentQuery(), $column, $value);
} }
/** @inheritdoc */ /** @inheritdoc */
...@@ -136,4 +142,4 @@ abstract class QueryBuilder implements DoesQueries ...@@ -136,4 +142,4 @@ abstract class QueryBuilder implements DoesQueries
{ {
return $this->substituteValues($this->getBuilder()->write($this->getCurrentQuery())); return $this->substituteValues($this->getBuilder()->write($this->getCurrentQuery()));
} }
} }
\ No newline at end of file
...@@ -29,7 +29,7 @@ class EntryValues extends SelectQueryBuilder ...@@ -29,7 +29,7 @@ class EntryValues extends SelectQueryBuilder
* @param string $fieldSlug Field slug * @param string $fieldSlug Field slug
* @param string $fieldValue Field value * @param string $fieldValue Field value
* @param string $type Optional. Type of comparison. Values: equals|notEquals|like Default: 'equals' * @param string $type Optional. Type of comparison. Values: equals|notEquals|like Default: 'equals'
* @param string $whereOperator Optional. Type of where. Default is 'AND'. Any valid MySQL where operator is accepted * @param string $whereOperator Optional. Type of where. Default is 'AND'. Any valid WHERE operator is accepted
* @return $this * @return $this
*/ */
public function queryByFieldValue($fieldSlug, $fieldValue, $type = 'equals', $whereOperator = 'AND') public function queryByFieldValue($fieldSlug, $fieldValue, $type = 'equals', $whereOperator = 'AND')
......
...@@ -3,14 +3,14 @@ ...@@ -3,14 +3,14 @@
namespace calderawp\CalderaFormsQuery\Select; namespace calderawp\CalderaFormsQuery\Select;
use calderawp\CalderaFormsQuery\DoesQueries; use calderawp\CalderaFormsQuery\CreatesSqlQueries;
use calderawp\CalderaFormsQuery\MySqlBuilder; use calderawp\CalderaFormsQuery\MySqlBuilder;
use calderawp\CalderaFormsQuery\QueryBuilder; use calderawp\CalderaFormsQuery\QueryBuilder;
use NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery; use NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery;
use NilPortugues\Sql\QueryBuilder\Manipulation\Delete; use NilPortugues\Sql\QueryBuilder\Manipulation\Delete;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select; use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
abstract class SelectQueryBuilder extends QueryBuilder implements DoesSelectQuery abstract class SelectQueryBuilder extends QueryBuilder implements DoesSelectQuery
{ {
/** /**
...@@ -47,8 +47,4 @@ abstract class SelectQueryBuilder extends QueryBuilder implements DoesSelectQue ...@@ -47,8 +47,4 @@ abstract class SelectQueryBuilder extends QueryBuilder implements DoesSelectQue
$this->getCurrentQuery()->orderBy($column, $order); $this->getCurrentQuery()->orderBy($column, $order);
return $this; return $this;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment