1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-12-21 03:10:06 +01:00

♻️ Add minimal Container boot

This commit is contained in:
2025-12-02 15:20:58 +01:00
parent c445c59ed8
commit 564e47f66e
14 changed files with 32 additions and 33 deletions

View File

@@ -7,9 +7,9 @@
//
extension Container {
public static func real() -> Container {
public static func real(minimal: Bool = false) -> Container {
let container = Container()
container.bind()
container.bind(coreOnly: minimal)
return container
}
}

View File

@@ -51,11 +51,18 @@ class Container {
/// (Swapping instances for specific dependencies can be introduced later with dedicated
/// methods if it ever becomes truly necessary.)
///
public func bind() {
/// - Parameter coreOnly: Only binds `shell`, `filesystem`, `command`, `paths` and `webApi`.
/// Use this to prevent slowing down tests for a minimal container.
///
public func bind(coreOnly: Bool = false) {
if self.bound {
fatalError("You cannot call `bind` on a Container more than once.")
}
defer {
self.bound = true
}
// These are the most basic building blocks. We need these before
// any of the other classes can be initialized!
self.shell = RealShell(container: self)
@@ -64,6 +71,10 @@ class Container {
self.paths = Paths(container: self)
self.webApi = RealWebApi(container: self)
if coreOnly {
return
}
// Please note that the order in which these are initialized, matters!
// For example, preferences leverages the Paths instance, so don't just
// swap these around for no reason... the order is very intentional.
@@ -71,9 +82,6 @@ class Container {
self.phpEnvs = PhpEnvironments(container: self)
self.favorites = Favorites()
self.warningManager = WarningManager(container: self)
// At this point, our container has been bound.
self.bound = true
}
/**

View File

@@ -10,7 +10,7 @@ import Testing
struct CommandTest {
@Test func determinePhpVersion() {
let container = Container.real()
let container = Container.real(minimal: true)
let version = container.command.execute(
path: container.paths.php,

View File

@@ -9,12 +9,11 @@
import Testing
import Foundation
@Suite(.serialized)
struct CaskFileParserTest {
var container: Container
init() async throws {
container = Container.real()
container = Container.real(minimal: true)
}
var Shell: ShellProtocol {

View File

@@ -53,7 +53,7 @@ struct HomebrewPackageTest {
/// or the JSON API of the Homebrew output may have changed.
@Test(.disabled("Uses system command; enable at your own risk"))
func can_parse_services_json_from_cli_output() async throws {
let container = Container.real()
let container = Container.real(minimal: true)
let services = try! JSONDecoder().decode(
[HomebrewService].self,
@@ -76,7 +76,7 @@ struct HomebrewPackageTest {
/// or the JSON API of the Homebrew output may have changed.
@Test(.disabled("Uses system command; enable at your own risk"))
func can_load_extension_json_from_cli_output() async throws {
let container = Container.real()
let container = Container.real(minimal: true)
let package = try! JSONDecoder().decode(
[HomebrewPackage].self,

View File

@@ -13,7 +13,7 @@ struct NginxConfigurationTest {
var container: Container
init () async throws {
container = Container.real()
container = Container.real(minimal: true)
}
// MARK: - Test Files

View File

@@ -9,12 +9,11 @@
import Testing
import Foundation
@Suite(.serialized)
class PhpConfigurationFileTest {
var container: Container
init() {
self.container = Container.real()
self.container = Container.real(minimal: true)
}
static var phpIniFileUrl: URL {

View File

@@ -9,25 +9,22 @@
import Testing
import Foundation
@Suite(.serialized)
struct PhpExtensionTest {
var container: Container
init () async throws {
container = Container.real()
}
static var phpIniFileUrl: URL {
TestBundle.url(forResource: "php", withExtension: "ini")!
}
@Test func can_load_extension() throws {
let container = Container.real(minimal: true)
let extensions = PhpExtension.from(container, filePath: Self.phpIniFileUrl.path)
#expect(!extensions.isEmpty)
}
@Test func extension_name_is_correct() throws {
let container = Container.real(minimal: true)
let extensions = PhpExtension.from(container, filePath: Self.phpIniFileUrl.path)
let extensionNames = extensions.map { (ext) -> String in
@@ -47,6 +44,8 @@ struct PhpExtensionTest {
}
@Test func extension_status_is_correct() throws {
let container = Container.real(minimal: true)
let extensions = PhpExtension.from(container, filePath: Self.phpIniFileUrl.path)
// xdebug should be enabled
@@ -57,6 +56,7 @@ struct PhpExtensionTest {
}
@Test func toggle_works_as_expected() async throws {
let container = Container.real(minimal: true)
let destination = Utility.copyToTemporaryFile(resourceName: "php", fileExtension: "ini")!
let extensions = PhpExtension.from(container, filePath: destination.path)
#expect(extensions.count == 6)

View File

@@ -9,20 +9,17 @@
import Testing
import Foundation
@Suite(.serialized) // serialized due to how unique temp directory works
struct RealFileSystemTest {
var filesystem: FileSystemProtocol
init() throws {
let container = Container()
container.bind()
let container = Container.real(minimal: true)
filesystem = container.filesystem
}
private func createUniqueTemporaryDirectory() -> String {
let tempDirectoryURL = NSURL.fileURL(withPath: NSTemporaryDirectory(), isDirectory: true)
let fullTempDirectoryPath = tempDirectoryURL.appendingPathComponent("phpmon-fs-tests").path
let fullTempDirectoryPath = tempDirectoryURL.appendingPathComponent("phpmon-fs-tests-\(UUID().uuidString)").path
try? FileManager.default.removeItem(atPath: fullTempDirectoryPath)
try! FileManager.default.createDirectory(atPath: fullTempDirectoryPath, withIntermediateDirectories: false)
return fullTempDirectoryPath

View File

@@ -9,13 +9,12 @@
import Testing
import Foundation
@Suite(.serialized)
struct RealShellTest {
var container: Container
init() async throws {
// Reset to the default shell
container = Container.real()
container = Container.real(minimal: true)
}
@Test func system_shell_is_default() async {

View File

@@ -9,7 +9,6 @@
import Testing
import Foundation
@Suite(.serialized)
struct TestableShellTest {
@Test func fake_shell_output_can_be_declared() async {
let greeting = BatchFakeShellOutput(items: [

View File

@@ -11,7 +11,7 @@ import Foundation
struct TestableConfigurationTest {
@Test func configuration_can_be_saved_as_json() async {
let container = Container.real()
let container = Container.real(minimal: true)
// WORKING
var configuration = TestableConfigurations.working

View File

@@ -13,8 +13,7 @@ struct RealWebApiTest {
private var container: Container
init() throws {
self.container = Container()
container.bind()
self.container = Container.real(minimal: true)
}
var WebApi: RealWebApi {

View File

@@ -9,7 +9,6 @@
import Testing
import Foundation
@Suite(.serialized)
struct FSNotifierTest {
@Test func notifier_fires_when_file_is_modified() async throws {