Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions src/core/handlers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ function Invoke-List {
function Invoke-Install {
param ($arguments)

$version = $arguments[0]
$arch = Resolve-Arch -arguments $arguments
$buildType = Resolve-BuildType -arguments $arguments

$version = $arguments[0]
if ($version -eq 'auto') {
$result = Select-PHPVersionAutomatically
if (-not $result.version) {
Expand All @@ -136,24 +136,40 @@ function Invoke-Install {
return -1
}

$version = $result.version
} elseif ($version -eq 'latest') {
return (Install-PHP -version $result.version -arch $arch -buildType $buildType)
}

if ($version -eq 'latest') {
$latestVersion = Get-LatestPHPVersion -arch $arch -buildType $buildType
if (-not $latestVersion) {
Show-Error -message "`nFailed to find the latest PHP version"
return -1
}

$version = $latestVersion.version
Show-Message -message "`nLatest available PHP version is $version"
if (-not $latestVersion.version) {
Show-Warning -message "`nPlease provide a PHP version to install"
return -1
}

Show-Message -message "`nLatest available PHP version is $($latestVersion.version)"

return (Install-PHP -version $latestVersion.version -arch $arch -buildType $buildType)
}

if (-not $version) {
$versions = Resolve-VersionsFromArguments -arguments $arguments

if ($versions.Count -eq 0) {
Show-Warning -message "`nPlease provide a PHP version to install"
return -1
}

return (Install-PHP -version $version -arch $arch -buildType $buildType)
$codes = @()
foreach ($version in $versions) {
$codes += Install-PHP -version $version -arch $arch -buildType $buildType
}

if ($codes | Where-Object -FilterScript { $_ -ne 0 }) { return -1 }
return 0
}

function Invoke-Use {
Expand Down Expand Up @@ -187,17 +203,24 @@ function Invoke-Use {
function Invoke-Uninstall {
param ($arguments)

$version = $arguments[0]
$versions = Resolve-VersionsFromArguments -arguments $arguments

if (-not $version) {
if ($versions.Count -eq 0) {
Show-Warning -message "`nPlease provide a PHP version to uninstall"
return -1
}

$remainingArgs = if ($arguments.Count -gt 1) { $arguments[1..($arguments.Count - 1)] } else { @() }
$remainingArgs = $arguments | Where-Object -FilterScript { $_ -notin $versions }
$skipConfirmation = [bool]($remainingArgs | Where-Object -FilterScript { @('-y', '--yes') -contains $_ } | Select-Object -First 1)

return (Uninstall-PHP -version $version -skipConfirmation $skipConfirmation)
$results = @()
foreach ($version in $versions) {
$result = Uninstall-PHP -version $version -skipConfirmation $skipConfirmation
$results += $result
}

if ($results | Where-Object -FilterScript { $_ -ne 0 }) { return -1 }
return 0
}

function Invoke-Ini {
Expand Down
10 changes: 9 additions & 1 deletion src/helpers/args.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


function Resolve-Alias {
param ($alias)

Expand Down Expand Up @@ -65,3 +65,11 @@ function Resolve-Arch {

return $arch
}

function Resolve-VersionsFromArguments {
param ($arguments)

$versions = $arguments | Where-Object -FilterScript { $_ -match '^\d+(\.\d+){0,2}$' }

return $versions
}
166 changes: 163 additions & 3 deletions tests/core/handlers.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,9 @@ Describe "Invoke-Install" {
}

It "Should install detected PHP version from the project" {
$arguments = @('auto')
Mock Select-PHPVersionAutomatically { return @{ code = -1; version = '8.1' } }

Mock Get-MatchingPHPVersions { return @() }
Mock Find-PHPVersionFromProject { return '8.1' }
$arguments = @('auto')
$result = Invoke-Install -arguments $arguments
$result | Should -Be 0

Expand Down Expand Up @@ -309,6 +308,86 @@ Describe "Invoke-Install" {

$result | Should -Be -1
}

It "Should resturn -1 when version property is null and displays a warning message" {
$arguments = @('latest')
Mock Get-LatestPHPVersion { return @{version = $null } }

$result = Invoke-Install -arguments $arguments

$result | Should -Be -1
Should -Invoke Show-Warning -ParameterFilter { $message -like '*Please provide a PHP version to install*' }
}

It "Should return -1 when detected PHP version is already installed" {
Mock Select-PHPVersionAutomatically { return @{ code = 0; version = '8.2' } }

$arguments = @('auto')
$result = Invoke-Install -arguments $arguments

$result | Should -Be -1
}

It "Should install multiple PHP versions when multiple versions are provided" {
$arguments = @('8.2.0', '8.3.0', '8.1.0')
Mock Install-PHP { return 0 }

$result = Invoke-Install -arguments $arguments
$result | Should -Be 0

Should -Invoke Install-PHP -Times 3
}

It "Should install each version with correct parameters when multiple versions are provided" {
$arguments = @('8.2.0', '8.3.0')
Mock Install-PHP { return 0 }

$result = Invoke-Install -arguments $arguments
$result | Should -Be 0

Should -Invoke Install-PHP -Times 1 -ParameterFilter { $version -eq '8.2.0' }
Should -Invoke Install-PHP -Times 1 -ParameterFilter { $version -eq '8.3.0' }
}

It "Should return -1 when any version installation fails in multiple versions" {
$arguments = @('8.2.0', '8.3.0')
Mock Install-PHP { return 0 }
Mock Install-PHP { return -1 } -ParameterFilter { $version -eq '8.3.0' }

$result = Invoke-Install -arguments $arguments
$result | Should -Be -1
}

It "Should continue installing remaining versions even if one fails" {
$arguments = @('8.2.0', '8.3.0', '8.1.0')
Mock Install-PHP { return 0 }
Mock Install-PHP { return -1 } -ParameterFilter { $version -eq '8.3.0' }

$result = Invoke-Install -arguments $arguments
$result | Should -Be -1

Should -Invoke Install-PHP -Times 3
}

It "Should handle multiple versions with arch and buildType flags" {
$arguments = @('8.2.0', '8.3.0', 'x64', 'ts')
Mock Install-PHP { return 0 }

$result = Invoke-Install -arguments $arguments
$result | Should -Be 0

Should -Invoke Install-PHP -Times 2 -ParameterFilter { $arch -eq 'x64' -and $buildType -eq 'ts' }
}

It "Should filter out non-version arguments when installing multiple versions" {
$arguments = @('8.2.0', '--flag', '8.3.0', '--another-flag')
Mock Install-PHP { return 0 }

$result = Invoke-Install -arguments $arguments
$result | Should -Be 0

Should -Invoke Install-PHP -Times 2
}
}

Describe "Invoke-Use" {
Expand Down Expand Up @@ -424,6 +503,87 @@ Describe "Invoke-Uninstall" {
$version -eq '8.2.0' -and $skipConfirmation -eq $false
}
}

It "Should uninstall multiple PHP versions when multiple versions are provided" {
$arguments = @('8.2.0', '8.3.0', '8.1.0')
Mock Uninstall-PHP { return 0 }

$result = Invoke-Uninstall -arguments $arguments
$result | Should -Be 0

Should -Invoke Uninstall-PHP -Times 3
}

It "Should uninstall each version with correct parameters when multiple versions are provided" {
$arguments = @('8.2.0', '8.3.0')
Mock Uninstall-PHP { return 0 }

$result = Invoke-Uninstall -arguments $arguments
$result | Should -Be 0

Should -Invoke Uninstall-PHP -Times 1 -ParameterFilter { $version -eq '8.2.0' }
Should -Invoke Uninstall-PHP -Times 1 -ParameterFilter { $version -eq '8.3.0' }
}

It "Should return -1 when any version uninstall fails in multiple versions" {
$arguments = @('8.2.0', '8.3.0')
Mock Uninstall-PHP { return 0 }
Mock Uninstall-PHP { return -1 } -ParameterFilter { $version -eq '8.3.0' }

$result = Invoke-Uninstall -arguments $arguments
$result | Should -Be -1
}

It "Should continue uninstalling remaining versions even if one fails" {
$arguments = @('8.2.0', '8.3.0', '8.1.0')
Mock Uninstall-PHP { return 0 }
Mock Uninstall-PHP { return -1 } -ParameterFilter { $version -eq '8.3.0' }

$result = Invoke-Uninstall -arguments $arguments
$result | Should -Be -1

Should -Invoke Uninstall-PHP -Times 3
}

It "Should pass skipConfirmation true to all versions when -y flag is provided with multiple versions" {
$arguments = @('8.2.0', '8.3.0', '-y')
Mock Uninstall-PHP { return 0 }

$result = Invoke-Uninstall -arguments $arguments
$result | Should -Be 0

Should -Invoke Uninstall-PHP -Times 2 -ParameterFilter { $skipConfirmation -eq $true }
}

It "Should pass skipConfirmation true to all versions when --yes flag is provided with multiple versions" {
$arguments = @('8.2.0', '8.3.0', '--yes')
Mock Uninstall-PHP { return 0 }

$result = Invoke-Uninstall -arguments $arguments
$result | Should -Be 0

Should -Invoke Uninstall-PHP -Times 2 -ParameterFilter { $skipConfirmation -eq $true }
}

It "Should filter out non-version arguments when uninstalling multiple versions" {
$arguments = @('8.2.0', '--flag', '8.3.0', '--another-flag')
Mock Uninstall-PHP { return 0 }

$result = Invoke-Uninstall -arguments $arguments
$result | Should -Be 0

Should -Invoke Uninstall-PHP -Times 2
}

It "Should handle mixed version and flag arguments correctly" {
$arguments = @('8.2.0', '-y', '8.3.0', '--force')
Mock Uninstall-PHP { return 0 }

$result = Invoke-Uninstall -arguments $arguments
$result | Should -Be 0

Should -Invoke Uninstall-PHP -Times 2 -ParameterFilter { $skipConfirmation -eq $true }
}
}

Describe "Invoke-Ini" {
Expand Down
Loading