Skip to content

Commit 1a3ffbf

Browse files
PHPUnit to Pest Converter (#16)
* Add Pest dependencies * Add base Pest file * Convert test cases * Adopt expectation API * Optimize uses * Set return type of base TestCase methods From the [PHPUnit 8 release notes][1], the `TestCase` methods below now declare a `void` return type: - `setUpBeforeClass()` - `setUp()` - `assertPreConditions()` - `assertPostConditions()` - `tearDown()` - `tearDownAfterClass()` - `onNotSuccessfulTest()` [1]: https://phpunit.de/announcements/phpunit-8.html * Add back missing properties (temp fix) --------- Co-authored-by: Shift <[email protected]>
1 parent e81a534 commit 1a3ffbf

File tree

8 files changed

+394
-454
lines changed

8 files changed

+394
-454
lines changed

composer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
2525
"statamic/cms": "^3.4"
2626
},
2727
"require-dev": {
28-
"phpunit/phpunit": "^9.5",
2928
"nunomaduro/collision": "^6.2",
30-
"orchestra/testbench": "^7.5"
29+
"orchestra/testbench": "^7.5",
30+
"pestphp/pest": "^1.0"
3131
},
3232
"config": {
3333
"allow-plugins": {
34-
"pixelfear/composer-dist-plugin": true
34+
"pixelfear/composer-dist-plugin": true,
35+
"pestphp/pest-plugin": true
3536
}
3637
}
37-
}
38+
}

tests/DataTest.php

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,45 @@
11
<?php
22

3-
namespace Tests;
4-
53
use JackSleight\StatamicBardMutator\Support\Data;
64

7-
class DataTest extends TestCase
8-
{
9-
/** @test */
10-
public function it_creates_node_object()
11-
{
12-
$node = Data::node('paragraph');
13-
14-
$this->assertEquals('paragraph', $node->type);
15-
}
16-
17-
/** @test */
18-
public function it_creates_node_object_with_attributes()
19-
{
20-
$node = Data::node('image', ['src' => 'https://example.com/image.jpg']);
21-
22-
$this->assertEquals('image', $node->type);
23-
$this->assertEquals('https://example.com/image.jpg', $node->attrs->src);
24-
}
25-
26-
/** @test */
27-
public function it_creates_mark_object()
28-
{
29-
$mark = Data::mark('bold');
30-
31-
$this->assertEquals('bold', $mark->type);
32-
}
33-
34-
/** @test */
35-
public function it_creates_mark_object_with_attributes()
36-
{
37-
$mark = Data::mark('link', ['href' => 'https://example.com']);
38-
39-
$this->assertEquals('link', $mark->type);
40-
$this->assertEquals('https://example.com', $mark->attrs->href);
41-
}
42-
43-
/** @test */
44-
public function it_creates_text_node_object()
45-
{
46-
$node = Data::text('Hello world');
47-
48-
$this->assertEquals('text', $node->type);
49-
$this->assertEquals('Hello world', $node->text);
50-
}
51-
52-
/** @test */
53-
public function it_creates_html_node_object()
54-
{
55-
$node = Data::html('<p>Hello world</p>');
56-
57-
$this->assertEquals('bmuHtml', $node->type);
58-
$this->assertEquals('<p>Hello world</p>', $node->html);
59-
}
60-
}
5+
uses(Tests\TestCase::class);
6+
7+
it('creates node object', function () {
8+
$node = Data::node('paragraph');
9+
10+
expect($node->type)->toEqual('paragraph');
11+
});
12+
13+
it('creates node object with attributes', function () {
14+
$node = Data::node('image', ['src' => 'https://example.com/image.jpg']);
15+
16+
expect($node->type)->toEqual('image');
17+
expect($node->attrs->src)->toEqual('https://example.com/image.jpg');
18+
});
19+
20+
it('creates mark object', function () {
21+
$mark = Data::mark('bold');
22+
23+
expect($mark->type)->toEqual('bold');
24+
});
25+
26+
it('creates mark object with attributes', function () {
27+
$mark = Data::mark('link', ['href' => 'https://example.com']);
28+
29+
expect($mark->type)->toEqual('link');
30+
expect($mark->attrs->href)->toEqual('https://example.com');
31+
});
32+
33+
it('creates text node object', function () {
34+
$node = Data::text('Hello world');
35+
36+
expect($node->type)->toEqual('text');
37+
expect($node->text)->toEqual('Hello world');
38+
});
39+
40+
it('creates html node object', function () {
41+
$node = Data::html('<p>Hello world</p>');
42+
43+
expect($node->type)->toEqual('bmuHtml');
44+
expect($node->html)->toEqual('<p>Hello world</p>');
45+
});

