1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-07 03:50:08 +02:00

👌 Read configuration from JSON file

This allows us to alter the configuration prior to launching the app,
which allows for additional flexibility during testing.
This commit is contained in:
2022-10-16 14:35:19 +02:00
parent 273070ef27
commit 5e3e0c087b
16 changed files with 602 additions and 102 deletions

View File

@ -0,0 +1,162 @@
//
// TestableConfigurations.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 04/10/2022.
// Copyright © 2022 Nico Verbruggen. All rights reserved.
//
import Foundation
class TestableConfigurations {
/** A functional, working system setup that is compatible with PHP Monitor. */
static var working: TestableConfiguration {
return TestableConfiguration(
architecture: "arm64",
filesystem: [
"/opt/homebrew/bin/brew"
: .fake(.binary),
"/opt/homebrew/bin/php"
: .fake(.binary),
"/opt/homebrew/bin/valet"
: .fake(.binary),
"/opt/homebrew/opt/php"
: .fake(.symlink, "/opt/homebrew/Cellar/php/8.1.10_1"),
"/opt/homebrew/Cellar/php/8.1.10_1"
: .fake(.directory),
"/opt/homebrew/Cellar/php/8.1.10_1/bin/php"
: .fake(.binary),
"/opt/homebrew/Cellar/php/8.1.10_1/bin/php-config"
: .fake(.binary),
"~/.config/valet"
: .fake(.directory),
"/opt/homebrew/etc/php/8.1/php-fpm.d/valet-fpm.conf"
: .fake(.text)
],
shellOutput: [
"sysctl -n sysctl.proc_translated"
: .instant("0"),
"id -un"
: .instant("user"),
"which node"
: .instant("/opt/homebrew/bin/node"),
"php -v"
: .instant("""
PHP 8.1.10 (cli) (built: Sep 3 2022 12:09:27) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.10, Copyright (c) Zend Technologies
with Zend OPcache v8.1.10, Copyright (c), by Zend Technologies
"""),
"ls /opt/homebrew/opt | grep php"
: .instant("php"),
"ls /opt/homebrew/opt | grep php@"
: .instant("php@8.1"),
"sudo /opt/homebrew/bin/brew services info nginx --json"
: .delayed(0.2, """
[
{
"name": "nginx",
"service_name": "homebrew.mxcl.nginx",
"running": true,
"loaded": true,
"schedulable": false,
"pid": 133,
"exit_code": 0,
"user": "root",
"status": "started",
"file": "/Library/LaunchDaemons/homebrew.mxcl.nginx.plist",
"command": "/opt/homebrew/opt/nginx/bin/nginx -g daemon off;",
"working_dir": "/opt/homebrew",
"root_dir": null,
"log_path": null,
"error_log_path": null,
"interval": null,
"cron": null
}
]
"""),
"cat /private/etc/sudoers.d/brew"
: .instant("""
Cmnd_Alias BREW = /opt/homebrew/bin/brew *
%admin ALL=(root) NOPASSWD:SETENV: BREW
"""),
"cat /private/etc/sudoers.d/valet"
: .instant("""
Cmnd_Alias VALET = /opt/homebrew/bin/valet *
%admin ALL=(root) NOPASSWD:SETENV: VALET
"""),
"valet --version"
: .instant("Laravel Valet 3.1.11"),
"/opt/homebrew/bin/brew tap"
: .instant("""
homebrew/cask
homebrew/core
homebrew/services
nicoverbruggen/cask
shivammathur/php
"""),
"chmod +x /Users/nicoverbruggen/.config/phpmon/bin/pm81"
: .instant(""),
"mkdir -p ~/.config/phpmon"
: .instant(""),
"mkdir -p ~/.config/phpmon/bin"
: .instant(""),
"brew info shivammathur/php/php --json"
: .instant("Error: No available formula with the name \"shivammathur/php/php\"."),
"/usr/bin/open -Ra \"PhpStorm\""
: .instant("Unable to find application named 'PhpStorm'", .stdErr),
"/usr/bin/open -Ra \"Visual Studio Code\""
: .instant("Unable to find application named 'Visual Studio Code'", .stdErr),
"/usr/bin/open -Ra \"Sublime Text\""
: .instant("Unable to find application named 'Sublime Text'", .stdErr),
"/usr/bin/open -Ra \"Sublime Merge\""
: .instant("Unable to find application named 'Sublime Merge'", .stdErr),
"/usr/bin/open -Ra \"iTerm\""
: .instant("Unable to find application named 'iTerm'", .stdErr),
"/opt/homebrew/bin/brew info php --json"
: .instant(ShellStrings.shared.brewJson),
"sudo /opt/homebrew/bin/brew services info --all --json"
: .instant(ShellStrings.shared.brewServicesAsRoot),
"/opt/homebrew/bin/brew services info --all --json"
: .instant(ShellStrings.shared.brewServicesAsUser),
"curl -s --max-time 5 '\(Constants.Urls.StableBuildCaskFile.absoluteString)' | grep version"
: .instant("version '5.6.2_976'")
],
commandOutput: [
"/opt/homebrew/bin/php-config --version": "8.1.10",
"/opt/homebrew/bin/php -r echo ini_get('memory_limit');": "512M",
"/opt/homebrew/bin/php -r echo ini_get('upload_max_filesize');": "512M",
"/opt/homebrew/bin/php -r echo ini_get('post_max_size');": "512M",
"/opt/homebrew/bin/php -r echo php_ini_scanned_files();"
: """
/opt/homebrew/etc/php/8.1/conf.d/error_log.ini,
/opt/homebrew/etc/php/8.1/conf.d/ext-opcache.ini,
/opt/homebrew/etc/php/8.1/conf.d/php-memory-limits.ini,
/opt/homebrew/etc/php/8.1/conf.d/xdebug.ini
"""
]
)
}
}
class ShellStrings {
static var shared = ShellStrings()
var brewJson: String = ""
var brewServicesAsUser: String = ""
var brewServicesAsRoot: String = ""
init() {
self.brewJson = loadFile("brew-formula")
self.brewServicesAsUser = loadFile("brew-services-normal")
self.brewServicesAsRoot = loadFile("brew-services-sudo")
}
private func loadFile(_ fileName: String, fileExtension: String = "json") -> String {
let bundle = Bundle(for: type(of: self))
return try! String(contentsOf: bundle.url(
forResource: fileName,
withExtension: fileExtension
)!, encoding: .utf8)
}
}

