Skip to content
Snippets Groups Projects
Unverified Commit cf9cae0e authored by Bartek Jaskulski's avatar Bartek Jaskulski
Browse files

feat(fields): #1 add sorting fields by priority field

parent cd630065
No related branches found
No related tags found
4 merge requests!28release: 3.0.0,!23Feature/strong typing pp,!19Add strong typing for 3.0 version,!18feat(fields): #1 add sorting fields by priority field
Pipeline #5934 passed with stages
in 1 minute and 29 seconds
......@@ -3,7 +3,6 @@ variables:
DISABLE_ACCEPTANCE: 1
DISABLE_PHP_5_5: 1
DISABLE_CODECEPTION: 1
DISABLE_INTEGRATION_TESTS: 1
IS_LIBRARY: 1
include: 'https://gitlab.com/wpdesk/gitlab-ci/raw/master/gitlab-ci-1.2.yml'
# Changelog
## [2.5.0] - 2021-09-03
### Added
- Added fields sorting by priority field
## [2.4.6] - 2021-08-23
### Fixed
- Get attribute now returns attribute instead of bool value
......
......@@ -16,7 +16,7 @@
}
],
"require": {
"php": ">=5.6",
"php": ">=7.0",
"ext-curl": "*",
"ext-json": "*",
"wpdesk/wp-persistence": "^2.0|^3.0",
......
......@@ -3,7 +3,7 @@
>
<testsuites>
<testsuite>
<directory prefix="Test" suffix=".php">./tests/integration</directory>
<directory suffix="Test.php">./tests/integration</directory>
</testsuite>
</testsuites>
......@@ -20,9 +20,4 @@
<log type="coverage-clover" target="build-coverage/clover.xml"/>
</logging>
<php>
<env name="WP_DEVELOP_DIR" value="/tmp/wordpress-develop"/>
<env name="WC_DEVELOP_DIR" value="/tmp/woocommerce"/>
</php>
</phpunit>
\ No newline at end of file
......@@ -163,4 +163,7 @@ interface Field {
/** @return Serializer */
public function get_serializer();
/** @return int */
public function get_priority();
}
......@@ -298,4 +298,15 @@ abstract class BasicField implements Field {
return $this;
}
/** @return int */
public function get_priority() {
return $this->meta['priority'] ?? 10;
}
public function set_priority( int $priority ) {
$this->meta['priority'] = $priority;
return $this;
}
}
......@@ -243,7 +243,16 @@ class FormWithFields implements Form, ContainerForm, FieldProvider {
* @inheritDoc
*/
public function get_fields() {
return $this->fields;
$fields = $this->fields;
usort(
$fields,
static function ( Field $a, Field $b ) {
return $a->get_priority() <=> $b->get_priority();
}
);
return $fields;
}
/**
......
<?php
namespace integration\Form;
use WPDesk\Forms\Field\InputNumberField;
use WPDesk\Forms\Field\InputTextField;
use WPDesk\Forms\Field\SelectField;
use WPDesk\Forms\Form\FormWithFields;
use PHPUnit\Framework\TestCase;
class FormWithFieldsTest extends TestCase {
/** @var FormWithFields */
private $form;
protected function setUp() {
$this->form = new FormWithFields([]);
}
public function test_should_return_fields_sorted_by_priority() {
$this->form->add_fields(
[
( new InputTextField() )
->set_label('third'),
( new SelectField() )
->set_label('second')
->set_priority(5),
( new InputNumberField() )
->set_label('first')
->set_priority(1)
]
);
$expected = [
( new InputNumberField() )
->set_label('first')
->set_priority(1),
( new SelectField() )
->set_label('second')
->set_priority(5),
( new InputTextField() )
->set_label('third')
];
self::assertEquals($expected, $this->form->get_fields());
}
public function test_should_return_fields_by_adding_order_if_no_priority_set() {
$this->form->add_fields(
[
( new InputTextField() )
->set_label('first'),
( new SelectField() )
->set_label('second'),
( new InputNumberField() )
->set_label('third'),
]
);
$expected = [
( new InputTextField() )
->set_label('first'),
( new SelectField() )
->set_label('second'),
( new InputNumberField() )
->set_label('third'),
];
self::assertEquals($expected, $this->form->get_fields());
}
}
......@@ -15,14 +15,4 @@ if ( getenv( 'PLUGIN_PATH' ) !== false ) {
define( 'PLUGIN_PATH', getenv( 'PLUGIN_PATH' ) );
} else {
define( 'PLUGIN_PATH', __DIR__ . DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR );
}
require_once( getenv( 'WP_DEVELOP_DIR' ) . '/tests/phpunit/includes/functions.php' );
tests_add_filter( 'muplugins_loaded', function () {
}, 100 );
putenv('WP_TESTS_DIR=' . getenv( 'WP_DEVELOP_DIR' ) . '/tests/phpunit');
require_once( getenv( 'WC_DEVELOP_DIR' ) . '/tests/bootstrap.php' );
do_action('plugins_loaded');
\ No newline at end of file
}
\ No newline at end of file
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