Skip to content

Commit

Permalink
Rewrite field settings tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vinkla committed Oct 28, 2023
1 parent c2565f0 commit befd8fb
Show file tree
Hide file tree
Showing 68 changed files with 1,447 additions and 601 deletions.
23 changes: 9 additions & 14 deletions tests/Fields/AccordionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,26 @@
namespace Extended\ACF\Tests\Fields;

use Extended\ACF\Fields\Accordion;
use PHPUnit\Framework\TestCase;
use Extended\ACF\Tests\Fields\Settings\Endpoint;
use Extended\ACF\Tests\Fields\Settings\Instructions;

class AccordionTest extends TestCase
class AccordionTest extends FieldTestCase
{
public function testType()
{
$field = Accordion::make('Accordion')->get();
$this->assertSame('accordion', $field['type']);
}
use Endpoint;
use Instructions;

public function testEndpoint()
{
$field = Accordion::make('Accordion Endpoint')->endpoint()->get();
$this->assertTrue($field['endpoint']);
}
public string $field = Accordion::class;
public string $type = 'accordion';

public function testMultiExpand()
{
$field = Accordion::make('Accordion Multi Expand')->multiExpand()->get();
$field = Accordion::make('Multi Expand')->multiExpand()->get();
$this->assertTrue($field['multi_expand']);
}

public function testOpen()
{
$field = Accordion::make('Accordion Open')->open()->get();
$field = Accordion::make('Open')->open()->get();
$this->assertTrue($field['open']);
}
}
36 changes: 19 additions & 17 deletions tests/Fields/ButtonGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,26 @@
namespace Extended\ACF\Tests\Fields;

use Extended\ACF\Fields\ButtonGroup;
use PHPUnit\Framework\TestCase;
use Extended\ACF\Tests\Fields\Settings\Choices;
use Extended\ACF\Tests\Fields\Settings\ConditionalLogic;
use Extended\ACF\Tests\Fields\Settings\DefaultValue;
use Extended\ACF\Tests\Fields\Settings\DirectionLayout;
use Extended\ACF\Tests\Fields\Settings\Instructions;
use Extended\ACF\Tests\Fields\Settings\Required;
use Extended\ACF\Tests\Fields\Settings\ReturnFormat;
use Extended\ACF\Tests\Fields\Settings\Wrapper;

class ButtonGroupTest extends TestCase
class ButtonGroupTest extends FieldTestCase
{
public function testType()
{
$field = ButtonGroup::make('Button Group')->get();
$this->assertSame('button_group', $field['type']);
}
use Choices;
use ConditionalLogic;
use DefaultValue;
use DirectionLayout;
use Instructions;
use Required;
use ReturnFormat;
use Wrapper;

public function testChoices()
{
$choices = ['blue' => 'Blue Pill', 'red' => 'Red Pill'];
$field = ButtonGroup::make('Choices')->choices($choices)->get();
$this->assertSame($choices, $field['choices']);

$choices = ['Forest Green', 'Sky Blue'];
$field = ButtonGroup::make('Choices List')->choices($choices)->get();
$this->assertSame(['forest_green' => 'Forest Green', 'sky_blue' => 'Sky Blue'], $field['choices']);
}
public string $field = ButtonGroup::class;
public string $type = 'button_group';
}
42 changes: 23 additions & 19 deletions tests/Fields/CheckboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,29 @@
namespace Extended\ACF\Tests\Fields;

use Extended\ACF\Fields\Checkbox;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

class CheckboxTest extends TestCase
use Extended\ACF\Tests\Fields\Settings\Choices;
use Extended\ACF\Tests\Fields\Settings\ConditionalLogic;
use Extended\ACF\Tests\Fields\Settings\DefaultValue;
use Extended\ACF\Tests\Fields\Settings\DirectionLayout;
use Extended\ACF\Tests\Fields\Settings\Disabled;
use Extended\ACF\Tests\Fields\Settings\Instructions;
use Extended\ACF\Tests\Fields\Settings\Required;
use Extended\ACF\Tests\Fields\Settings\ReturnFormat;
use Extended\ACF\Tests\Fields\Settings\Wrapper;