View File

@ -0,0 +1,23 @@
//
// XCPMApplication.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 16/10/2022.
// Copyright © 2022 Nico Verbruggen. All rights reserved.
//
import XCTest
class XCPMApplication: XCUIApplication {
public func withConfiguration(_ configuration: TestableConfiguration) {
let path = persistTestable(configuration)
self.launchArguments = ["--configuration:\(path)"]
}
private func persistTestable(_ configuration: TestableConfiguration) -> String {
let tempDirectoryURL = NSURL.fileURL(withPath: NSTemporaryDirectory(), isDirectory: true)
let targetURL = tempDirectoryURL.appendingPathComponent("\(UUID().uuidString).json")
try! configuration.toJson().write(toFile: targetURL.path, atomically: true, encoding: .utf8)
return targetURL.path
}
}

View File

@ -1,5 +1,5 @@
//
// UI_Tests.swift
// StartupTest.swift
// UI Tests
//
// Created by Nico Verbruggen on 14/10/2022.
@ -17,14 +17,18 @@ final class StartupTest: UITestCase {
override func tearDownWithError() throws {}
func testApplicationCanLaunchWithTestConfigurationAndIdles() throws {
let app = XCUIApplication()
app.launchArguments = ["--configuration:working"]
let app = XCPMApplication()
app.withConfiguration(TestableConfigurations.working)
app.launch()
sleep(5)
}
func testApplicationCanLaunchWithTestConfigurationAndThrowsAlert() throws {
let app = XCUIApplication()
app.launchArguments = ["--configuration:broken"]
var configuration = TestableConfigurations.working
configuration.filesystem["/opt/homebrew/bin/php"] = nil // PHP binary must be missing
let app = XCPMApplication()
app.withConfiguration(configuration)
app.launch()
// Dialog 1: "PHP is not correctly installed"

View File

@ -0,0 +1,173 @@
[
{
"name": "dnsmasq",
"service_name": "homebrew.mxcl.dnsmasq",
"running": false,
"loaded": false,
"schedulable": false,
"pid": null,
"exit_code": null,
"user": "root",
"status": "none",
"file": "/Users/nicoverbruggen/Library/LaunchAgents/homebrew.mxcl.dnsmasq.plist",
"command": "/opt/homebrew/opt/dnsmasq/sbin/dnsmasq --keep-in-foreground -C /opt/homebrew/etc/dnsmasq.conf -7 /opt/homebrew/etc/dnsmasq.d,*.conf",
"working_dir": null,
"root_dir": null,
"log_path": null,
"error_log_path": null,
"interval": null,
"cron": null
},
{
"name": "httpd",
"service_name": "homebrew.mxcl.httpd",
"running": false,
"loaded": false,
"schedulable": false,
"pid": null,
"exit_code": null,
"user": null,
"status": "none",
"file": "/opt/homebrew/opt/httpd/homebrew.mxcl.httpd.plist",
"command": "/opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND",
"working_dir": null,
"root_dir": null,
"log_path": null,
"error_log_path": null,
"interval": null,
"cron": null
},
{
"name": "mailhog",
"service_name": "homebrew.mxcl.mailhog",
"running": true,
"loaded": true,
"schedulable": false,
"pid": 686,
"exit_code": 0,
"user": "nicoverbruggen",
"status": "started",
"file": "/Users/nicoverbruggen/Library/LaunchAgents/homebrew.mxcl.mailhog.plist",
"command": "/opt/homebrew/opt/mailhog/bin/MailHog",
"working_dir": null,
"root_dir": null,
"log_path": "/opt/homebrew/var/log/mailhog.log",
"error_log_path": "/opt/homebrew/var/log/mailhog.log",
"interval": null,
"cron": null
},
{
"name": "nginx",
"service_name": "homebrew.mxcl.nginx",
"running": false,
"loaded": false,
"schedulable": false,
"pid": null,
"exit_code": null,
"user": "root",
"status": "none",
"file": "/Users/nicoverbruggen/Library/LaunchAgents/homebrew.mxcl.nginx.plist",
"command": "/opt/homebrew/opt/nginx/bin/nginx -g daemon off;",
"working_dir": "/opt/homebrew",
"root_dir": null,
"log_path": null,
"error_log_path": null,
"interval": null,
"cron": null
},
{
"name": "php",
"service_name": "homebrew.mxcl.php",
"running": false,
"loaded": false,
"schedulable": false,
"pid": null,
"exit_code": null,
"user": "root",
"status": "none",
"file": "/Users/nicoverbruggen/Library/LaunchAgents/homebrew.mxcl.php.plist",
"command": "/opt/homebrew/opt/php/sbin/php-fpm --nodaemonize",
"working_dir": "/opt/homebrew/var",
"root_dir": null,
"log_path": null,
"error_log_path": "/opt/homebrew/var/log/php-fpm.log",
"interval": null,
"cron": null
},
{
"name": "php@7.4",
"service_name": "homebrew.mxcl.php@7.4",
"running": false,
"loaded": false,
"schedulable": false,
"pid": null,
"exit_code": null,
"user": null,
"status": "none",
"file": "/opt/homebrew/opt/php@7.4/homebrew.mxcl.php@7.4.plist",
"command": "/opt/homebrew/opt/php@7.4/sbin/php-fpm --nodaemonize",
"working_dir": "/opt/homebrew/var",
"root_dir": null,
"log_path": null,
"error_log_path": "/opt/homebrew/var/log/php-fpm.log",
"interval": null,
"cron": null
},
{
"name": "php@8.0",
"service_name": "homebrew.mxcl.php@8.0",
"running": false,
"loaded": false,
"schedulable": false,
"pid": null,
"exit_code": null,
"user": null,
"status": "none",
"file": "/opt/homebrew/opt/php@8.0/homebrew.mxcl.php@8.0.plist",
"command": "/opt/homebrew/opt/php@8.0/sbin/php-fpm --nodaemonize",
"working_dir": "/opt/homebrew/var",
"root_dir": null,
"log_path": null,
"error_log_path": "/opt/homebrew/var/log/php-fpm.log",
"interval": null,
"cron": null
},
{
"name": "php@8.2",
"service_name": "homebrew.mxcl.php@8.2",
"running": false,
"loaded": false,
"schedulable": false,
"pid": null,
"exit_code": null,
"user": "root",
"status": "none",
"file": "/Users/nicoverbruggen/Library/LaunchAgents/homebrew.mxcl.php@8.2.plist",
"command": "/opt/homebrew/opt/php@8.2/sbin/php-fpm --nodaemonize",
"working_dir": "/opt/homebrew/var",
"root_dir": null,
"log_path": null,
"error_log_path": "/opt/homebrew/var/log/php-fpm.log",
"interval": null,
"cron": null
},
{
"name": "unbound",
"service_name": "homebrew.mxcl.unbound",
"running": false,
"loaded": false,
"schedulable": false,
"pid": null,
"exit_code": null,
"user": null,
"status": "none",
"file": "/opt/homebrew/opt/unbound/homebrew.mxcl.unbound.plist",
"command": "/opt/homebrew/opt/unbound/sbin/unbound -d -c /opt/homebrew/etc/unbound/unbound.conf",
"working_dir": null,
"root_dir": null,
"log_path": null,
"error_log_path": null,
"interval": null,
"cron": null
}
]

View File

@ -0,0 +1,173 @@
[
{
"name": "dnsmasq",
"service_name": "homebrew.mxcl.dnsmasq",
"running": true,
"loaded": true,
"schedulable": false,
"pid": 122,
"exit_code": 0,
"user": "root",
"status": "started",
"file": "/Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist",
"command": "/opt/homebrew/opt/dnsmasq/sbin/dnsmasq --keep-in-foreground -C /opt/homebrew/etc/dnsmasq.conf -7 /opt/homebrew/etc/dnsmasq.d,*.conf",
"working_dir": null,
"root_dir": null,
"log_path": null,
"error_log_path": null,
"interval": null,
"cron": null
},
{
"name": "httpd",
"service_name": "homebrew.mxcl.httpd",
"running": false,
"loaded": false,
"schedulable": false,
"pid": null,
"exit_code": null,
"user": null,
"status": "none",
"file": "/opt/homebrew/opt/httpd/homebrew.mxcl.httpd.plist",
"command": "/opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND",
"working_dir": null,
"root_dir": null,
"log_path": null,
"error_log_path": null,
"interval": null,
"cron": null
},
{
"name": "mailhog",
"service_name": "homebrew.mxcl.mailhog",
"running": false,
"loaded": false,
"schedulable": false,
"pid": null,
"exit_code": null,
"user": "root",
"status": "none",
"file": "/Library/LaunchDaemons/homebrew.mxcl.mailhog.plist",
"command": "/opt/homebrew/opt/mailhog/bin/MailHog",
"working_dir": null,
"root_dir": null,
"log_path": "/opt/homebrew/var/log/mailhog.log",
"error_log_path": "/opt/homebrew/var/log/mailhog.log",
"interval": null,
"cron": null
},
{
"name": "nginx",
"service_name": "homebrew.mxcl.nginx",
"running": true,
"loaded": true,
"schedulable": false,
"pid": 133,
"exit_code": 0,
"user": "root",
"status": "started",
"file": "/Library/LaunchDaemons/homebrew.mxcl.nginx.plist",
"command": "/opt/homebrew/opt/nginx/bin/nginx -g daemon off;",
"working_dir": "/opt/homebrew",
"root_dir": null,
"log_path": null,
"error_log_path": null,
"interval": null,
"cron": null
},
{
"name": "php",
"service_name": "homebrew.mxcl.php",
"running": true,
"loaded": true,
"schedulable": false,
"pid": 160,
"exit_code": 0,
"user": "root",
"status": "started",
"file": "/Library/LaunchDaemons/homebrew.mxcl.php.plist",
"command": "/opt/homebrew/opt/php/sbin/php-fpm --nodaemonize",
"working_dir": "/opt/homebrew/var",
"root_dir": null,
"log_path": null,
"error_log_path": "/opt/homebrew/var/log/php-fpm.log",
"interval": null,
"cron": null
},
{
"name": "php@7.4",
"service_name": "homebrew.mxcl.php@7.4",
"running": false,
"loaded": false,
"schedulable": false,
"pid": null,
"exit_code": null,
"user": null,
"status": "none",
"file": "/opt/homebrew/opt/php@7.4/homebrew.mxcl.php@7.4.plist",
"command": "/opt/homebrew/opt/php@7.4/sbin/php-fpm --nodaemonize",
"working_dir": "/opt/homebrew/var",
"root_dir": null,
"log_path": null,
"error_log_path": "/opt/homebrew/var/log/php-fpm.log",
"interval": null,
"cron": null
},
{
"name": "php@8.0",
"service_name": "homebrew.mxcl.php@8.0",
"running": false,
"loaded": false,
"schedulable": false,
"pid": null,
"exit_code": null,
"user": null,
"status": "none",
"file": "/opt/homebrew/opt/php@8.0/homebrew.mxcl.php@8.0.plist",
"command": "/opt/homebrew/opt/php@8.0/sbin/php-fpm --nodaemonize",
"working_dir": "/opt/homebrew/var",
"root_dir": null,
"log_path": null,
"error_log_path": "/opt/homebrew/var/log/php-fpm.log",
"interval": null,
"cron": null
},
{
"name": "php@8.2",
"service_name": "homebrew.mxcl.php@8.2",
"running": true,
"loaded": true,
"schedulable": false,
"pid": 147,
"exit_code": 0,
"user": "root",
"status": "started",
"file": "/Library/LaunchDaemons/homebrew.mxcl.php@8.2.plist",
"command": "/opt/homebrew/opt/php@8.2/sbin/php-fpm --nodaemonize",
"working_dir": "/opt/homebrew/var",
"root_dir": null,
"log_path": null,
"error_log_path": "/opt/homebrew/var/log/php-fpm.log",
"interval": null,
"cron": null
},
{
"name": "unbound",
"service_name": "homebrew.mxcl.unbound",
"running": false,
"loaded": false,
"schedulable": false,
"pid": null,
"exit_code": null,
"user": null,
"status": "none",
"file": "/opt/homebrew/opt/unbound/homebrew.mxcl.unbound.plist",
"command": "/opt/homebrew/opt/unbound/sbin/unbound -d -c /opt/homebrew/etc/unbound/unbound.conf",
"working_dir": null,
"root_dir": null,
"log_path": null,
"error_log_path": null,
"interval": null,
"cron": null
}
]

View File

@ -0,0 +1,21 @@
//
// TestableConfigurationTest.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 16/10/2022.
// Copyright © 2022 Nico Verbruggen. All rights reserved.
//
import XCTest
class TestableConfigurationTest: XCTestCase {
func test_configuration_can_be_saved_as_json() async {
var configuration = TestableConfigurations.working
configuration.filesystem["/opt/homebrew/bin/php"] = nil
print(configuration.filesystem.keys)
let json = configuration.toJson()
try! json.write(toFile: "/tmp/pmc_working.json", atomically: true, encoding: .utf8)
try! json.write(toFile: "/tmp/pmc_broken.json", atomically: true, encoding: .utf8)
}
}