|
| 1 | +import { arePathsEqual } from 'common/lib/fs-utils'; |
| 2 | +import { |
| 3 | + SiteData, |
| 4 | + getSiteByFolder, |
| 5 | + lockAppdata, |
| 6 | + readAppdata, |
| 7 | + saveAppdata, |
| 8 | + unlockAppdata, |
| 9 | +} from 'cli/lib/appdata'; |
| 10 | +import { connect, disconnect } from 'cli/lib/pm2-manager'; |
| 11 | +import { |
| 12 | + isServerRunning, |
| 13 | + startWordPressServer, |
| 14 | + stopWordPressServer, |
| 15 | +} from 'cli/lib/wordpress-server-manager'; |
| 16 | + |
| 17 | +jest.mock( 'cli/lib/appdata', () => ( { |
| 18 | + ...jest.requireActual( 'cli/lib/appdata' ), |
| 19 | + getSiteByFolder: jest.fn(), |
| 20 | + lockAppdata: jest.fn(), |
| 21 | + readAppdata: jest.fn(), |
| 22 | + saveAppdata: jest.fn(), |
| 23 | + unlockAppdata: jest.fn(), |
| 24 | +} ) ); |
| 25 | +jest.mock( 'cli/lib/pm2-manager' ); |
| 26 | +jest.mock( 'cli/lib/wordpress-server-manager' ); |
| 27 | +jest.mock( 'common/lib/fs-utils' ); |
| 28 | + |
| 29 | +describe( 'Site Set-PHP-Version Command', () => { |
| 30 | + const testSitePath = '/test/site/path'; |
| 31 | + |
| 32 | + const createTestSite = (): SiteData => ( { |
| 33 | + id: 'test-site-id', |
| 34 | + name: 'Test Site', |
| 35 | + path: testSitePath, |
| 36 | + port: 8881, |
| 37 | + adminUsername: 'admin', |
| 38 | + adminPassword: 'password123', |
| 39 | + running: false, |
| 40 | + phpVersion: '8.0', |
| 41 | + url: `http://localhost:8881`, |
| 42 | + enableHttps: false, |
| 43 | + customDomain: 'test.local', |
| 44 | + } ); |
| 45 | + |
| 46 | + const testProcessDescription = { |
| 47 | + name: 'test-site-id', |
| 48 | + pmId: 0, |
| 49 | + status: 'online', |
| 50 | + pid: 12345, |
| 51 | + }; |
| 52 | + |
| 53 | + let testSite: SiteData; |
| 54 | + |
| 55 | + beforeEach( () => { |
| 56 | + jest.clearAllMocks(); |
| 57 | + |
| 58 | + testSite = createTestSite(); |
| 59 | + |
| 60 | + ( getSiteByFolder as jest.Mock ).mockResolvedValue( testSite ); |
| 61 | + ( connect as jest.Mock ).mockResolvedValue( undefined ); |
| 62 | + ( disconnect as jest.Mock ).mockReturnValue( undefined ); |
| 63 | + ( lockAppdata as jest.Mock ).mockResolvedValue( undefined ); |
| 64 | + ( readAppdata as jest.Mock ).mockResolvedValue( { |
| 65 | + sites: [ testSite ], |
| 66 | + snapshots: [], |
| 67 | + } ); |
| 68 | + ( saveAppdata as jest.Mock ).mockResolvedValue( undefined ); |
| 69 | + ( unlockAppdata as jest.Mock ).mockResolvedValue( undefined ); |
| 70 | + ( isServerRunning as jest.Mock ).mockResolvedValue( undefined ); |
| 71 | + ( startWordPressServer as jest.Mock ).mockResolvedValue( testProcessDescription ); |
| 72 | + ( stopWordPressServer as jest.Mock ).mockResolvedValue( undefined ); |
| 73 | + ( arePathsEqual as jest.Mock ).mockImplementation( ( a: string, b: string ) => a === b ); |
| 74 | + } ); |
| 75 | + |
| 76 | + afterEach( () => { |
| 77 | + jest.restoreAllMocks(); |
| 78 | + } ); |
| 79 | + |
| 80 | + describe( 'Error Cases', () => { |
| 81 | + it( 'should throw when PHP version is identical to current version', async () => { |
| 82 | + const { runCommand } = await import( '../set-php-version' ); |
| 83 | + |
| 84 | + await expect( runCommand( testSitePath, '8.0' ) ).rejects.toThrow( |
| 85 | + 'Site is already using the specified PHP version.' |
| 86 | + ); |
| 87 | + expect( disconnect ).toHaveBeenCalled(); |
| 88 | + } ); |
| 89 | + |
| 90 | + it( 'should throw when site not found in appdata', async () => { |
| 91 | + ( readAppdata as jest.Mock ).mockResolvedValue( { |
| 92 | + sites: [], |
| 93 | + snapshots: [], |
| 94 | + } ); |
| 95 | + |
| 96 | + const { runCommand } = await import( '../set-php-version' ); |
| 97 | + |
| 98 | + await expect( runCommand( testSitePath, '7.4' ) ).rejects.toThrow( |
| 99 | + 'The specified folder is not added to Studio.' |
| 100 | + ); |
| 101 | + expect( disconnect ).toHaveBeenCalled(); |
| 102 | + } ); |
| 103 | + |
| 104 | + it( 'should throw when appdata save fails', async () => { |
| 105 | + ( saveAppdata as jest.Mock ).mockRejectedValue( new Error( 'Save failed' ) ); |
| 106 | + |
| 107 | + const { runCommand } = await import( '../set-php-version' ); |
| 108 | + |
| 109 | + await expect( runCommand( testSitePath, '7.4' ) ).rejects.toThrow(); |
| 110 | + expect( disconnect ).toHaveBeenCalled(); |
| 111 | + } ); |
| 112 | + |
| 113 | + it( 'should throw when PM2 connection fails', async () => { |
| 114 | + ( connect as jest.Mock ).mockRejectedValue( new Error( 'PM2 connection failed' ) ); |
| 115 | + |
| 116 | + const { runCommand } = await import( '../set-php-version' ); |
| 117 | + |
| 118 | + await expect( runCommand( testSitePath, '7.4' ) ).rejects.toThrow(); |
| 119 | + expect( disconnect ).toHaveBeenCalled(); |
| 120 | + } ); |
| 121 | + |
| 122 | + it( 'should throw when WordPress server stop fails', async () => { |
| 123 | + ( isServerRunning as jest.Mock ).mockResolvedValue( testProcessDescription ); |
| 124 | + ( stopWordPressServer as jest.Mock ).mockRejectedValue( new Error( 'Server stop failed' ) ); |
| 125 | + |
| 126 | + const { runCommand } = await import( '../set-php-version' ); |
| 127 | + |
| 128 | + await expect( runCommand( testSitePath, '7.4' ) ).rejects.toThrow(); |
| 129 | + expect( disconnect ).toHaveBeenCalled(); |
| 130 | + } ); |
| 131 | + } ); |
| 132 | + |
| 133 | + describe( 'Success Cases', () => { |
| 134 | + it( 'should update PHP version on a stopped site', async () => { |
| 135 | + const { runCommand } = await import( '../set-php-version' ); |
| 136 | + |
| 137 | + await runCommand( testSitePath, '7.4' ); |
| 138 | + |
| 139 | + expect( getSiteByFolder ).toHaveBeenCalledWith( testSitePath ); |
| 140 | + expect( lockAppdata ).toHaveBeenCalled(); |
| 141 | + expect( readAppdata ).toHaveBeenCalled(); |
| 142 | + expect( saveAppdata ).toHaveBeenCalled(); |
| 143 | + |
| 144 | + const savedAppdata = ( saveAppdata as jest.Mock ).mock.calls[ 0 ][ 0 ]; |
| 145 | + expect( savedAppdata.sites[ 0 ].phpVersion ).toBe( '7.4' ); |
| 146 | + |
| 147 | + expect( unlockAppdata ).toHaveBeenCalled(); |
| 148 | + expect( isServerRunning ).toHaveBeenCalledWith( testSite.id ); |
| 149 | + expect( stopWordPressServer ).not.toHaveBeenCalled(); |
| 150 | + expect( startWordPressServer ).not.toHaveBeenCalled(); |
| 151 | + expect( disconnect ).toHaveBeenCalled(); |
| 152 | + } ); |
| 153 | + |
| 154 | + it( 'should update PHP version and restart a running site', async () => { |
| 155 | + ( isServerRunning as jest.Mock ).mockResolvedValue( testProcessDescription ); |
| 156 | + |
| 157 | + const { runCommand } = await import( '../set-php-version' ); |
| 158 | + |
| 159 | + await runCommand( testSitePath, '7.4' ); |
| 160 | + |
| 161 | + expect( saveAppdata ).toHaveBeenCalled(); |
| 162 | + const savedAppdata = ( saveAppdata as jest.Mock ).mock.calls[ 0 ][ 0 ]; |
| 163 | + expect( savedAppdata.sites[ 0 ].phpVersion ).toBe( '7.4' ); |
| 164 | + |
| 165 | + expect( isServerRunning ).toHaveBeenCalledWith( testSite.id ); |
| 166 | + expect( stopWordPressServer ).toHaveBeenCalledWith( testSite.id ); |
| 167 | + expect( startWordPressServer ).toHaveBeenCalledWith( expect.any( Object ) ); |
| 168 | + expect( disconnect ).toHaveBeenCalled(); |
| 169 | + } ); |
| 170 | + } ); |
| 171 | +} ); |
0 commit comments