class CheckboxTest extends FieldTestCase
{
public function testType()
{
$field = Checkbox::make('Checkbox')->get();
$this->assertSame('checkbox', $field['type']);
}

public function testLayout()
{
$field = Checkbox::make('Checkbox Layout')->layout('horizontal')->get();
$this->assertSame('horizontal', $field['layout']);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid argument layout [test].');

Checkbox::make('Invalid Checkbox Layout')->layout('test');
}
use Choices;
use ConditionalLogic;
use DefaultValue;
use DirectionLayout;
use Disabled;
use Instructions;
use Required;
use ReturnFormat;
use Wrapper;

public string $field = Checkbox::class;
public string $type = 'checkbox';
}
23 changes: 15 additions & 8 deletions tests/Fields/ColorPickerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,26 @@
namespace Extended\ACF\Tests\Fields;

use Extended\ACF\Fields\ColorPicker;
use PHPUnit\Framework\TestCase;
use Extended\ACF\Tests\Fields\Settings\ConditionalLogic;
use Extended\ACF\Tests\Fields\Settings\DefaultValue;
use Extended\ACF\Tests\Fields\Settings\Instructions;
use Extended\ACF\Tests\Fields\Settings\Required;
use Extended\ACF\Tests\Fields\Settings\Wrapper;

class ColorPickerTest extends TestCase
class ColorPickerTest extends FieldTestCase
{
public function testType()
{
$field = ColorPicker::make('Color Picker')->get();
$this->assertSame('color_picker', $field['type']);
}
use ConditionalLogic;
use DefaultValue;
use Instructions;
use Required;
use Wrapper;

public string $field = ColorPicker::class;
public string $type = 'color_picker';

public function testEnableOpacity()
{
$field = ColorPicker::make('Color Picker Enable Opacity')->enableOpacity()->get();
$field = ColorPicker::make('Enable Opacity')->enableOpacity()->get();
$this->assertTrue($field['enable_opacity']);
}
}
47 changes: 21 additions & 26 deletions tests/Fields/DatePickerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,26 @@
namespace Extended\ACF\Tests\Fields;

use Extended\ACF\Fields\DatePicker;
use PHPUnit\Framework\TestCase;

class DatePickerTest extends TestCase
use Extended\ACF\Tests\Fields\Settings\ConditionalLogic;
use Extended\ACF\Tests\Fields\Settings\DateTimeFormat;
use Extended\ACF\Tests\Fields\Settings\Disabled;
use Extended\ACF\Tests\Fields\Settings\Immutable;
use Extended\ACF\Tests\Fields\Settings\Instructions;
use Extended\ACF\Tests\Fields\Settings\Required;
use Extended\ACF\Tests\Fields\Settings\WeekDay;
use Extended\ACF\Tests\Fields\Settings\Wrapper;

class DatePickerTest extends FieldTestCase
{
public function testType()
{
$field = DatePicker::make('Date Picker')->get();
$this->assertSame('date_picker', $field['type']);
}

public function testDisplayFormat()
{
$field = DatePicker::make('Date Display Format')->displayFormat('d/m/Y')->get();
$this->assertSame('d/m/Y', $field['display_format']);
}

public function testReturnFormat()
{
$field = DatePicker::make('Date Return Format')->returnFormat('d/m/Y')->get();
$this->assertSame('d/m/Y', $field['return_format']);
}

public function testFirstDayOfWeek()
{
$field = DatePicker::make('Date Week Day')->firstDayOfWeek(1)->get();
$this->assertSame(1, $field['first_day']);
}
use ConditionalLogic;
use DateTimeFormat;
use Disabled;
use Immutable;
use Instructions;
use Required;
use WeekDay;
use Wrapper;

public string $field = DatePicker::class;
public string $type = 'date_picker';
}
27 changes: 20 additions & 7 deletions tests/Fields/DateTimePickerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,26 @@
namespace Extended\ACF\Tests\Fields;

use Extended\ACF\Fields\DateTimePicker;
use PHPUnit\Framework\TestCase;
use Extended\ACF\Tests\Fields\Settings\ConditionalLogic;
use Extended\ACF\Tests\Fields\Settings\DateTimeFormat;
use Extended\ACF\Tests\Fields\Settings\Disabled;
use Extended\ACF\Tests\Fields\Settings\Immutable;
use Extended\ACF\Tests\Fields\Settings\Instructions;
use Extended\ACF\Tests\Fields\Settings\Required;
use Extended\ACF\Tests\Fields\Settings\WeekDay;
use Extended\ACF\Tests\Fields\Settings\Wrapper;

