PHPackages                             zeus/mock - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. zeus/mock

ActiveLibrary[Testing &amp; Quality](/categories/testing)

zeus/mock
=========

This library allows you to create mock objects.

v1.0.14(1y ago)314MITPHP

Since Feb 18Pushed 1y ago1 watchersCompare

[ Source](https://github.com/diloabininyeri/php-mock)[ Packagist](https://packagist.org/packages/zeus/mock)[ RSS](/packages/zeus-mock/feed)WikiDiscussions main Synced today

READMEChangelog (10)Dependencies (1)Versions (16)Used By (0)

MockFactory
===========

[](#mockfactory)

The `MockObjectFactory` class allows for the dynamic creation of mock objects for classes or interfaces, which can be useful in unit testing or when you want to simulate the behavior of an object. This is achieved through method mocking, where you can override methods to return predefined results for testing purposes.

Features
--------

[](#features)

- **Mock any class or interface:** Create mock objects for both classes and interfaces dynamically.
- **Method mocking:** Override methods with custom behaviors, defined via closures.
- **Supports class inheritance:** The mock objects will inherit from the original class, allowing you to test behavior as if the real object was used.
- **Supports interface implementation:** If the target is an interface, it will generate a mock implementing the interface.
- **Flexible mock behavior:** Customize method returns and behaviors for specific testing scenarios.

Installation
------------

[](#installation)

```
composer require zeus/mock
```

Example Usage
-------------

[](#example-usage)

```
use Zeus\Mock\MockObjectFactory;

$mockFactory = new MockObjectFactory();

// Create a mock instance of a class
$mockObject = $mockFactory->createMock(SomeClass::class);

// Mock a specific method on the class
$mockFactory->mockMethod('someMethod', fn($arg) => 'mocked result');

// Use the mock object
echo $mockObject->someMethod('test');  // Outputs 'mocked result'
```

**Mocking an Interface**

```
interface Logger {
    public function log(string $message): void;
}

$mockFactory = new MockObjectFactory();

// Create a mock instance of an interface
$mockLogger = $mockFactory->createMock(Logger::class);

// Mock the log method
$mockFactory->mockMethod('log', fn($message) =>print 'mocked log: ' . $message);

// Use the mock interface
echo $mockLogger->log('Test');  // Outputs 'mocked log: Test'
```

**Mocking a Service Class with Dependencies**

```
class DatabaseService {
    private Logger $logger;

    public function __construct(Logger $logger) {
        $this->logger = $logger;
    }

    public function saveData(string $data): void {
        // Simulate saving data and logging the action
        $this->logger->log('Data saved: ' . $data);
    }
}

$mockFactory = new MockObjectFactory();

// Mock the Logger interface
$mockLogger = $mockFactory->createMock(Logger::class);

// Mock the saveData method
$mockFactory->mockMethod('log', fn($message) =>print 'mocked log: ' . $message);

// Create the mock DatabaseService with the mocked Logger
$mockDatabaseService = $mockFactory->createMock(DatabaseService::class,[
   'logger' => $mockLogger
]);

// Use the service with mocked behavior
$mockDatabaseService->saveData('Test Data');  // Will output 'mocked log: Data saved: Test Data'
```

**Type Hinting and instanceof Check**

The MockFactory ensures that type hinting is respected in the mock classes. For example, if a method expects a specific type, the mock will follow that expectation.

```
class SomeService {
    public function processData(array $data): int {
        return count($data);
    }
}

$mockFactory = new MockObjectFactoryMock();

// Create a mock instance of SomeService
$mockService = $mockFactory->createMock(SomeService::class);

// Mock the processData method
$mockFactory->mockMethod('processData', fn($data) => 42);

// Use the mock object
echo $mockService->processData([1, 2, 3]);  // Outputs '42'

// Check the instance type
if ($mockService instanceof SomeService) {
    echo "It's an instance of SomeService!";
}
```

**Benefits of Type Hinting in Mocks**

**Avoid type-related issues**: Ensures the mock correctly implements method signatures, avoiding errors caused by incorrect argument types.

**Seamless integration**: Your tests will integrate with the actual system as expected without concerns for type mismatch. Method Mocking and Custom Behavior

**Method Mocking and Custom Behavior**

You can mock specific methods using closures that define the behavior you need for testing.

```
$mockFactory->mockMethod('methodName', fn($arg1, $arg2) => 'custom result');

// Call the method
echo $mockObject->methodName($arg1, $arg2);  // Outputs 'custom result'
```

Example Class Here is a basic example class that demonstrates how to use the `MockFactory` in different scenarios:

```
