Skip to content
Prev Previous commit
Next Next commit
Remove getPhpVersion hook and IPC handler (#239)
* Remove `getPhpVersion` hook and IPC handler

* Use `??` operator instead of `||` when setting the php version

* Add inline comment in Jest setup

The comment clarifies why we need the polyfill.
Related to: #231 (comment)

* Update `ContentTabSettings` unit tests
  • Loading branch information
fluiddot authored Jun 13, 2024
commit de6ed02eb01dfd196034d8bf3a49525bef8c0351
2 changes: 2 additions & 0 deletions jest-setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import '@testing-library/jest-dom';
// We need this polyfill because the `ReadableStream` class is
// used by `@php-wasm/universal` and it's not available in the Jest environment.
// eslint-disable-next-line import/no-unresolved
import 'web-streams-polyfill/polyfill';
import nock from 'nock';
Expand Down
4 changes: 2 additions & 2 deletions src/components/content-tab-settings.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Icon, file } from '@wordpress/icons';
import { useI18n } from '@wordpress/react-i18n';
import { PropsWithChildren } from 'react';
import { useGetPhpVersion } from '../hooks/use-get-php-version';
import { DEFAULT_PHP_VERSION } from '../../vendor/wp-now/src/constants';
import { useGetWpVersion } from '../hooks/use-get-wp-version';
import { getIpcApi } from '../lib/get-ipc-api';
import { decodePassword } from '../lib/passwords';
Expand Down Expand Up @@ -32,7 +32,7 @@ export function ContentTabSettings( { selectedSite }: ContentTabSettingsProps )
// Empty strings account for legacy sites lacking a stored password.
const storedPassword = decodePassword( selectedSite.adminPassword ?? '' );
const password = storedPassword === '' ? 'password' : storedPassword;
const phpVersion = useGetPhpVersion( selectedSite );
const phpVersion = selectedSite.phpVersion ?? DEFAULT_PHP_VERSION;
const wpVersion = useGetWpVersion( selectedSite );
return (
<div className="p-8">
Expand Down
13 changes: 4 additions & 9 deletions src/components/tests/content-tab-settings.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// To run tests, execute `npm run test -- src/components/content-tab-settings.test.tsx` from the root directory
import { fireEvent, render, screen, within } from '@testing-library/react';
import { userEvent } from '@testing-library/user-event';
import { useGetPhpVersion } from '../../hooks/use-get-php-version';
import { useGetWpVersion } from '../../hooks/use-get-wp-version';
import { useOffline } from '../../hooks/use-offline';
import { useSiteDetails } from '../../hooks/use-site-details';
import { getIpcApi } from '../../lib/get-ipc-api';
import { ContentTabSettings } from '../content-tab-settings';

jest.mock( '../../hooks/use-get-wp-version' );
jest.mock( '../../hooks/use-get-php-version' );
jest.mock( '../../hooks/use-site-details' );
jest.mock( '../../lib/get-ipc-api' );

Expand All @@ -30,7 +28,6 @@ describe( 'ContentTabSettings', () => {
beforeEach( () => {
jest.clearAllMocks();
( useGetWpVersion as jest.Mock ).mockReturnValue( '7.7.7' );
( useGetPhpVersion as jest.Mock ).mockReturnValue( '8.0' );
( getIpcApi as jest.Mock ).mockReturnValue( {
copyText,
openLocalPath,
Expand Down Expand Up @@ -168,9 +165,9 @@ describe( 'ContentTabSettings', () => {
startServer,
stopServer,
} );
( useGetPhpVersion as jest.Mock ).mockReturnValue( '8.0' );

const { rerender } = render( <ContentTabSettings selectedSite={ selectedSite } /> );
expect( screen.getByText( '8.0' ) ).toBeVisible();
await user.click( screen.getByRole( 'button', { name: 'Edit PHP version' } ) );
const dialog = screen.getByRole( 'dialog' );
expect( dialog ).toBeVisible();
Expand All @@ -189,8 +186,7 @@ describe( 'ContentTabSettings', () => {
expect( stopServer ).not.toHaveBeenCalled();
expect( startServer ).not.toHaveBeenCalled();

( useGetPhpVersion as jest.Mock ).mockReturnValue( '8.2' );
rerender( <ContentTabSettings selectedSite={ selectedSite } /> );
rerender( <ContentTabSettings selectedSite={ { ...selectedSite, phpVersion: '8.2' } } /> );
expect( screen.getByText( '8.2' ) ).toBeVisible();
} );

Expand All @@ -208,9 +204,9 @@ describe( 'ContentTabSettings', () => {
startServer,
stopServer,
} );
( useGetPhpVersion as jest.Mock ).mockReturnValue( '8.0' );

const { rerender } = render( <ContentTabSettings selectedSite={ selectedSite } /> );
expect( screen.getByText( '8.0' ) ).toBeVisible();
await user.click( screen.getByRole( 'button', { name: 'Edit PHP version' } ) );
const dialog = screen.getByRole( 'dialog' );
expect( dialog ).toBeVisible();
Expand All @@ -229,8 +225,7 @@ describe( 'ContentTabSettings', () => {
expect( stopServer ).toHaveBeenCalled();
expect( startServer ).toHaveBeenCalled();

( useGetPhpVersion as jest.Mock ).mockReturnValue( '8.2' );
rerender( <ContentTabSettings selectedSite={ selectedSite } /> );
rerender( <ContentTabSettings selectedSite={ { ...selectedSite, phpVersion: '8.2' } } /> );
expect( screen.getByText( '8.2' ) ).toBeVisible();
} );
} );
Expand Down
11 changes: 0 additions & 11 deletions src/hooks/use-get-php-version.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/ipc-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,14 +465,6 @@ export async function getAppGlobals( _event: IpcMainInvokeEvent ): Promise< AppG
};
}

export async function getPhpVersion( _event: IpcMainInvokeEvent, id: string ) {
const server = SiteServer.get( id );
if ( ! server ) {
return DEFAULT_PHP_VERSION;
}
return server.details.phpVersion || DEFAULT_PHP_VERSION;
}

export async function getWpVersion( _event: IpcMainInvokeEvent, id: string ) {
const server = SiteServer.get( id );
if ( ! server ) {
Expand Down
1 change: 0 additions & 1 deletion src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const api: IpcApi = {
copyText: ( text: string ) => ipcRenderer.invoke( 'copyText', text ),
getAppGlobals: () => ipcRenderer.invoke( 'getAppGlobals' ),
removeTemporalFile: ( path: string ) => ipcRenderer.invoke( 'removeTemporalFile', path ),
getPhpVersion: ( id: string ) => ipcRenderer.invoke( 'getPhpVersion', id ),
getWpVersion: ( id: string ) => ipcRenderer.invoke( 'getWpVersion', id ),
generateProposedSitePath: ( siteName: string ) =>
ipcRenderer.invoke( 'generateProposedSitePath', siteName ),
Expand Down
2 changes: 1 addition & 1 deletion src/site-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class SiteServer {
...this.details,
url: this.server.url,
port: this.server.options.port,
phpVersion: this.server.options.phpVersion || DEFAULT_PHP_VERSION,
phpVersion: this.server.options.phpVersion ?? DEFAULT_PHP_VERSION,
running: true,
themeDetails,
};
Expand Down