mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-11-08 05:30:05 +01:00
♻️ Migrate more tests to Swift Testing
This commit is contained in:
41
tests/unit/_ST/Parsers/Config/BytePhpPreferenceTest.swift
Normal file
41
tests/unit/_ST/Parsers/Config/BytePhpPreferenceTest.swift
Normal file
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// BytePhpPreferenceTest.swift
|
||||
// Unit Tests
|
||||
//
|
||||
// Created by Nico Verbruggen on 04/09/2023.
|
||||
// Copyright © 2023 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import Testing
|
||||
|
||||
struct BytePhpPreferenceTest {
|
||||
@Test func test_can_extract_memory_value() throws {
|
||||
let pref = BytePhpPreference(key: "memory_limit")
|
||||
|
||||
#expect(pref.internalValue == "512M")
|
||||
#expect(pref.unit == .megabyte)
|
||||
#expect(pref.value == 512)
|
||||
}
|
||||
|
||||
@Test func test_can_parse_all_kinds_of_values() throws {
|
||||
var (unit, value) = BytePhpPreference.readFrom(internalValue: "1G")!
|
||||
#expect(unit == .gigabyte)
|
||||
#expect(value == 1)
|
||||
|
||||
(unit, value) = BytePhpPreference.readFrom(internalValue: "256M")!
|
||||
#expect(unit == .megabyte)
|
||||
#expect(value == 256)
|
||||
|
||||
(unit, value) = BytePhpPreference.readFrom(internalValue: "512K")!
|
||||
#expect(unit == .kilobyte)
|
||||
#expect(value == 512)
|
||||
|
||||
(unit, value) = BytePhpPreference.readFrom(internalValue: "1024")!
|
||||
#expect(unit == .kilobyte)
|
||||
#expect(value == 1024)
|
||||
|
||||
(unit, value) = BytePhpPreference.readFrom(internalValue: "-1")!
|
||||
#expect(unit == .kilobyte)
|
||||
#expect(value == -1)
|
||||
}
|
||||
}
|
||||
78
tests/unit/_ST/Parsers/Config/PhpConfigurationFileTest.swift
Normal file
78
tests/unit/_ST/Parsers/Config/PhpConfigurationFileTest.swift
Normal file
@@ -0,0 +1,78 @@
|
||||
//
|
||||
// PhpConfigurationFileTest.swift
|
||||
// PHP Monitor
|
||||
//
|
||||
// Created by Nico Verbruggen on 04/05/2022.
|
||||
// Copyright © 2023 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
|
||||
class PhpConfigurationFileTest {
|
||||
|
||||
static var phpIniFileUrl: URL {
|
||||
return TestBundle.url(forResource: "php", withExtension: "ini")!
|
||||
}
|
||||
|
||||
@Test func test_can_load_extension() throws {
|
||||
let iniFile = PhpConfigurationFile.from(filePath: Self.phpIniFileUrl.path)
|
||||
|
||||
#expect(iniFile != nil)
|
||||
#expect(!iniFile!.extensions.isEmpty)
|
||||
}
|
||||
|
||||
@Test func test_can_check_key_existence() throws {
|
||||
let iniFile = PhpConfigurationFile.from(filePath: Self.phpIniFileUrl.path)!
|
||||
|
||||
#expect(iniFile.has(key: "error_reporting"))
|
||||
#expect(iniFile.has(key: "display_errors"))
|
||||
#expect(false == iniFile.has(key: "my_unknown_key"))
|
||||
}
|
||||
|
||||
@Test func test_can_check_key_value() throws {
|
||||
let iniFile = PhpConfigurationFile.from(filePath: Self.phpIniFileUrl.path)!
|
||||
|
||||
#expect(iniFile.get(for: "error_reporting") != nil)
|
||||
#expect(iniFile.get(for: "error_reporting") == "E_ALL")
|
||||
|
||||
#expect(iniFile.get(for: "display_errors") != nil)
|
||||
#expect(iniFile.get(for: "display_errors") == "On")
|
||||
}
|
||||
|
||||
@Test func test_can_customize_configuration_value() throws {
|
||||
let destination = Utility
|
||||
.copyToTemporaryFile(resourceName: "php", fileExtension: "ini")!
|
||||
|
||||
let configurationFile = PhpConfigurationFile
|
||||
.from(filePath: destination.path)!
|
||||
|
||||
// 0. Verify the original value
|
||||
#expect(configurationFile.get(for: "error_reporting") == "E_ALL")
|
||||
|
||||
// 1. Change the value
|
||||
try! configurationFile.replace(
|
||||
key: "error_reporting",
|
||||
value: "E_ALL & ~E_DEPRECATED & ~E_STRICT"
|
||||
)
|
||||
#expect(
|
||||
configurationFile.get(for: "error_reporting") ==
|
||||
"E_ALL & ~E_DEPRECATED & ~E_STRICT"
|
||||
)
|
||||
|
||||
// 2. Ensure that same key and value doesn't break subsequent saves
|
||||
try! configurationFile.replace(
|
||||
key: "error_reporting",
|
||||
value: "error_reporting"
|
||||
)
|
||||
#expect(configurationFile.get(for: "error_reporting") == "error_reporting")
|
||||
|
||||
// 3. Verify subsequent saves weren't broken
|
||||
try! configurationFile.replace(
|
||||
key: "error_reporting",
|
||||
value: "E_ALL"
|
||||
)
|
||||
#expect(configurationFile.get(for: "error_reporting") == "E_ALL")
|
||||
}
|
||||
|
||||
}
|
||||
91
tests/unit/_ST/Parsers/HomebrewPackageTest.swift
Normal file
91
tests/unit/_ST/Parsers/HomebrewPackageTest.swift
Normal file
@@ -0,0 +1,91 @@
|
||||
//
|
||||
// BrewJsonParserTest.swift
|
||||
// PHP Monitor
|
||||
//
|
||||
// Created by Nico Verbruggen on 14/02/2021.
|
||||
// Copyright © 2023 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
|
||||
struct HomebrewPackageTest {
|
||||
|
||||
// - MARK: SYNTHETIC TESTS
|
||||
|
||||
static var jsonBrewFile: URL {
|
||||
return TestBundle
|
||||
.url(forResource: "brew-formula", withExtension: "json")!
|
||||
}
|
||||
|
||||
static var jsonBrewServicesFile: URL {
|
||||
return TestBundle
|
||||
.url(forResource: "brew-services", withExtension: "json")!
|
||||
}
|
||||
|
||||
@Test func test_can_load_extension_json() throws {
|
||||
let json = try! String(contentsOf: Self.jsonBrewFile, encoding: .utf8)
|
||||
let package = try! JSONDecoder().decode(
|
||||
[HomebrewPackage].self, from: json.data(using: .utf8)!
|
||||
).first!
|
||||
|
||||
#expect(package.full_name == "php")
|
||||
#expect(package.aliases.first! == "php@8.4")
|
||||
#expect(package.installed.contains(where: { installed in
|
||||
installed.version.starts(with: "8.4")
|
||||
}) == true)
|
||||
}
|
||||
|
||||
@Test func test_can_parse_services_json() throws {
|
||||
let json = try! String(contentsOf: Self.jsonBrewServicesFile, encoding: .utf8)
|
||||
let services = try! JSONDecoder().decode(
|
||||
[HomebrewService].self, from: json.data(using: .utf8)!
|
||||
)
|
||||
|
||||
#expect(!services.isEmpty)
|
||||
#expect(services.first?.name == "dnsmasq")
|
||||
#expect(services.first?.service_name == "homebrew.mxcl.dnsmasq")
|
||||
}
|
||||
|
||||
// - MARK: LIVE TESTS
|
||||
|
||||
/// This test requires that you have a valid Homebrew installation set up,
|
||||
/// and requires the Valet services to be installed: php, nginx and dnsmasq.
|
||||
/// If this test fails, there is an issue with your Homebrew installation
|
||||
/// or the JSON API of the Homebrew output may have changed.
|
||||
@Test(.disabled("Uses system command; enable at your own risk"))
|
||||
func test_can_parse_services_json_from_cli_output() async throws {
|
||||
ActiveShell.useSystem()
|
||||
|
||||
let services = try! JSONDecoder().decode(
|
||||
[HomebrewService].self,
|
||||
from: await Shell.pipe(
|
||||
"sudo \(Paths.brew) services info --all --json"
|
||||
).out.data(using: .utf8)!
|
||||
).filter({ service in
|
||||
return ["php", "nginx", "dnsmasq"].contains(service.name)
|
||||
})
|
||||
|
||||
#expect(services.contains(where: {$0.name == "php"}))
|
||||
#expect(services.contains(where: {$0.name == "nginx"}))
|
||||
#expect(services.contains(where: {$0.name == "dnsmasq"}))
|
||||
#expect(services.count == 3)
|
||||
}
|
||||
|
||||
/// This test requires that you have a valid Homebrew installation set up,
|
||||
/// and requires the `php` formula to be installed.
|
||||
/// If this test fails, there is an issue with your Homebrew installation
|
||||
/// or the JSON API of the Homebrew output may have changed.
|
||||
@Test(.disabled("Uses system command; enable at your own risk"))
|
||||
func test_can_load_extension_json_from_cli_output() async throws {
|
||||
|
||||
ActiveShell.useSystem()
|
||||
|
||||
let package = try! JSONDecoder().decode(
|
||||
[HomebrewPackage].self,
|
||||
from: await Shell.pipe("\(Paths.brew) info php --json").out.data(using: .utf8)!
|
||||
).first!
|
||||
|
||||
#expect(package.full_name == "php")
|
||||
}
|
||||
}
|
||||
70
tests/unit/_ST/Parsers/NginxConfigurationTest.swift
Normal file
70
tests/unit/_ST/Parsers/NginxConfigurationTest.swift
Normal file
@@ -0,0 +1,70 @@
|
||||
//
|
||||
// NginxConfigurationTest.swift
|
||||
// PHP Monitor
|
||||
//
|
||||
// Created by Nico Verbruggen on 29/11/2021.
|
||||
// Copyright © 2023 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
|
||||
struct NginxConfigurationTest {
|
||||
|
||||
// MARK: - Test Files
|
||||
|
||||
static var regularUrl: URL {
|
||||
TestBundle.url(forResource: "nginx-site", withExtension: "test")!
|
||||
}
|
||||
|
||||
static var isolatedUrl: URL {
|
||||
TestBundle.url(forResource: "nginx-site-isolated", withExtension: "test")!
|
||||
}
|
||||
|
||||
static var proxyUrl: URL {
|
||||
TestBundle.url(forResource: "nginx-proxy", withExtension: "test")!
|
||||
}
|
||||
|
||||
static var secureProxyUrl: URL {
|
||||
TestBundle.url(forResource: "nginx-secure-proxy", withExtension: "test")!
|
||||
}
|
||||
|
||||
static var customTldProxyUrl: URL {
|
||||
TestBundle.url(forResource: "nginx-secure-proxy-custom-tld", withExtension: "test")!
|
||||
}
|
||||
|
||||
// MARK: - Tests
|
||||
|
||||
@Test func test_can_determine_site_name_and_tld() throws {
|
||||
#expect("nginx-site" == NginxConfigurationFile.from(filePath: Self.regularUrl.path)?.domain)
|
||||
#expect("test" == NginxConfigurationFile.from(filePath: Self.regularUrl.path)?.tld)
|
||||
}
|
||||
|
||||
@Test func test_can_determine_isolation() throws {
|
||||
#expect(nil == NginxConfigurationFile.from(filePath: Self.regularUrl.path)?.isolatedVersion)
|
||||
#expect("8.1" == NginxConfigurationFile.from(filePath: Self.isolatedUrl.path)?.isolatedVersion)
|
||||
}
|
||||
|
||||
@Test func test_can_determine_proxy() throws {
|
||||
let proxied = NginxConfigurationFile.from(filePath: Self.proxyUrl.path)!
|
||||
#expect(proxied.contents.contains("# valet stub: proxy.valet.conf"))
|
||||
#expect("http://127.0.0.1:90" == proxied.proxy)
|
||||
|
||||
let normal = NginxConfigurationFile.from(filePath: Self.regularUrl.path)!
|
||||
#expect(false == normal.contents.contains("# valet stub: proxy.valet.conf"))
|
||||
#expect(nil == normal.proxy)
|
||||
}
|
||||
|
||||
@Test func test_can_determine_secured_proxy() throws {
|
||||
let proxied = NginxConfigurationFile.from(filePath: Self.secureProxyUrl.path)!
|
||||
#expect(proxied.contents.contains("# valet stub: secure.proxy.valet.conf"))
|
||||
#expect("http://127.0.0.1:90" == proxied.proxy)
|
||||
}
|
||||
|
||||
@Test func test_can_determine_proxy_with_custom_tld() throws {
|
||||
let proxied = NginxConfigurationFile.from(filePath: Self.customTldProxyUrl.path)!
|
||||
#expect(proxied.contents.contains("# valet stub: secure.proxy.valet.conf"))
|
||||
#expect("http://localhost:8080" == proxied.proxy)
|
||||
}
|
||||
|
||||
}
|
||||
71
tests/unit/_ST/Parsers/PhpExtensionTest.swift
Normal file
71
tests/unit/_ST/Parsers/PhpExtensionTest.swift
Normal file
@@ -0,0 +1,71 @@
|
||||
//
|
||||
// ExtensionParserTest.swift
|
||||
// PHP Monitor
|
||||
//
|
||||
// Created by Nico Verbruggen on 13/02/2021.
|
||||
// Copyright © 2023 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
|
||||
struct PhpExtensionTest {
|
||||
static var phpIniFileUrl: URL {
|
||||
return TestBundle.url(forResource: "php", withExtension: "ini")!
|
||||
}
|
||||
|
||||
@Test func test_can_load_extension() throws {
|
||||
let extensions = PhpExtension.from(filePath: Self.phpIniFileUrl.path)
|
||||
|
||||
#expect(!extensions.isEmpty)
|
||||
}
|
||||
|
||||
@Test func test_extension_name_is_correct() throws {
|
||||
let extensions = PhpExtension.from(filePath: Self.phpIniFileUrl.path)
|
||||
|
||||
let extensionNames = extensions.map { (ext) -> String in
|
||||
return ext.name
|
||||
}
|
||||
|
||||
// These 6 should be found
|
||||
#expect(extensionNames.contains("xdebug"))
|
||||
#expect(extensionNames.contains("imagick"))
|
||||
#expect(extensionNames.contains("sodium-next"))
|
||||
#expect(extensionNames.contains("opcache"))
|
||||
#expect(extensionNames.contains("yaml"))
|
||||
#expect(extensionNames.contains("custom"))
|
||||
|
||||
#expect(extensionNames.contains("fake") == false)
|
||||
#expect(extensionNames.contains("nice") == false)
|
||||
}
|
||||
|
||||
@Test func test_extension_status_is_correct() throws {
|
||||
let extensions = PhpExtension.from(filePath: Self.phpIniFileUrl.path)
|
||||
|
||||
// xdebug should be enabled
|
||||
#expect(extensions[0].enabled == true)
|
||||
|
||||
// imagick should be disabled
|
||||
#expect(extensions[1].enabled == false)
|
||||
}
|
||||
|
||||
@Test func test_toggle_works_as_expected() async throws {
|
||||
let destination = Utility.copyToTemporaryFile(resourceName: "php", fileExtension: "ini")!
|
||||
let extensions = PhpExtension.from(filePath: destination.path)
|
||||
#expect(extensions.count == 6)
|
||||
|
||||
// Try to disable xdebug (should be detected first)!
|
||||
let xdebug = extensions.first!
|
||||
#expect(xdebug.name == "xdebug")
|
||||
#expect(xdebug.enabled == true)
|
||||
await xdebug.toggle()
|
||||
#expect(xdebug.enabled == false)
|
||||
|
||||
// Check if the file contains the appropriate data
|
||||
let file = try! String(contentsOf: destination, encoding: .utf8)
|
||||
#expect(file.contains("; zend_extension=\"xdebug.so\""))
|
||||
|
||||
// Make sure if we load the data again, it's disabled
|
||||
#expect(PhpExtension.from(filePath: destination.path).first!.enabled == false)
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,6 @@
|
||||
import Testing
|
||||
import Foundation
|
||||
|
||||
@Suite("Parsers")
|
||||
struct ValetConfigurationTest {
|
||||
static var jsonConfigFileUrl: URL {
|
||||
return TestBundle.url(
|
||||
@@ -18,8 +17,7 @@ struct ValetConfigurationTest {
|
||||
)!
|
||||
}
|
||||
|
||||
@Test("Can load config file")
|
||||
func can_load_config_file() throws {
|
||||
@Test func can_load_config_file() throws {
|
||||
let json = try? String(
|
||||
contentsOf: Self.jsonConfigFileUrl,
|
||||
encoding: .utf8
|
||||
@@ -37,5 +35,4 @@ struct ValetConfigurationTest {
|
||||
#expect(config.defaultSite == "/Users/username/default-site")
|
||||
#expect(config.loopback == "127.0.0.1")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
45
tests/unit/_ST/Parsers/ValetRcTest.swift
Normal file
45
tests/unit/_ST/Parsers/ValetRcTest.swift
Normal file
@@ -0,0 +1,45 @@
|
||||
//
|
||||
// ValetRcTest.swift
|
||||
// Unit Tests
|
||||
//
|
||||
// Created by Nico Verbruggen on 20/01/2023.
|
||||
// Copyright © 2023 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
|
||||
struct ValetRcTest {
|
||||
// MARK: - Test Files
|
||||
static var validPath: URL {
|
||||
return TestBundle.url(forResource: "valetrc", withExtension: "valid")!
|
||||
}
|
||||
|
||||
static var brokenPath: URL {
|
||||
return TestBundle.url(forResource: "valetrc", withExtension: "broken")!
|
||||
}
|
||||
|
||||
// MARK: - Tests
|
||||
@Test func test_can_extract_fields_from_valet_rc_file() throws {
|
||||
let fakeFile = RCFile.fromPath("/Users/fake/file.rc")
|
||||
#expect(nil == fakeFile)
|
||||
|
||||
// Can parse the file
|
||||
let validFile = RCFile.fromPath(ValetRcTest.validPath.path)
|
||||
#expect(nil != validFile)
|
||||
|
||||
let fields = validFile!.fields
|
||||
|
||||
// Correctly parses and trims (and omits double quotes) per line
|
||||
#expect(fields["PHP"] == "php@8.2")
|
||||
#expect(fields["OTHER"] == "thing")
|
||||
#expect(fields["PHPMON_WATCH"] == "true")
|
||||
#expect(fields["SYNTAX"] == "variable")
|
||||
|
||||
// Ignores entries prefixed with #
|
||||
#expect(!fields.keys.contains("#PHP"))
|
||||
|
||||
// Ignores invalid lines
|
||||
#expect(!fields.keys.contains("OOF"))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user