Skip to content

Commit 66eab7f

Browse files
committed
Bring back PestPHP
1 parent 1bcf04b commit 66eab7f

6 files changed

Lines changed: 277 additions & 65 deletions

File tree

.github/workflows/tests.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,15 @@ jobs:
154154
- name: Run composer install
155155
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
156156

157-
- name: Run PHPUnit tests
158-
run: ./vendor/bin/phpunit
157+
- name: Run Pest tests
158+
run: ./vendor/bin/pest -v --bail --colors=always
159159

160160
- name: Upload test results
161161
if: always()
162162
uses: actions/upload-artifact@v4
163163
with:
164164
name: test-results-php${{ matrix.php }}-wp${{ matrix.wordpress }}
165-
path: var/browser
165+
path: tests/Browser/Screenshots
166166
retention-days: 30
167167

168168
- name: Show FrankenPHP logs

composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
{
22
"require-dev": {
33
"rector/rector": "^1.2",
4-
"zenstruck/browser": "^1.9",
5-
"phpunit/phpunit": "^12",
6-
"symfony/panther": "^2.2",
7-
"dbrekelmans/bdi": "^1.4"
4+
"pestphp/pest": "^4.1",
5+
"pestphp/pest-plugin-browser": "^4.1",
6+
"symfony/var-dumper": "^7.3"
87
},
98
"autoload": {
109
"psr-4": {

phpunit.xml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
<directory suffix="Test.php">./tests</directory>
1010
</testsuite>
1111
</testsuites>
12-
<extensions>
13-
<bootstrap class="Zenstruck\Browser\Test\BrowserExtension"/>
14-
</extensions>
12+
<source>
13+
<include>
14+
<directory>src</directory>
15+
</include>
16+
</source>
1517
</phpunit>

tests/Browser/PluginSettingsTest.php

Lines changed: 196 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,199 @@
22

33
namespace Tests\Browser;
44

5-
class PluginSettingsTest extends BrowserTestCase
6-
{
7-
public function test_activation_and_presence_of_default_scripts(): void
8-
{
9-
$browser = $this->asAdmin();
10-
$this->activatePluginIfNeeded($browser);
11-
12-
$browser->visit('/wp-admin/plugins.php')
13-
->assertSeeElement('#deactivate-simpleanalytics')
14-
->visit('/')
15-
->assertContains('<!-- Simple Analytics: Not logging requests from admins -->')
16-
->assertContains('<script src="http://localhost:8100/wp-content/plugins/simpleanalytics/resources/js/inactive.js"');
17-
18-
$this->myBrowser()
19-
->visit('/')
20-
->assertContains('<script src="https://scripts.simpleanalyticscdn.com/latest.js"></script>');
21-
}
22-
23-
public function test_adds_script_with_ignored_pages(): void
24-
{
25-
$browser = $this->asAdmin();
26-
$this->activatePluginIfNeeded($browser);
27-
28-
$browser->visit('/wp-admin/options-general.php?page=simpleanalytics&tab=ignore-rules')
29-
->fillField('simpleanalytics_ignore_pages', '/vouchers')
30-
->click('Save Changes')
31-
->visit('/wp-admin/options-general.php?page=simpleanalytics&tab=ignore-rules')
32-
->assertFieldEquals('simpleanalytics_ignore_pages', '/vouchers');
33-
34-
$this->myBrowser()
35-
->visit('/')
36-
->assertContains('data-ignore-pages="/vouchers"');
37-
}
38-
//it('adds inactive script for selected user roles', function () {
39-
// $admin = asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=ignore-rules')
40-
// ->check('simpleanalytics_exclude_user_roles-editor')
41-
// ->check('simpleanalytics_exclude_user_roles-author')
42-
// ->click('Save Changes')
43-
// ->assertChecked('simpleanalytics_exclude_user_roles-editor')
44-
// ->assertChecked('simpleanalytics_exclude_user_roles-author');
45-
//
46-
// $admin->navigate('http://localhost:8100')
47-
// ->assertPresent(DEFAULT_SCRIPT_SELECTOR);
48-
//
49-
// asAuthor()->navigate('http://localhost:8100')
50-
// ->assertPresent(INACTIVE_ADMIN_SCRIPT_SELECTOR)
51-
// ->assertSourceHas(INACTIVE_ADMIN_COMMENT);
52-
//
53-
// asEditor()->navigate('http://localhost:8100')
54-
// ->assertPresent(INACTIVE_ADMIN_SCRIPT_SELECTOR)
55-
// ->assertSourceHas(INACTIVE_ADMIN_COMMENT);
56-
//});
57-
58-
// public function
59-
}
5+
use function Tests\{asAdmin, asAuthor, asEditor};
6+
7+
const DEFAULT_SCRIPT_SELECTOR = 'script[src="https://scripts.simpleanalyticscdn.com/latest.js"]';
8+
const INACTIVE_ADMIN_SCRIPT_SELECTOR = 'script[src="http://localhost:8100/wp-content/plugins/simpleanalytics/resources/js/inactive.js"]';
9+
const INACTIVE_ADMIN_COMMENT = '<!-- Simple Analytics: Not logging requests from admins -->';
10+
const NOSCRIPT_SELECTOR = 'noscript img[src="https://queue.simpleanalyticscdn.com/noscript.gif"][alt=""][referrerpolicy="no-referrer-when-downgrade"]';
11+
12+
it('can be activated', function () {
13+
asAdmin()
14+
->navigate('http://localhost:8100/wp-admin/plugins.php')
15+
->assertPresent('tr[data-slug="simpleanalytics"]')
16+
->click('#activate-simpleanalytics')
17+
->assertPresent('a[href="options-general.php?page=simpleanalytics"]')
18+
->assertPresent('#deactivate-simpleanalytics')
19+
->screenshot();
20+
});
21+
22+
it('adds a script by default', function () {
23+
visit('http://localhost:8100')->assertPresent(DEFAULT_SCRIPT_SELECTOR);
24+
});
25+
26+
it('adds inactive script for authenticated users by default', function () {
27+
asAdmin()
28+
->navigate('http://localhost:8100')
29+
->assertPresent('script[src="http://localhost:8100/wp-content/plugins/simpleanalytics/resources/js/inactive.js"]')
30+
->assertSourceHas(INACTIVE_ADMIN_COMMENT);
31+
});
32+
33+
it('adds a script with ignored pages', function () {
34+
asAdmin()
35+
->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=ignore-rules')
36+
->fill('simpleanalytics_ignore_pages', '/vouchers')
37+
->click('Save Changes')
38+
->refresh()
39+
->assertValue('simpleanalytics_ignore_pages', '/vouchers')
40+
->screenshot();
41+
42+
visit('http://localhost:8100')->refresh()->assertSourceHas('data-ignore-pages="/vouchers"');
43+
});
44+
45+
it('adds inactive script for selected user roles', function () {
46+
$admin = asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=ignore-rules')
47+
->check('simpleanalytics_exclude_user_roles-editor')
48+
->check('simpleanalytics_exclude_user_roles-author')
49+
->click('Save Changes')
50+
->assertChecked('simpleanalytics_exclude_user_roles-editor')
51+
->assertChecked('simpleanalytics_exclude_user_roles-author');
52+
53+
$admin->navigate('http://localhost:8100')
54+
->assertPresent(DEFAULT_SCRIPT_SELECTOR);
55+
56+
asAuthor()->navigate('http://localhost:8100')
57+
->assertPresent(INACTIVE_ADMIN_SCRIPT_SELECTOR)
58+
->assertSourceHas(INACTIVE_ADMIN_COMMENT);
59+
60+
asEditor()->navigate('http://localhost:8100')
61+
->assertPresent(INACTIVE_ADMIN_SCRIPT_SELECTOR)
62+
->assertSourceHas(INACTIVE_ADMIN_COMMENT);
63+
});
64+
65+
it('adds a script with collect do not track enabled', function () {
66+
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=advanced')
67+
->check('simpleanalytics_collect_dnt')
68+
->click('Save Changes')
69+
->assertChecked('simpleanalytics_collect_dnt');
70+
71+
visit('http://localhost:8100')->assertSourceHas('data-collect-dnt="true"');
72+
});
73+
74+
it('adds a script with hash mode enabled', function () {
75+
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=advanced')
76+
->check('simpleanalytics_hash_mode')
77+
->click('Save Changes')
78+
->assertChecked('simpleanalytics_hash_mode');
79+
80+
visit('http://localhost:8100')->assertSourceHas('data-mode="hash"');
81+
});
82+
83+
it('adds a script with manually collect page views enabled', function () {
84+
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=advanced')
85+
->check('simpleanalytics_manual_collect')
86+
->click('Save Changes')
87+
->assertChecked('simpleanalytics_manual_collect');
88+
89+
visit('http://localhost:8100')->assertSourceHas('data-auto-collect="true"');
90+
});
91+
92+
/*it('adds noscript tag when support no javascript mode is enabled', function () {
93+
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=advanced')
94+
->check('simpleanalytics_noscript')
95+
->click('Save Changes')
96+
->assertChecked('simpleanalytics_noscript');
97+
98+
visit('http://localhost:8100')->assertPresent(NOSCRIPT_SELECTOR);
99+
});*/
100+
101+
/*it('adds a script with onload callback', function () {
102+
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=advanced')
103+
->fill('simpleanalytics_onload_callback', 'sa_event("My event")')
104+
->click('Save Changes')
105+
->assertValue('simpleanalytics_onload_callback', 'sa_event("My event")');
106+
107+
visit('http://localhost:8100')->assertSourceHas('data-onload="sa_event(\"My event\")"');
108+
});*/
109+
110+
it('adds a script with overwrite domain name', function () {
111+
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=advanced')
112+
->fill('simpleanalytics_hostname', 'example.com')
113+
->click('Save Changes')
114+
->assertValue('simpleanalytics_hostname', 'example.com');
115+
116+
visit('http://localhost:8100')->assertSourceHas('data-hostname="example.com"');
117+
});
118+
119+
it('adds a script with global variable name', function () {
120+
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=advanced')
121+
->fill('simpleanalytics_sa_global', 'ba_event')
122+
->click('Save Changes')
123+
->assertValue('simpleanalytics_sa_global', 'ba_event');
124+
125+
visit('http://localhost:8100')->assertSourceHas('data-sa-global="ba_event"');
126+
});
127+
128+
it('adds automated events script when collect automated events is enabled', function () {
129+
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=events')
130+
->check('simpleanalytics_automated_events')
131+
->click('Save Changes')
132+
->assertChecked('simpleanalytics_automated_events');
133+
134+
visit('http://localhost:8100')->assertPresent('script[src="https://scripts.simpleanalyticscdn.com/auto-events.js"]');
135+
});
136+
137+
it('adds automated events script with auto collect downloads', function () {
138+
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=events')
139+
->check('simpleanalytics_automated_events')
140+
->fill('simpleanalytics_event_collect_downloads', 'outbound,emails,downloads')
141+
->click('Save Changes')
142+
->assertChecked('simpleanalytics_automated_events')
143+
->assertValue('simpleanalytics_event_collect_downloads', 'outbound,emails,downloads');
144+
145+
visit('http://localhost:8100')->assertSourceHas('data-collect="outbound,emails,downloads"');
146+
});
147+
148+
it('adds automated events script with download file extensions', function () {
149+
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=events')
150+
->check('simpleanalytics_automated_events')
151+
->fill('simpleanalytics_event_extensions', 'pdf,zip')
152+
->click('Save Changes')
153+
->assertChecked('simpleanalytics_automated_events')
154+
->assertValue('simpleanalytics_event_extensions', 'pdf,zip');
155+
156+
visit('http://localhost:8100')->assertSourceHas('data-extensions="pdf,zip"');
157+
});
158+
159+
it('adds automated events script with use titles of page enabled', function () {
160+
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=events')
161+
->check('simpleanalytics_automated_events')
162+
->check('simpleanalytics_event_use_title')
163+
->click('Save Changes')
164+
->assertChecked('simpleanalytics_automated_events')
165+
->assertChecked('simpleanalytics_event_use_title');
166+
167+
visit('http://localhost:8100')->assertSourceHas('data-use-title');
168+
});
169+
170+
it('adds automated events script with use full urls enabled', function () {
171+
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=events')
172+
->check('simpleanalytics_automated_events')
173+
->check('simpleanalytics_event_full_urls')
174+
->click('Save Changes')
175+
->assertChecked('simpleanalytics_automated_events')
176+
->assertChecked('simpleanalytics_event_full_urls');
177+
178+
visit('http://localhost:8100')->assertSourceHas('data-full-urls');
179+
});
180+
181+
it('adds automated events script with override global', function () {
182+
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=events')
183+
->check('simpleanalytics_automated_events')
184+
->fill('simpleanalytics_event_sa_global', 'ba_event')
185+
->click('Save Changes')
186+
->assertChecked('simpleanalytics_automated_events')
187+
->assertValue('simpleanalytics_event_sa_global', 'ba_event');
188+
189+
visit('http://localhost:8100')->assertSourceHas('data-sa-global="ba_event"');
190+
});
191+
192+
it('adds a script with a custom domain name', function () {
193+
asAdmin()
194+
->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=general')
195+
->fill('simpleanalytics_custom_domain', 'mydomain.com')
196+
->click('Save Changes')
197+
->assertValue('simpleanalytics_custom_domain', 'mydomain.com');
198+
199+
visit('http://localhost:8100')->assertPresent('script[src="https://mydomain.com/latest.js"]');
200+
});

tests/Pest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Test Case
8+
|--------------------------------------------------------------------------
9+
|
10+
| The closure you provide to your test functions is always bound to a specific PHPUnit test
11+
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
12+
| need to change it using the "pest()" function to bind a different classes or traits.
13+
|
14+
*/
15+
16+
use Pest\Browser\Api\Webpage;
17+
18+
pest()->extend(TestCase::class)->in('Feature');
19+
pest()->browser()->timeout(180000);
20+
21+
/*
22+
|--------------------------------------------------------------------------
23+
| Expectations
24+
|--------------------------------------------------------------------------
25+
|
26+
| When you're writing tests, you often need to check that values meet certain conditions. The
27+
| "expect()" function gives you access to a set of "expectations" methods that you can use
28+
| to assert different things. Of course, you may extend the Expectation API at any time.
29+
|
30+
*/
31+
32+
expect()->extend('toBeOne', function () {
33+
return $this->toBe(1);
34+
});
35+
36+
/*
37+
|--------------------------------------------------------------------------
38+
| Functions
39+
|--------------------------------------------------------------------------
40+
|
41+
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
42+
| project that you don't want to repeat in every file. Here you can also expose helpers as
43+
| global functions to help you to reduce the number of lines of code in your test files.
44+
|
45+
*/
46+
47+
function asUser(string $login, string $password)
48+
{
49+
return visit('http://localhost:8100/wp-admin')
50+
->fill('user_login', $login)
51+
->fill('user_pass', $password)
52+
->press('wp-submit')
53+
->assertUrlIs('http://localhost:8100/wp-admin');
54+
}
55+
56+
function asAdmin()
57+
{
58+
return asUser('admin', 'admin');
59+
}
60+
61+
function asAuthor()
62+
{
63+
return asUser('author', 'author');
64+
}
65+
66+
function asEditor()
67+
{
68+
return asUser('editor', 'editor');
69+
}

tests/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66

77
abstract class TestCase extends BaseTestCase
88
{
9+
//
910
}

0 commit comments

Comments
 (0)