પ્લેગ્રાઉન્ડ CLI નો પ્રોગ્રામેટિક ઉપયોગ
runCLI ફંક્શનનો ઉપયોગ કરીને તમારા JavaScript/TypeScript કોડમાંથી પ્લેગ્રાઉન્ડ CLI ને પ્રોગ્રામેટિક રીતે પણ નિયંત્રિત કરી શકાય છે. આથી તમારા કોડની અંદર CLI ની તમામ સુવિધાઓનો સીધો ઉપયોગ કરી શકાય છે, જે ખાસ કરીને end-to-end ટેસ્ટને ઓટોમેટ કરવા માટે ઉપયોગી છે. તમે runCLI ને આપો છો તે વિકલ્પો સીધા જ CLI flags સાથે મેળ ખાય છે.
ચોક્કસ વર્ઝન સાથે WordPress ઇન્સ્ટન્સ ચલાવવું
runCLI ફંક્શનનો ઉપયોગ કરીને તમે PHP અને WordPress વર્ઝન જેવા વિકલ્પો નિર્દિષ્ટ કરી શકો છો. નીચેના ઉદાહરણમાં, આપણે PHP 8.3, WordPress નું નવીનતમ વર્ઝન અને આપમેળે લૉગિન થવાની સુવિધા પસંદ કરીએ છીએ. સપોર્ટેડ તમામ આર્ગ્યુમેન્ટ્સ RunCLIArgs ટાઇપમાં વ્યાખ્યાયિત છે.
import { runCLI } from "@wp-playground/cli";
const cliServer = await runCLI({
command: 'server',
php: '8.3',
wp: 'latest',
login: true,
});
ઉપરનો કોડ તમારા પસંદગીના TypeScript runtime દ્વારા ચલાવો, ઉદાહરણ તરીકે tsx:
npx tsx my-script.ts
Blueprint સેટ કરવું
તમે Blueprint બે રીતે આપી શકો છો: blueprint પ્રોપર્ટીમાં સીધો object literal આપીને અથવા બહારની .json ફાઇલનો પાથ ધરાવતી string આપીને.
import { runCLI, RunCLIServer } from "@wp-playground/cli";
const cliServer: RunCLIServer = await runCLI({
command: 'server',
wp: 'latest',
blueprint: {
steps: [
{
"step": "setSiteOptions",
"options": {
"blogname": "Blueprint Title",
"blogdescription": "A great blog description"
}
}
],
},
});
તમારા Blueprint object ને સંપૂર્ણ type-safety સાથે વ્યાખ્યાયિત કરવા માટે, @wp-playground/blueprints પેકેજમાંથી BlueprintDeclaration ટાઇપ import કરીને તેનો ઉપયોગ કરી શકો છો:
import type { BlueprintDeclaration } from '@wp-playground/blueprints';
const myBlueprint: BlueprintDeclaration = {
landingPage: "/wp-admin/",
steps: [
{
"step": "installTheme",
"themeData": {
"resource": "wordpress.org/themes",
"slug": "twentytwentyone"
},
"options": {
"activate": true
}
}
]
};
પ્રોગ્રામેટિક રીતે પ્લગિન માઉન્ટ કરવું
runCLI નો ઉપયોગ કરીને તમે લોકલ ડિરેક્ટરીઓ પ્રોગ્રામેટિક રીતે માઉન્ટ કરી શકો છો. તેના માટે mount અને mount-before-install વિકલ્પો ઉપલબ્ધ છે. hostPath પ્રોપર્ટીમાં તમારા લોકલ કમ્પ્યુટરની ડિરેક્ટરીનો પાથ આપવો પડે છે. આ પાથ તમારી સ્ક્રિપ્ટ જ્યાંથી ચલાવવામાં આવે છે તે સ્થાનના સાપેક્ષ (relative) હોવો જોઈએ.
import { runCLI } from "@wp-playground/cli";
const cliServer = await runCLI({
command: 'server',
login: true,
'mount-before-install': [
{
hostPath: './[my-plugin-local-path]',
vfsPath: '/wordpress/wp-content/plugins/my-plugin',
},
],
});
Mounts ને Blueprints સાથે જોડવા
તમે પ્રોજેક્ટના ભાગોને માઉન્ટ કરવાની પ્રક્રિયાને Blueprints સાથે જોડી શકો છો. ઉદાહરણ તરીકે:
import { runCLI, RunCLIServer } from "@wp-playground/cli";
const cliServer: RunCLIServer = await runCLI({
command: 'server',
php: '8.3',
wp: 'latest',
login: true,
mount: [
{
"hostPath": "./plugin/",
"vfsPath": "/wordpress/wp-content/plugins/playwright-test"
}
],
blueprint: {
steps: [
{
"step": "activatePlugin",
"pluginPath": "/wordpress/wp-content/plugins/playwright-test/plugin-playwright.php"
}
]
}
});
ઓટોમેટેડ ટેસ્ટિંગ
Vitest સાથે ઇન્ટિગ્રેશન ટેસ્ટિંગ
પ્રોગ્રામેટિક API ઓટોમેટેડ ટેસ્ટિંગ માટે ખૂબ જ ઉપયોગી છે. અહીં Vitest નો ઉપયોગ કરતું સંપૂર્ણ ઉદાહરણ છે:
import { describe, test, expect, afterEach } from 'vitest';
import { runCLI, RunCLIServer } from "@wp-playground/cli";
describe('My Plugin Tests', () => {
let cliServer: RunCLIServer | undefined;
afterEach(async () => {
if (cliServer) {
// RunCLIServer exposes Symbol.asyncDispose as its public async cleanup API.
await cliServer[Symbol.asyncDispose]();
}
});
test('plugin activates successfully', async () => {
cliServer = await runCLI({
command: 'server',
mount: [
{
hostPath: './my-plugin',
vfsPath: '/wordpress/wp-content/plugins/my-plugin'
}
],
blueprint: {
steps: [
{
step: 'activatePlugin',
pluginPath: '/wordpress/wp-content/plugins/my-plugin/plugin.php'
}
]
}
});
const homeUrl = new URL('/', cliServer.serverUrl);
const response = await fetch(homeUrl);
expect(response.status).toBe(200);
const html = await response.text();
expect(html).toContain('My Plugin');
});
test('plugin settings page loads', async () => {
cliServer = await runCLI({
command: 'server',
login: true, // Auto-login as admin
mount: [
{
hostPath: './my-plugin',
vfsPath: '/wordpress/wp-content/plugins/my-plugin'
}
],
blueprint: {
steps: [
{
step: 'activatePlugin',
pluginPath: '/wordpress/wp-content/plugins/my-plugin/plugin.php'
}
]
}
});
const settingsUrl = new URL(
'/wp-admin/options-general.php?page=my-plugin',
cliServer.serverUrl
);
const response = await fetch(settingsUrl, { redirect: 'manual' });
// Note: A plain `fetch` call does not send the admin session cookie set by `login: true`,
// so this request is typically redirected to the login page instead of returning 200.
expect(response.status).toBe(302);
});
});
અલગ WordPress/PHP વર્ઝન સાથે ટેસ્ટિંગ
test('plugin works with WordPress 6.4 and PHP 8.3', async () => {
cliServer = await runCLI({
command: 'server',
php: '8.3',
wp: '6.4',
mount: [
{
hostPath: './my-plugin',
vfsPath: '/wordpress/wp-content/plugins/my-plugin'
}
],
blueprint: {
steps: [
{
step: 'activatePlugin',
pluginPath: '/wordpress/wp-content/plugins/my-plugin/plugin.php'
}
]
}
});
const homeUrl = new URL('/', cliServer.serverUrl);
const response = await fetch(homeUrl);
expect(response.status).toBe(200);
});
અદ્યતન કન્ફિગરેશન
WordPress અને SQLite સેટઅપ છોડવું
જો તમારે WordPress વિના માત્ર PHP કોડનું પરીક્ષણ કરવું હોય, તો ઝડપી ટેસ્ટિંગ માટે સેટઅપ પ્રક્રિયા છોડી શકો છો:
import { runCLI } from "@wp-playground/cli";
const cliServer = await runCLI({
command: 'server',
php: '8.3',
wordpressInstallMode: 'do-not-attempt-installing',
skipSqliteSetup: true,
});
// Test PHP version
await cliServer.playground.writeFile(
'/wordpress/version.php',
'<?php echo phpversion(); ?>'
);
const versionUrl = new URL('/version.php', cliServer.serverUrl);
const response = await fetch(versionUrl);
const version = await response.text();
console.log('PHP Version:', version); // Outputs: 8.3.x