Skip to content

Commit

Permalink
Merge pull request #1 from divengine/updateReadme
Browse files Browse the repository at this point in the history
Update examples in Readme for v1.1.0
  • Loading branch information
rafageist authored Aug 14, 2024
2 parents 0c0bcde + de67dc7 commit be8ba20
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ echo $value; // Outputs the same value as before
This example covers the full range of Laze's capabilities in a concise manner suitable for a README, showing how it can be applied in real-world scenarios.

- Constraints: Ensures that APP_CONFIG implements the Configurable interface.
- Closure Returning Closure: The DEFERRED key holds a closure that returns another closure, delaying the final result until it's needed.
- Closure Returning Closure: MY_FUNCTION key holds a closure that returns another closure
- Lazy Value with Object Instance: APP_CONFIG stores an instance of AppConfig, which is validated by the constraint.
- Reusing define and read: FINAL_MESSAGE reads from both APP_CONFIG and DEFERRED, combining their values.
- Reusing define and read: FINAL_MESSAGE reads from both APP_CONFIG and MY_FUNCTION, combining their values.
- PHPUnit Test: Demonstrates how APP_CONFIG can be redefined for testing, using a mock object.

```php
Expand Down Expand Up @@ -96,9 +96,9 @@ laze::constraint(
);

// 2. Define a lazy value that returns a closure
laze::define('DEFERRED', function() {
laze::define('MY_FUNCTION', function() {
return function() {
return "Deferred Result";
return "Function Result";
};
});

Expand All @@ -116,23 +116,32 @@ laze::define('APP_CONFIG', function() {
laze::define('FINAL_MESSAGE', function() {
$config = laze::read('APP_CONFIG');
$timezone = $config->getSetting('timezone');
return laze::read('DEFERRED')() . " in timezone $timezone";
return laze::read('MY_FUNCTION')() . " in timezone $timezone";
});

$finalMessage = laze::read('FINAL_MESSAGE');
echo $finalMessage; // Outputs: "Deferred Result in timezone UTC"
echo $finalMessage; // Outputs: "Function Result in timezone UTC"

// 5. PHPUnit Test - Redefining a value
class LazeTest extends \PHPUnit\Framework\TestCase {
public function testAppConfigCanBeMocked() {

// mock function
laze::define('MY_FUNCTION', function() {
return function() {
return "Mocked Result";
};
});

// mock object
laze::define('APP_CONFIG', function() {
$mockConfig = $this->createMock(Configurable::class);
$mockConfig->method('getSetting')->willReturn('mocked_timezone');
return $mockConfig;
});

$message = laze::read('FINAL_MESSAGE');
$this->assertEquals("Deferred Result in timezone mocked_timezone", $message);
$this->assertEquals("Mocked Result in timezone mocked_timezone", $message);
}
}

Expand Down

0 comments on commit be8ba20

Please sign in to comment.