class DateTimePickerTest extends TestCase
class DateTimePickerTest extends FieldTestCase
{
public function testType()
{
$field = DateTimePicker::make('Date Time Picker')->get();
$this->assertSame('date_time_picker', $field['type']);
}
use ConditionalLogic;
use DateTimeFormat;
use Disabled;
use Immutable;
use Instructions;
use Required;
use WeekDay;
use Wrapper;

public string $field = DateTimePicker::class;
public string $type = 'date_time_picker';
}
29 changes: 22 additions & 7 deletions tests/Fields/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,28 @@
namespace Extended\ACF\Tests\Fields;

use Extended\ACF\Fields\Email;
use PHPUnit\Framework\TestCase;
use Extended\ACF\Tests\Fields\Settings\Affixable;
use Extended\ACF\Tests\Fields\Settings\ConditionalLogic;
use Extended\ACF\Tests\Fields\Settings\DefaultValue;
use Extended\ACF\Tests\Fields\Settings\Disabled;
use Extended\ACF\Tests\Fields\Settings\Immutable;
use Extended\ACF\Tests\Fields\Settings\Instructions;
use Extended\ACF\Tests\Fields\Settings\Placeholder;
use Extended\ACF\Tests\Fields\Settings\Required;
use Extended\ACF\Tests\Fields\Settings\Wrapper;

class EmailTest extends TestCase
class EmailTest extends FieldTestCase
{
public function testType()
{
$field = Email::make('Email Address')->get();
$this->assertSame('email', $field['type']);
}
use Affixable;
use ConditionalLogic;
use DefaultValue;
use Disabled;
use Immutable;
use Instructions;
use Placeholder;
use Required;
use Wrapper;

public string $field = Email::class;
public string $type = 'email';
}
86 changes: 86 additions & 0 deletions tests/Fields/FieldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/**
* Copyright (c) Vincent Klaiber
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @see https://github.com/vinkla/extended-acf
*/

declare(strict_types=1);

namespace Extended\ACF\Tests\Fields;

use Extended\ACF\Fields\Text;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Symfony\Component\VarDumper\VarDumper;

class FieldTest extends TestCase
{
public function testLabel()
{
$field = Text::make('Label')->get();
$this->assertSame('Label', $field['label']);
}

public function testName()
{
$field = Text::make('Label Name')->get();
$this->assertSame('label_name', $field['name']);

$field = Text::make('Custom Name', 'custom_name')->get();
$this->assertSame('custom_name', $field['name']);
}

public function testKey()
{
$field = Text::make('Key')->get();
$this->assertSame('field_722bfe15', $field['key']);
}

public function testCustomKey()
{
$field = Text::make('Custom Key')->key('field_123456')->get();
$this->assertSame('field_123456', $field['key']);
}

public function testKeyUniqueness()
{
$this->expectExceptionMessage('The key [field_16217cde] is not unique.');
Text::make('Key Uniqueness 1')->key('field_16217cde');
Text::make('Key Uniqueness 2')->key('field_16217cde');
}

public function testKeyPrefix()
{
$this->expectExceptionMessage('The key should have the prefix [field_].');
Text::make('Key refix')->key('phone')->get();
}

public function testWithSettings()
{
$field = Text::make('With Settings')->withSettings(['custom' => 'setting'])->get();
$this->assertSame('setting', $field['custom']);

$this->expectException(InvalidArgumentException::class);
Text::make('With Settings Label')->withSettings(['label' => 'invalid'])->get();
}

public function testDump()
{
$log = [];

VarDumper::setHandler(function ($value) use (&$log) {
$log[] = $value;
});

Text::make('Dump')->dump(1, 2);

$this->assertSame([['label' => 'Dump', 'name' => 'dump', 'type' => 'text', 'key' => 'field_076f7d8c'], 1, 2], $log);

VarDumper::setHandler(null);
}
}
Loading

0 comments on commit befd8fb

Please sign in to comment.