mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2026-04-03 09:50:10 +02:00
♻️ Add minimal Container boot
This commit is contained in:
@@ -7,9 +7,9 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
extension Container {
|
extension Container {
|
||||||
public static func real() -> Container {
|
public static func real(minimal: Bool = false) -> Container {
|
||||||
let container = Container()
|
let container = Container()
|
||||||
container.bind()
|
container.bind(coreOnly: minimal)
|
||||||
return container
|
return container
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,11 +51,18 @@ class Container {
|
|||||||
/// (Swapping instances for specific dependencies can be introduced later with dedicated
|
/// (Swapping instances for specific dependencies can be introduced later with dedicated
|
||||||
/// methods if it ever becomes truly necessary.)
|
/// 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 {
|
if self.bound {
|
||||||
fatalError("You cannot call `bind` on a Container more than once.")
|
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
|
// These are the most basic building blocks. We need these before
|
||||||
// any of the other classes can be initialized!
|
// any of the other classes can be initialized!
|
||||||
self.shell = RealShell(container: self)
|
self.shell = RealShell(container: self)
|
||||||
@@ -64,6 +71,10 @@ class Container {
|
|||||||
self.paths = Paths(container: self)
|
self.paths = Paths(container: self)
|
||||||
self.webApi = RealWebApi(container: self)
|
self.webApi = RealWebApi(container: self)
|
||||||
|
|
||||||
|
if coreOnly {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Please note that the order in which these are initialized, matters!
|
// Please note that the order in which these are initialized, matters!
|
||||||
// For example, preferences leverages the Paths instance, so don't just
|
// For example, preferences leverages the Paths instance, so don't just
|
||||||
// swap these around for no reason... the order is very intentional.
|
// swap these around for no reason... the order is very intentional.
|
||||||
@@ -71,9 +82,6 @@ class Container {
|
|||||||
self.phpEnvs = PhpEnvironments(container: self)
|
self.phpEnvs = PhpEnvironments(container: self)
|
||||||
self.favorites = Favorites()
|
self.favorites = Favorites()
|
||||||
self.warningManager = WarningManager(container: self)
|
self.warningManager = WarningManager(container: self)
|
||||||
|
|
||||||
// At this point, our container has been bound.
|
|
||||||
self.bound = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import Testing
|
|||||||
|
|
||||||
struct CommandTest {
|
struct CommandTest {
|
||||||
@Test func determinePhpVersion() {
|
@Test func determinePhpVersion() {
|
||||||
let container = Container.real()
|
let container = Container.real(minimal: true)
|
||||||
|
|
||||||
let version = container.command.execute(
|
let version = container.command.execute(
|
||||||
path: container.paths.php,
|
path: container.paths.php,
|
||||||
|
|||||||
@@ -9,12 +9,11 @@
|
|||||||
import Testing
|
import Testing
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
@Suite(.serialized)
|
|
||||||
struct CaskFileParserTest {
|
struct CaskFileParserTest {
|
||||||
var container: Container
|
var container: Container
|
||||||
|
|
||||||
init() async throws {
|
init() async throws {
|
||||||
container = Container.real()
|
container = Container.real(minimal: true)
|
||||||
}
|
}
|
||||||
|
|
||||||
var Shell: ShellProtocol {
|
var Shell: ShellProtocol {
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ struct HomebrewPackageTest {
|
|||||||
/// or the JSON API of the Homebrew output may have changed.
|
/// or the JSON API of the Homebrew output may have changed.
|
||||||
@Test(.disabled("Uses system command; enable at your own risk"))
|
@Test(.disabled("Uses system command; enable at your own risk"))
|
||||||
func can_parse_services_json_from_cli_output() async throws {
|
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(
|
let services = try! JSONDecoder().decode(
|
||||||
[HomebrewService].self,
|
[HomebrewService].self,
|
||||||
@@ -76,7 +76,7 @@ struct HomebrewPackageTest {
|
|||||||
/// or the JSON API of the Homebrew output may have changed.
|
/// or the JSON API of the Homebrew output may have changed.
|
||||||
@Test(.disabled("Uses system command; enable at your own risk"))
|
@Test(.disabled("Uses system command; enable at your own risk"))
|
||||||
func can_load_extension_json_from_cli_output() async throws {
|
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(
|
let package = try! JSONDecoder().decode(
|
||||||
[HomebrewPackage].self,
|
[HomebrewPackage].self,
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ struct NginxConfigurationTest {
|
|||||||
var container: Container
|
var container: Container
|
||||||
|
|
||||||
init () async throws {
|
init () async throws {
|
||||||
container = Container.real()
|
container = Container.real(minimal: true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Test Files
|
// MARK: - Test Files
|
||||||
|
|||||||
@@ -9,12 +9,11 @@
|
|||||||
import Testing
|
import Testing
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
@Suite(.serialized)
|
|
||||||
class PhpConfigurationFileTest {
|
class PhpConfigurationFileTest {
|
||||||
var container: Container
|
var container: Container
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
self.container = Container.real()
|
self.container = Container.real(minimal: true)
|
||||||
}
|
}
|
||||||
|
|
||||||
static var phpIniFileUrl: URL {
|
static var phpIniFileUrl: URL {
|
||||||
|
|||||||
@@ -9,25 +9,22 @@
|
|||||||
import Testing
|
import Testing
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
@Suite(.serialized)
|
|
||||||
struct PhpExtensionTest {
|
struct PhpExtensionTest {
|
||||||
var container: Container
|
|
||||||
|
|
||||||
init () async throws {
|
|
||||||
container = Container.real()
|
|
||||||
}
|
|
||||||
|
|
||||||
static var phpIniFileUrl: URL {
|
static var phpIniFileUrl: URL {
|
||||||
TestBundle.url(forResource: "php", withExtension: "ini")!
|
TestBundle.url(forResource: "php", withExtension: "ini")!
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test func can_load_extension() throws {
|
@Test func can_load_extension() throws {
|
||||||
|
let container = Container.real(minimal: true)
|
||||||
|
|
||||||
let extensions = PhpExtension.from(container, filePath: Self.phpIniFileUrl.path)
|
let extensions = PhpExtension.from(container, filePath: Self.phpIniFileUrl.path)
|
||||||
|
|
||||||
#expect(!extensions.isEmpty)
|
#expect(!extensions.isEmpty)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test func extension_name_is_correct() throws {
|
@Test func extension_name_is_correct() throws {
|
||||||
|
let container = Container.real(minimal: true)
|
||||||
|
|
||||||
let extensions = PhpExtension.from(container, filePath: Self.phpIniFileUrl.path)
|
let extensions = PhpExtension.from(container, filePath: Self.phpIniFileUrl.path)
|
||||||
|
|
||||||
let extensionNames = extensions.map { (ext) -> String in
|
let extensionNames = extensions.map { (ext) -> String in
|
||||||
@@ -47,6 +44,8 @@ struct PhpExtensionTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test func extension_status_is_correct() throws {
|
@Test func extension_status_is_correct() throws {
|
||||||
|
let container = Container.real(minimal: true)
|
||||||
|
|
||||||
let extensions = PhpExtension.from(container, filePath: Self.phpIniFileUrl.path)
|
let extensions = PhpExtension.from(container, filePath: Self.phpIniFileUrl.path)
|
||||||
|
|
||||||
// xdebug should be enabled
|
// xdebug should be enabled
|
||||||
@@ -57,6 +56,7 @@ struct PhpExtensionTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test func toggle_works_as_expected() async throws {
|
@Test func toggle_works_as_expected() async throws {
|
||||||
|
let container = Container.real(minimal: true)
|
||||||
let destination = Utility.copyToTemporaryFile(resourceName: "php", fileExtension: "ini")!
|
let destination = Utility.copyToTemporaryFile(resourceName: "php", fileExtension: "ini")!
|
||||||
let extensions = PhpExtension.from(container, filePath: destination.path)
|
let extensions = PhpExtension.from(container, filePath: destination.path)
|
||||||
#expect(extensions.count == 6)
|
#expect(extensions.count == 6)
|
||||||
|
|||||||
@@ -9,20 +9,17 @@
|
|||||||
import Testing
|
import Testing
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
@Suite(.serialized) // serialized due to how unique temp directory works
|
|
||||||
struct RealFileSystemTest {
|
struct RealFileSystemTest {
|
||||||
var filesystem: FileSystemProtocol
|
var filesystem: FileSystemProtocol
|
||||||
|
|
||||||
init() throws {
|
init() throws {
|
||||||
let container = Container()
|
let container = Container.real(minimal: true)
|
||||||
container.bind()
|
|
||||||
|
|
||||||
filesystem = container.filesystem
|
filesystem = container.filesystem
|
||||||
}
|
}
|
||||||
|
|
||||||
private func createUniqueTemporaryDirectory() -> String {
|
private func createUniqueTemporaryDirectory() -> String {
|
||||||
let tempDirectoryURL = NSURL.fileURL(withPath: NSTemporaryDirectory(), isDirectory: true)
|
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.removeItem(atPath: fullTempDirectoryPath)
|
||||||
try! FileManager.default.createDirectory(atPath: fullTempDirectoryPath, withIntermediateDirectories: false)
|
try! FileManager.default.createDirectory(atPath: fullTempDirectoryPath, withIntermediateDirectories: false)
|
||||||
return fullTempDirectoryPath
|
return fullTempDirectoryPath
|
||||||
|
|||||||
@@ -9,13 +9,12 @@
|
|||||||
import Testing
|
import Testing
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
@Suite(.serialized)
|
|
||||||
struct RealShellTest {
|
struct RealShellTest {
|
||||||
var container: Container
|
var container: Container
|
||||||
|
|
||||||
init() async throws {
|
init() async throws {
|
||||||
// Reset to the default shell
|
// Reset to the default shell
|
||||||
container = Container.real()
|
container = Container.real(minimal: true)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test func system_shell_is_default() async {
|
@Test func system_shell_is_default() async {
|
||||||
|
|||||||
@@ -9,7 +9,6 @@
|
|||||||
import Testing
|
import Testing
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
@Suite(.serialized)
|
|
||||||
struct TestableShellTest {
|
struct TestableShellTest {
|
||||||
@Test func fake_shell_output_can_be_declared() async {
|
@Test func fake_shell_output_can_be_declared() async {
|
||||||
let greeting = BatchFakeShellOutput(items: [
|
let greeting = BatchFakeShellOutput(items: [
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import Foundation
|
|||||||
|
|
||||||
struct TestableConfigurationTest {
|
struct TestableConfigurationTest {
|
||||||
@Test func configuration_can_be_saved_as_json() async {
|
@Test func configuration_can_be_saved_as_json() async {
|
||||||
let container = Container.real()
|
let container = Container.real(minimal: true)
|
||||||
|
|
||||||
// WORKING
|
// WORKING
|
||||||
var configuration = TestableConfigurations.working
|
var configuration = TestableConfigurations.working
|
||||||
|
|||||||
@@ -13,8 +13,7 @@ struct RealWebApiTest {
|
|||||||
private var container: Container
|
private var container: Container
|
||||||
|
|
||||||
init() throws {
|
init() throws {
|
||||||
self.container = Container()
|
self.container = Container.real(minimal: true)
|
||||||
container.bind()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var WebApi: RealWebApi {
|
var WebApi: RealWebApi {
|
||||||
|
|||||||
@@ -9,7 +9,6 @@
|
|||||||
import Testing
|
import Testing
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
@Suite(.serialized)
|
|
||||||
struct FSNotifierTest {
|
struct FSNotifierTest {
|
||||||
|
|
||||||
@Test func notifier_fires_when_file_is_modified() async throws {
|
@Test func notifier_fires_when_file_is_modified() async throws {
|
||||||
|
|||||||
Reference in New Issue
Block a user