tests/Pest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Test Case
7+
|--------------------------------------------------------------------------
8+
|
9+
| The closure you provide to your test functions is always bound to a specific PHPUnit test
10+
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
11+
| need to change it using the "uses()" function to bind a different classes or traits.
12+
|
13+
*/
14+
15+
/** @link https://pestphp.com/docs/configuring-tests */
16+
17+
/*
18+
|--------------------------------------------------------------------------
19+
| Expectations
20+
|--------------------------------------------------------------------------
21+
|
22+
| When you're writing tests, you often need to check that values meet certain conditions. The
23+
| "expect()" function gives you access to a set of "expectations" methods that you can use
24+
| to assert different things. Of course, you may extend the Expectation API at any time.
25+
|
26+
*/
27+
28+
/** @link https://pestphp.com/docs/custom-expectations */
29+
30+
/*
31+
|--------------------------------------------------------------------------
32+
| Functions
33+
|--------------------------------------------------------------------------
34+
|
35+
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
36+
| project that you don't want to repeat in every file. Here you can also expose helpers as
37+
| global functions to help you to reduce the number of lines of code in your test files.
38+
|
39+
*/
40+
41+
/** @link https://pestphp.com/docs/custom-helpers */

tests/PhaseTest.php

Lines changed: 82 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,109 @@
11
<?php
22

3-
namespace Tests;
4-
53
use JackSleight\StatamicBardMutator\Facades\Mutator;
64

