Skip to content

Commit d602922

Browse files
committed
Add base tests
1 parent 86a5f0d commit d602922

File tree

15 files changed

+619
-4
lines changed

15 files changed

+619
-4
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,7 @@ package-lock.json
7575
### config
7676
/vendor/
7777
composer.lock
78+
79+
### Tests
80+
.phpunit.result.cache
81+
tests/coverage/

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"phpro/grumphp-shim": "^0.19.1",
1010
"squizlabs/php_codesniffer": "^3.5",
1111
"vimeo/psalm": "^3.13",
12-
"wp-coding-standards/wpcs": "^2.3"
12+
"wp-coding-standards/wpcs": "^2.3",
13+
"10up/wp_mock": "^0.4.2"
1314
},
1415
"scripts": {
1516
"cs": [
@@ -20,7 +21,9 @@
2021
],
2122
"psalm": [
2223
"./vendor/bin/psalm"
23-
]
24+
],
25+
"tests": "./vendor/bin/phpunit",
26+
"tests-cov": "phpdbg -qrr ./vendor/bin/phpunit --coverage-html ./tests/coverage/"
2427
},
2528
"autoload": {
2629
"psr-4": {

inc/Services/Acf.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function warning(): void {
5252
return;
5353
}
5454

55-
wp_die( sprintf( __( 'This theme can\'t work without ACF plugin. <a href="%s">Please login to admin</a>, and activate it !', 'framework-textdomain' ), esc_url( wp_login_url() ) ) ); // phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment, WordPress.Security.EscapeOutput.OutputNotEscaped
55+
\wp_die( sprintf( __( 'This theme can\'t work without ACF plugin. <a href="%s">Please login to admin</a>, and activate it !', 'framework-textdomain' ), esc_url( wp_login_url() ) ) ); // phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment, WordPress.Security.EscapeOutput.OutputNotEscaped
5656
}
5757

5858
/**
@@ -138,7 +138,6 @@ public function init_acf(): void {
138138
if ( ! is_file( get_theme_file_path( $this->path . $file . '.php' ) ) ) {
139139
continue;
140140
}
141-
142141
require_once get_theme_file_path( $this->path . $file . '.php' );
143142
}
144143
}

phpcs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<exclude-pattern>dist/</exclude-pattern>
1515
<exclude-pattern>src/</exclude-pattern>
1616
<exclude-pattern>scripts/</exclude-pattern>
17+
<exclude-pattern>tests/</exclude-pattern>
1718

1819
<!-- <<< EXCLUDE EXTERNAL (MU-)PLUGINS OR THEMES HERE >>> -->
1920

phpunit.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit beStrictAboutTestsThatDoNotTestAnything="false" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="./tests/bootstrap.php" executionOrder="depends,defects" forceCoversAnnotation="false" beStrictAboutCoversAnnotation="false" beStrictAboutOutputDuringTests="true" beStrictAboutTodoAnnotatedTests="true" colors="true" verbose="true">
3+
<coverage processUncoveredFiles="true">
4+
<include>
5+
<file>functions.php</file>
6+
<directory suffix=".php">inc</directory>
7+
</include>
8+
</coverage>
9+
<testsuites>
10+
<testsuite name="BFF test suite">
11+
<directory suffix="Test.php">tests</directory>
12+
</testsuite>
13+
</testsuites>
14+
</phpunit>

psalm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<directory name="src" />
1414
<directory name="dist" />
1515
<directory name="config" />
16+
<directory name="tests" />
1617
</ignoreFiles>
1718
</projectFiles>
1819

tests/FrameworkTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use BEA\Theme\Framework\Service_Container;
4+
use BEA\Theme\Framework\Services\Acf;
5+
use BEA\Theme\Framework\Services\Theme;
6+
use PHPUnit\Framework\TestCase;
7+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
8+
use WP_Mock\Functions;
9+
10+
class FrameworkTest extends \WP_Mock\Tools\TestCase {
11+
public function setUp(): void {
12+
WP_Mock::setUp();
13+
}
14+
15+
public function tearDown(): void {
16+
WP_Mock::tearDown();
17+
}
18+
19+
public function testNotAService() {
20+
\BEA\Theme\Framework\Framework::register_service( 'not-a-service' );
21+
22+
$this->assertFalse( \BEA\Theme\Framework\Framework::get_container()->get_service( 'not-a-service' ) );
23+
}
24+
25+
public function testSameContainer() {
26+
$container = \BEA\Theme\Framework\Framework::get_container();
27+
28+
$this->assertSame( $container, \BEA\Theme\Framework\Framework::get_container() );
29+
}
30+
31+
public function testServiceSet() {
32+
\BEA\Theme\Framework\Framework::register_service( Acf::class );
33+
34+
$this->assertNotEmpty( \BEA\Theme\Framework\Framework::get_container()->get_service( Acf::class ) );
35+
}
36+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Services;
4+
5+
use BEA\Theme\Framework\Service_Container;
6+
use BEA\Theme\Framework\Services\Svg;
7+
use WP_Mock;
8+
use WP_Mock\Tools\TestCase;
9+
use function BEA\Theme\Framework\Helpers\Formatting\Escape\escape_attribute_value;
10+
use function BEA\Theme\Framework\Helpers\Formatting\Escape\escape_content_value;
11+
use function BEA\Theme\Framework\Helpers\Formatting\Text\get_the_text;
12+
use function BEA\Theme\Framework\Helpers\Formatting\Text\the_text;
13+
use function ob_get_clean;
14+
use function ob_start;
15+
16+
class EscapeTest extends TestCase {
17+
public function setUp(): void {
18+
WP_Mock::setUp();
19+
}
20+
21+
public function tearDown(): void {
22+
WP_Mock::tearDown();
23+
}
24+
25+
public function testAttribute() {
26+
$value = escape_attribute_value( 'ok', '' );
27+
$this->assertSame( 'ok', $value );
28+
29+
$value = escape_attribute_value( 'ok', 'test_escape' );
30+
$this->assertSame( 'esc_ok', $value );
31+
}
32+
public function testContent() {
33+
$value = escape_content_value( 'ok', '' );
34+
$this->assertSame( 'ok', $value );
35+
36+
$value = escape_content_value( 'ok', 'test_escape' );
37+
$this->assertSame( 'esc_ok', $value );
38+
}
39+
}
40+
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
<?php
2+
3+
namespace Services;
4+
5+
use BEA\Theme\Framework\Service_Container;
6+
use BEA\Theme\Framework\Services\Svg;
7+
use WP_Mock;
8+
use WP_Mock\Tools\TestCase;
9+
use function BEA\Theme\Framework\Helpers\Formatting\Text\get_the_text;
10+
use function BEA\Theme\Framework\Helpers\Formatting\Text\the_text;
11+
use function ob_get_clean;
12+
use function ob_start;
13+
14+
class TextTest extends TestCase {
15+
public function setUp(): void {
16+
WP_Mock::setUp();
17+
}
18+
19+
public function tearDown(): void {
20+
WP_Mock::tearDown();
21+
}
22+
23+
public function testEmpty() {
24+
$test = get_the_text( '' );
25+
$this->assertEmpty( $test );
26+
27+
ob_start();
28+
the_text( '' );
29+
$buffer = ob_get_clean();
30+
$this->assertSame( $test, $buffer );
31+
32+
}
33+
34+
public function testBeforeAfter() {
35+
WP_Mock::userFunction( 'wp_parse_args', [
36+
'times' => 6,
37+
'return_in_order' => [
38+
[
39+
'before' => 'b',
40+
'after' => '',
41+
'escape' => '',
42+
],
43+
[
44+
'before' => '',
45+
'after' => 'a',
46+
'escape' => '',
47+
],
48+
[
49+
'before' => 'b',
50+
'after' => 'a',
51+
'escape' => '',
52+
],
53+
[
54+
'before' => 'b',
55+
'after' => '',
56+
'escape' => '',
57+
],
58+
[
59+
'before' => '',
60+
'after' => 'a',
61+
'escape' => '',
62+
],
63+
[
64+
'before' => 'b',
65+
'after' => 'a',
66+
'escape' => '',
67+
],
68+
],
69+
] );
70+
71+
$val = get_the_text( 'val', [ 'before' => 'b' ] );
72+
$val2 = get_the_text( 'val', [ 'after' => 'a' ] );
73+
$val3 = get_the_text( 'val', [ 'before' => 'b', 'after' => 'a' ] );
74+
75+
ob_start();
76+
the_text( 'val', [ 'before' => 'b' ] );
77+
$buffer = ob_get_clean();
78+
79+
ob_start();
80+
the_text( 'val', [ 'after' => 'a' ] );
81+
$buffer2 = ob_get_clean();
82+
83+
ob_start();
84+
the_text( 'val', [ 'before' => 'b', 'after' => 'a' ] );
85+
$buffer3 = ob_get_clean();
86+
87+
// Test returns
88+
$this->assertSame( 'bval', $val );
89+
$this->assertSame( 'vala', $val2 );
90+
$this->assertSame( 'bvala', $val3 );
91+
92+
// Test Buffer and generated the same
93+
$this->assertSame( $val, $buffer );
94+
$this->assertSame( $val2, $buffer2 );
95+
$this->assertSame( $val3, $buffer3 );
96+
}
97+
98+
public function testEscape() {
99+
WP_Mock::userFunction( 'wp_parse_args', [
100+
'times' => 4,
101+
'return_in_order' => [
102+
[
103+
'before' => '',
104+
'after' => '',
105+
'escape' => 'test_escape',
106+
],
107+
[
108+
'before' => 'b',
109+
'after' => '',
110+
'escape' => 'test_escape',
111+
],
112+
[
113+
'before' => '',
114+
'after' => 'a',
115+
'escape' => 'test_escape',
116+
],
117+
[
118+
'before' => 'b',
119+
'after' => 'a',
120+
'escape' => 'test_escape',
121+
],
122+
],
123+
] );
124+
125+
$this->assertSame( 'esc_val', get_the_text( 'val', [
126+
'before' => '',
127+
'after' => '',
128+
'escape' => 'test_escape',
129+
] ) );
130+
$this->assertSame( 'besc_val', get_the_text( 'val', [
131+
'before' => 'b',
132+
'after' => '',
133+
'escape' => 'test_escape',
134+
] ) );
135+
$this->assertSame( 'esc_vala', get_the_text( 'val', [
136+
'before' => '',
137+
'after' => 'a',
138+
'escape' => 'test_escape',
139+
] ) );
140+
$this->assertSame( 'besc_vala', get_the_text( 'val', [
141+
'before' => 'b',
142+
'after' => 'a',
143+
'escape' => 'test_escape',
144+
] ) );
145+
}
146+
147+
public function testFilterSettings() {
148+
WP_Mock::userFunction( 'wp_parse_args', [
149+
'return' =>
150+
[
151+
'before' => '',
152+
'after' => '',
153+
'escape' => 'test_escape',
154+
],
155+
] );
156+
157+
WP_Mock::onFilter( 'bea_theme_framework_text_settings' )->with( [
158+
'before' => '',
159+
'after' => '',
160+
'escape' => 'test_escape',
161+
], 'val' )->reply( [
162+
'before' => 'b',
163+
'after' => 'a',
164+
'escape' => 'esc_html',
165+
] );
166+
167+
$this->assertSame( 'bvala', get_the_text( 'val' ) );
168+
}
169+
170+
public function testFilterValue() {
171+
WP_Mock::userFunction( 'wp_parse_args', [
172+
'return' =>
173+
[
174+
'before' => '',
175+
'after' => '',
176+
'escape' => 'test_escape',
177+
],
178+
] );
179+
180+
WP_Mock::onFilter( 'bea_theme_framework_text_value' )->with( 'esc_val', [
181+
'before' => '',
182+
'after' => '',
183+
'escape' => 'test_escape',
184+
] )->reply( 'filtered' );
185+
186+
$this->assertSame( 'filtered', get_the_text( 'val' ) );
187+
}
188+
}
189+

0 commit comments

Comments
 (0)