From 4c752b6a15d1f8d684f69b7dc4c3c4ee3b65f45b Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Sun, 16 Oct 2022 15:13:13 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Localization=20support=20for=20in?= =?UTF-8?q?=20test=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHP Monitor.xcodeproj/project.pbxproj | 6 +++ phpmon/Common/Extensions/DataExtension.swift | 4 +- .../Common/Extensions/StringExtension.swift | 17 ++++-- phpmon/Domain/App/App.swift | 17 +++--- phpmon/Domain/App/ServicesManager.swift | 8 +-- phpmon/Localizable.strings | 6 +++ tests/ui/StartupTest.swift | 54 +++++++++++++------ 7 files changed, 82 insertions(+), 30 deletions(-) diff --git a/PHP Monitor.xcodeproj/project.pbxproj b/PHP Monitor.xcodeproj/project.pbxproj index c549a24..f6b744e 100644 --- a/PHP Monitor.xcodeproj/project.pbxproj +++ b/PHP Monitor.xcodeproj/project.pbxproj @@ -136,6 +136,9 @@ C44F868E2835BD8D005C353A /* phpmon-config.json in Resources */ = {isa = PBXBuildFile; fileRef = C44F868D2835BD8D005C353A /* phpmon-config.json */; }; C450C8C628C919EC002A2B4B /* PreferenceName.swift in Sources */ = {isa = PBXBuildFile; fileRef = C450C8C528C919EC002A2B4B /* PreferenceName.swift */; }; C450C8C728C919EC002A2B4B /* PreferenceName.swift in Sources */ = {isa = PBXBuildFile; fileRef = C450C8C528C919EC002A2B4B /* PreferenceName.swift */; }; + C4570C3A28FC355300D18420 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = C473319E2470923A009A0597 /* Localizable.strings */; }; + C4570C3B28FC355300D18420 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = C473319E2470923A009A0597 /* Localizable.strings */; }; + C4570C3C28FC355400D18420 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = C473319E2470923A009A0597 /* Localizable.strings */; }; C459B4BD27F6093700E9B4B4 /* nginx-proxy.test in Resources */ = {isa = PBXBuildFile; fileRef = C459B4BC27F6093700E9B4B4 /* nginx-proxy.test */; }; C45E76142854A65300B4FE0C /* ServicesManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = C45E76132854A65300B4FE0C /* ServicesManager.swift */; }; C45E76152854A65300B4FE0C /* ServicesManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = C45E76132854A65300B4FE0C /* ServicesManager.swift */; }; @@ -1813,6 +1816,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + C4570C3B28FC355300D18420 /* Localizable.strings in Resources */, C4E2E85528FC256B003B070C /* brew-services-sudo.json in Resources */, C4E2E85928FC256B003B070C /* brew-services-normal.json in Resources */, C4E2E84F28FC22E4003B070C /* brew-formula.json in Resources */, @@ -1824,6 +1828,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + C4570C3A28FC355300D18420 /* Localizable.strings in Resources */, C4E2E85628FC256B003B070C /* brew-services-sudo.json in Resources */, C4E2E85A28FC256B003B070C /* brew-services-normal.json in Resources */, C4E2E85028FC22E4003B070C /* brew-formula.json in Resources */, @@ -1835,6 +1840,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + C4570C3C28FC355400D18420 /* Localizable.strings in Resources */, 54FCFD27276C883F004CE748 /* SelectPreferenceView.xib in Resources */, 54FCFD2E276C8D67004CE748 /* HotkeyPreferenceView.xib in Resources */, C42CFB1827DFDFDC00862737 /* nginx-site-isolated.test in Resources */, diff --git a/phpmon/Common/Extensions/DataExtension.swift b/phpmon/Common/Extensions/DataExtension.swift index 1608b93..ce241b7 100644 --- a/phpmon/Common/Extensions/DataExtension.swift +++ b/phpmon/Common/Extensions/DataExtension.swift @@ -12,7 +12,9 @@ extension Data { var prettyPrintedJSONString: NSString? { guard let object = try? JSONSerialization.jsonObject(with: self, options: []), let data = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted]), - let prettyPrintedString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) else { return nil } + let prettyPrintedString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) else { + return nil + } return prettyPrintedString } diff --git a/phpmon/Common/Extensions/StringExtension.swift b/phpmon/Common/Extensions/StringExtension.swift index 4c24f3d..5af4bf2 100644 --- a/phpmon/Common/Extensions/StringExtension.swift +++ b/phpmon/Common/Extensions/StringExtension.swift @@ -7,15 +7,26 @@ import Foundation import SwiftUI +struct Localization { + static var bundle: Bundle = { + var bundle = Bundle.main + if isRunningTests { + bundle = Bundle(identifier: "com.nicoverbruggen.phpmon.dev") + ?? Bundle(identifier: "com.nicoverbruggen.phpmon")! + } + return bundle + }() +} + extension String { var localized: String { if #available(macOS 13, *) { return NSLocalizedString( - self, tableName: nil, bundle: Bundle.main, value: "", comment: "" + self, tableName: nil, bundle: Localization.bundle, value: "", comment: "" ).replacingOccurrences(of: "Preferences", with: "Settings") } - return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "") + return NSLocalizedString(self, tableName: nil, bundle: Localization.bundle, value: "", comment: "") } var localizedForSwiftUI: LocalizedStringKey { @@ -49,7 +60,7 @@ extension String { static func random(_ length: Int) -> String { let characters = "0123456789abcdefghijklmnopqrstuvwxyz" - return String((0..) -> String { diff --git a/phpmon/Domain/App/App.swift b/phpmon/Domain/App/App.swift index 83dd659..0b7da2a 100644 --- a/phpmon/Domain/App/App.swift +++ b/phpmon/Domain/App/App.swift @@ -31,12 +31,10 @@ class App { return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String } - /** - A fake architecture. - When set, the real machine's system architecture is not used, - but this fixed value is used instead. - */ - static var fakeArchitecture: String? + /** Just the bundle name. */ + static var identifier: String { + Bundle.main.infoDictionary?["CFBundleIdentifier"] as! String + } /** The system architecture. Paths differ based on this value. */ static var architecture: String { @@ -55,6 +53,13 @@ class App { return machine } + /** + A fake architecture. + When set, the real machine's system architecture is not used, + but this fixed value is used instead. + */ + static var fakeArchitecture: String? + // MARK: Variables /** Technical information about the current environment. */ diff --git a/phpmon/Domain/App/ServicesManager.swift b/phpmon/Domain/App/ServicesManager.swift index 00d4fd0..ab5697c 100644 --- a/phpmon/Domain/App/ServicesManager.swift +++ b/phpmon/Domain/App/ServicesManager.swift @@ -51,9 +51,11 @@ class ServicesManager: ObservableObject { .decode([HomebrewService].self, from: rootJson) .filter({ return userServiceNames.contains($0.name) }) - ServicesManager.shared.userServices = Dictionary( - uniqueKeysWithValues: rootServices.map { ($0.name, $0) } - ) + DispatchQueue.main.async { + ServicesManager.shared.userServices = Dictionary( + uniqueKeysWithValues: rootServices.map { ($0.name, $0) } + ) + } } /** diff --git a/phpmon/Localizable.strings b/phpmon/Localizable.strings index 501356b..f10113f 100644 --- a/phpmon/Localizable.strings +++ b/phpmon/Localizable.strings @@ -83,6 +83,12 @@ "mi_xdebug_actions" = "Actions"; "mi_xdebug_disable_all" = "Disable All Modes"; +// GENERIC + +"generic.ok" = "OK"; +"generic.retry" = "Retry"; +"generic.notice" = "Notice"; + // PRESET LOADING "preset_help_title" = "Working with Configuration Presets"; diff --git a/tests/ui/StartupTest.swift b/tests/ui/StartupTest.swift index 0dc4c01..bdeea0c 100644 --- a/tests/ui/StartupTest.swift +++ b/tests/ui/StartupTest.swift @@ -16,13 +16,6 @@ final class StartupTest: UITestCase { override func tearDownWithError() throws {} - func testApplicationCanLaunchWithTestConfigurationAndIdles() throws { - let app = XCPMApplication() - app.withConfiguration(TestableConfigurations.working) - app.launch() - sleep(5) - } - func testApplicationCanLaunchWithTestConfigurationAndThrowsAlert() throws { var configuration = TestableConfigurations.working configuration.filesystem["/opt/homebrew/bin/php"] = nil // PHP binary must be missing @@ -33,25 +26,52 @@ final class StartupTest: UITestCase { // Dialog 1: "PHP is not correctly installed" assertAllExist([ - app.dialogs["Notice"], - app.staticTexts["PHP is not correctly installed"], - app.buttons["OK"], + app.dialogs["generic.notice".localized], + app.staticTexts["startup.errors.php_binary.title".localized], + app.buttons["generic.ok".localized], ]) - click(app.buttons["OK"]) + click(app.buttons["generic.ok".localized]) // Dialog 2: PHP Monitor failed to start assertAllExist([ - app.dialogs["Notice"], - app.staticTexts["PHP Monitor cannot start due to a problem with your system configuration"], - app.buttons["Retry"], - app.buttons["Quit"] + app.dialogs["generic.notice".localized], + app.staticTexts["alert.cannot_start.title".localized], + app.buttons["alert.cannot_start.retry".localized], + app.buttons["alert.cannot_start.close".localized] ]) - click(app.buttons["Retry"]) + click(app.buttons["alert.cannot_start.retry".localized]) // Dialog 1 again - assertExists(app.staticTexts["PHP is not correctly installed"]) + assertExists(app.staticTexts["startup.errors.php_binary.title".localized]) // At this point, we can terminate the test app.terminate() } + + func testApplicationCanWarnAboutPhpFpmIssue() throws { + var configuration = TestableConfigurations.working + configuration.filesystem["/opt/homebrew/etc/php/8.1/php-fpm.d/valet-fpm.conf"] = nil + + let app = XCPMApplication() + app.withConfiguration(configuration) + app.launch() + + assertExists(app.staticTexts["alert.php_fpm_broken.title".localized], 3.0) + click(app.buttons["generic.ok".localized]) + } + + func testApplicationCanLaunchWithTestConfigurationAndCanClickMenuItem() throws { + let app = XCPMApplication() + app.withConfiguration(TestableConfigurations.working) + app.launch() + + // Note: If this fails here, make sure the menu bar item can be displayed + // If you use Bartender or something like this, this item may be hidden and tests will fail + let statusBarItem = app.statusItems.firstMatch + statusBarItem.click() + assertAllExist([ + app.menuItems["mi_about".localized], + app.menuItems["mi_quit".localized] + ]) + } }