7-
class PhaseTest extends TestCase
8-
{
9-
/** @test */
10-
public function it_calls_mutator_once_per_node()
11-
{
12-
$calls = 0;
13-
Mutator::html('paragraph', function ($value) use (&$calls) {
14-
$calls++;
5+
uses(Tests\TestCase::class);
6+
7+
it('calls mutator once per node', function () {
8+
$calls = 0;
9+
Mutator::html('paragraph', function ($value) use (&$calls) {
10+
$calls++;
11+
12+
return $value;
13+
});
14+
15+
$value = $this->getTestValue([[
16+
'type' => 'paragraph',
17+
]]);
18+
expect($this->renderTestValue($value))->toEqual('<p></p>');
19+
expect($calls)->toEqual(1);
20+
});
1521

16-
return $value;
17-
});
22+
it('calls mutator once per mark', function () {
23+
$calls = 0;
24+
Mutator::html('bold', function ($value) use (&$calls) {
25+
$calls++;
1826

19-
$value = $this->getTestValue([[
20-
'type' => 'paragraph',
21-
]]);
22-
$this->assertEquals('<p></p>', $this->renderTestValue($value));
23-
$this->assertEquals(1, $calls);
24-
}
27+
return $value;
28+
});
2529

26-
/** @test */
27-
public function it_calls_mutator_once_per_mark()
28-
{
29-
$calls = 0;
30-
Mutator::html('bold', function ($value) use (&$calls) {
31-
$calls++;
30+
$value = $this->getTestValue([[
31+
'type' => 'text',
32+
'text' => 'Some text',
33+
'marks' => [
34+
[
35+
'type' => 'bold',
36+
],
37+
],
38+
]]);
39+
expect($this->renderTestValue($value))->toEqual('<strong>Some text</strong>');
40+
expect($calls)->toEqual(1);
41+
});
3242

33-
return $value;
34-
});
43+
it('calls mutator once per adjacent marks', function () {
44+
$calls = 0;
45+
Mutator::html('bold', function ($value) use (&$calls) {
46+
$calls++;
3547

36-
$value = $this->getTestValue([[
48+
return $value;
49+
});
50+
51+
$value = $this->getTestValue([
52+
[
3753
'type' => 'text',
3854
'text' => 'Some text',
3955
'marks' => [
4056
[
4157
'type' => 'bold',
4258
],
4359
],
44-
]]);
45-
$this->assertEquals('<strong>Some text</strong>', $this->renderTestValue($value));
46-
$this->assertEquals(1, $calls);
47-
}
48-
49-
/** @test */
50-
public function it_calls_mutator_once_per_adjacent_marks()
51-
{
52-
$calls = 0;
53-
Mutator::html('bold', function ($value) use (&$calls) {
54-
$calls++;
55-
56-
return $value;
57-
});
58-
59-
$value = $this->getTestValue([
60-
[
61-
'type' => 'text',
62-
'text' => 'Some text',
63-
'marks' => [
64-
[
65-
'type' => 'bold',
66-
],
67-
],
68-
],
69-
[
70-
'type' => 'text',
71-
'text' => ' and some more text',
72-
'marks' => [
73-
[
74-
'type' => 'bold',
75-
],
60+
],
61+
[
62+
'type' => 'text',
63+
'text' => ' and some more text',
64+
'marks' => [
65+
[
66+
'type' => 'bold',
7667
],
7768
],
78-
]);
79-
$this->assertEquals('<strong>Some text and some more text</strong>', $this->renderTestValue($value));
80-
$this->assertEquals(1, $calls);
81-
}
69+
],
70+
]);
71+
expect($this->renderTestValue($value))->toEqual('<strong>Some text and some more text</strong>');
72+
expect($calls)->toEqual(1);
73+
});
8274

83-
/** @test */
84-
public function it_fetches_adjacent_marks_mutated_value()
85-
{
86-
Mutator::html('link', function ($value) {
87-
$value[0] = 'fancy-link';
75+
it('fetches adjacent marks mutated value', function () {
76+
Mutator::html('link', function ($value) {
77+
$value[0] = 'fancy-link';
8878

89-
return $value;
90-
});
79+
return $value;
80+
});
9181

92-
$value = $this->getTestValue([
93-
[
94-
'type' => 'text',
95-
'text' => 'Some text',
96-
'marks' => [
97-
[
98-
'type' => 'link',
99-
'attrs' => [
100-
'href' => 'http://example.com/',
101-
],
82+
$value = $this->getTestValue([
83+
[
84+
'type' => 'text',
85+
'text' => 'Some text',
86+
'marks' => [
87+
[
88+
'type' => 'link',
89+
'attrs' => [
90+
'href' => 'http://example.com/',
10291
],
10392
],
10493
],
105-
[
106-
'type' => 'text',
107-
'text' => ' and some more text',
108-
'marks' => [
109-
[
110-
'type' => 'link',
111-
'attrs' => [
112-
'href' => 'http://example.com/',
113-
],
94+
],
95+
[
96+
'type' => 'text',
97+
'text' => ' and some more text',
98+
'marks' => [
99+
[
100+
'type' => 'link',
101+
'attrs' => [
102+
'href' => 'http://example.com/',
114103
],
115104
],
116105
],
117-
]);
118-
$this->assertEquals('<fancy-link href="http://example.com/">Some text and some more text</fancy-link>', $this->renderTestValue($value));
119-
}
120-
}
106+
],
107+
]);
108+
expect($this->renderTestValue($value))->toEqual('<fancy-link href="http://example.com/">Some text and some more text</fancy-link>');
109+
});

0 commit comments

Comments
 (0)