Skip to content

Commit 8d71fc1

Browse files
committed
Add additional tests
1 parent 90b5670 commit 8d71fc1

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

tests/DataView/DataViewTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public function test_to_js(): void {
3838

3939
$uuid = $field->uuid();
4040
$expected = <<<TEXT
41-
"render":( data ) => datakit_fields.html("$uuid", data, []),
41+
"render": ( data ) => datakit_fields.html("$uuid", data, []),
4242
TEXT;
4343

44-
self::assertStringContainsString( $expected, $view->to_js() );
44+
self::assertStringContainsString( $expected, $view->to_js( true ) );
4545
}
4646

4747
/**

tests/Field/AbstractFieldTestCase.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* This class contains tests that should pass for all field types.
1212
*
1313
* @since $ver$
14+
*
15+
* @template T of Field The field type.
1416
*/
1517
abstract class AbstractFieldTestCase extends TestCase {
1618
/**
@@ -29,7 +31,7 @@ abstract protected static function fieldClass(): string;
2931
* @param string $id The field id.
3032
* @param string $header the field header.
3133
*
32-
* @return Field The field instance.
34+
* @return T The field.
3335
*/
3436
protected function createField( string $id, string $header ): Field {
3537
$field_class = static::fieldClass();
@@ -53,6 +55,7 @@ public function testCreate(): void {
5355
* Test case for {@see Field::to_array()}.
5456
*
5557
* @since $ver$
58+
* @return T The field.
5659
*/
5760
public function testToArray(): Field {
5861
$field = $this->createField( 'field_id', 'Field header' );

tests/Field/EnumFieldTest.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,27 @@
1111
* Unit tests for {@see EnumField}.
1212
*
1313
* @since $ver$
14+
*
15+
* @template T of EnumField
16+
*
17+
* @extends AbstractFieldTestCase<T>
1418
*/
1519
final class EnumFieldTest extends AbstractFieldTestCase {
1620
/**
1721
* @inheritDoc
1822
* @since $ver$
1923
*/
20-
protected static function fieldClass() : string {
24+
protected static function fieldClass(): string {
2125
return EnumField::class;
2226
}
2327

2428
/**
2529
* @inheritDoc
2630
* @since $ver$
31+
* @return T
2732
*/
28-
protected function createField( string $id, string $header ) : Field {
29-
$field_class = static::fieldClass();
33+
protected function createField( string $id, string $header ): Field {
34+
$field_class = self::fieldClass();
3035

3136
return call_user_func( [ $field_class, 'create' ], $id, $header, [
3237
'active' => 'Active',
@@ -40,8 +45,9 @@ protected function createField( string $id, string $header ) : Field {
4045
* Adds specific tests for EnumField.
4146
*
4247
* @since $ver$
48+
* @return T
4349
*/
44-
public function testToArray() : Field {
50+
public function testToArray(): Field {
4551
$field = parent::testToArray();
4652
$field_array = $field->to_array();
4753

tests/Field/HtmlFieldTest.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace DataKit\DataViews\Tests\Field;
4+
5+
use DataKit\DataViews\Field\HtmlField;
6+
7+
/**
8+
* Unit tests for {@see HtmlField}
9+
*
10+
* @since $ver$
11+
*
12+
* @extends AbstractFieldTestCase<HtmlField>
13+
*/
14+
final class HtmlFieldTest extends AbstractFieldTestCase {
15+
/**
16+
* @inheritDoc
17+
* @since $ver$
18+
*/
19+
protected static function fieldClass(): string {
20+
return HtmlField::class;
21+
}
22+
23+
/**
24+
* Test case for {@see HtmlField::allow_scripts()} and {@see HtmlField::deny_scripts()}.
25+
*
26+
* @since $ver$
27+
*/
28+
public function test_scripts(): void {
29+
$field = $this->createField( 'html', 'Html Field' );
30+
$allowed = $field->allow_scripts();
31+
$denied = $allowed->deny_scripts();
32+
33+
self::assertStringContainsString( '{"is_scripts_allowed":false}', $field->to_array()['render'] );
34+
self::assertStringContainsString( '{"is_scripts_allowed":false}', $denied->to_array()['render'] );
35+
self::assertStringContainsString( '{"is_scripts_allowed":true}', $allowed->to_array()['render'] );
36+
}
37+
}

0 commit comments

Comments
 (0)