diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 38447e9e09d3e045220f8589039a98f7368dba3b..b865367b37c950486d9171cbdfa3d7f1a922fc6a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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' diff --git a/changelog.txt b/changelog.txt index ee2493bd9c3d7c31e97633474a1590abb0d726d8..e6b1ae04f325245c5a796b0f1df00c36f1a0f785 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,5 +1,9 @@ # 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 diff --git a/composer.json b/composer.json index 6a79cb8b9cb4beafebefa156376aceb5fd0e654b..afcddb9856f7c0f001f1b88f1f0839929ec10a7d 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ } ], "require": { - "php": ">=5.6", + "php": ">=7.0", "ext-curl": "*", "ext-json": "*", "wpdesk/wp-persistence": "^2.0|^3.0", diff --git a/phpunit-integration.xml b/phpunit-integration.xml index 4a342abf125d638d044bafa01cd9a75a771b4b3d..c78958bbc0d69e1ecbc39361c73862ad29b6e373 100644 --- a/phpunit-integration.xml +++ b/phpunit-integration.xml @@ -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 diff --git a/src/Field.php b/src/Field.php index 458bb386815097e0262bff46d4c8fcbdd0fd1f03..4205df4652abc3089ee641fef09bfcec9b88a65d 100644 --- a/src/Field.php +++ b/src/Field.php @@ -163,4 +163,7 @@ interface Field { /** @return Serializer */ public function get_serializer(); + + /** @return int */ + public function get_priority(); } diff --git a/src/Field/BasicField.php b/src/Field/BasicField.php index da939228abaf3687dd3e1f41a6bfb96efa121b2c..7158b73b39a615dd7fec72c6a45cb584aaaf2cca 100644 --- a/src/Field/BasicField.php +++ b/src/Field/BasicField.php @@ -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; + } } diff --git a/src/Form/FormWithFields.php b/src/Form/FormWithFields.php index 4b5819cd6393de3b5c70d6305e2ab17627818c00..a5a494cbee9cac26ea3121655ac3a540b77c6bb6 100644 --- a/src/Form/FormWithFields.php +++ b/src/Form/FormWithFields.php @@ -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; } /** diff --git a/tests/integration/Form/FormWithFieldsTest.php b/tests/integration/Form/FormWithFieldsTest.php new file mode 100644 index 0000000000000000000000000000000000000000..9d8c2b2c267aa207a1659abe2374d87cd9e9db5c --- /dev/null +++ b/tests/integration/Form/FormWithFieldsTest.php @@ -0,0 +1,71 @@ +<?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()); + } +} diff --git a/tests/integration/bootstrap.php b/tests/integration/bootstrap.php index a422fd9c7fea652a15b2aced0b2e6134a5590201..bbe288972d79fa0587ace00864945146bbe5716e 100644 --- a/tests/integration/bootstrap.php +++ b/tests/integration/bootstrap.php @@ -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