From aab3fc8694e7caec98b2d330c4a8420391c7559d Mon Sep 17 00:00:00 2001 From: Freek Van der Herten Date: Thu, 21 Apr 2022 14:23:20 +0200 Subject: [PATCH] wip --- src/Cache.php | 10 ++++++++-- tests/OnceTest.php | 42 ++++++++++++++++++++++++++++-------------- tests/TestClass.php | 3 +-- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/Cache.php b/src/Cache.php index 63f1cf1..927ed43 100644 --- a/src/Cache.php +++ b/src/Cache.php @@ -2,13 +2,14 @@ namespace Spatie\Once; +use Countable; use WeakMap; -class Cache +class Cache implements Countable { protected static self $cache; - protected WeakMap $values; + public WeakMap $values; protected bool $enabled = true; @@ -76,4 +77,9 @@ public function isEnabled(): bool { return $this->enabled; } + + public function count(): int + { + return count($this->values); + } } diff --git a/tests/OnceTest.php b/tests/OnceTest.php index 9764bc4..41f1957 100644 --- a/tests/OnceTest.php +++ b/tests/OnceTest.php @@ -10,7 +10,7 @@ $this->cache->flush(); }); -test('It will run the a callback without arguments only once', function () { +it('will run the a callback without arguments only once', function () { $testClass = new class() { public function getNumber() { @@ -30,7 +30,7 @@ public function getNumber() } }); -test('It will run the given callback only once per variation arguments in use', function () { +it('will run the given callback only once per variation arguments in use', function () { $testClass = new class() { public function getNumberForLetter($letter) { @@ -50,7 +50,7 @@ public function getNumberForLetter($letter) } }); -test('It will run the given callback only once for falsy result', function () { +it('will run the given callback only once for falsy result', function () { $testClass = new class() { public $counter = 0; @@ -69,7 +69,7 @@ public function getNull() expect($testClass->counter)->toBe(1); }); -test('It will work properly with unset objects', function () { +it('will work properly with unset objects', function () { $previousNumbers = []; foreach (range(1, 5) as $number) { @@ -85,7 +85,7 @@ public function getNull() } }); -test('It will remember the memoized value when serialized when called in the same request', function () { +it('will remember the memoized value when serialized when called in the same request', function () { $testClass = new TestClass(); $firstNumber = $testClass->getRandomNumber(); @@ -99,7 +99,7 @@ public function getNull() expect($testClass->getRandomNumber())->toBe($firstNumber); }); -test('It will run callback once on static method', function () { +it('will run callback once on static method', function () { $object = new class() { public static function getNumber() { @@ -120,7 +120,7 @@ public static function getNumber() } }); -test('It will run callback once on static method per variation arguments in use', function () { +it('will run callback once on static method per variation arguments in use', function () { $object = new class() { public static function getNumberForLetter($letter) { @@ -141,7 +141,7 @@ public static function getNumberForLetter($letter) } }); -test('It can flush the entire cache', function () { +it('can flush the entire cache', function () { $testClass = new class() { public function getNumber() { @@ -158,7 +158,7 @@ public function getNumber() expect($testClass->getNumber())->not()->toBe($firstResult); }); -test('It can enable and disable the cache', function () { +it('can enable and disable the cache', function () { $testClass = new class() { public function getNumber() { @@ -180,13 +180,13 @@ public function getNumber() expect($testClass->getNumber())->toBe($testClass->getNumber()); }); -test('It will not throw error with eval', function () { +it('will not throw error with eval', function () { $result = eval('return once( function () { return random_int(1, 1000); } ) ;'); expect(in_array($result, range(1, 1000)))->toBeTrue(); }); -test('It will differentiate between closures', function () { +it('will differentiate between closures', function () { $testClass = new class() { public function getNumber() { @@ -214,7 +214,7 @@ public function secondNumber() expect($testClass->secondNumber())->not()->toBe($testClass->getNumber()); }); -test('It will run callback once for closure called on differemt lines', function () { +it('will run callback once for closure called on differemt lines', function () { $testClass = new class() { public function getNumbers() { @@ -235,7 +235,7 @@ public function getNumbers() expect($results[1])->toBe($results[0]); }); -test('It will work in global functions', function () { +it('will work in global functions', function () { function globalFunction() { return once(function () { @@ -246,7 +246,7 @@ function globalFunction() expect(globalFunction())->toBe(globalFunction()); }); -test('It will work with two static functions with the same name', function () { +it('will work with two static functions with the same name', function () { $a = new class() { public static function getName() { @@ -255,6 +255,7 @@ public static function getName() }); } }; + $b = new class() { public static function getName() { @@ -263,9 +264,22 @@ public static function getName() }); } }; + $aClass = get_class($a); $bClass = get_class($b); expect($aClass::getName())->toBe('A'); expect($bClass::getName())->toBe('B'); }); + +it('can count the items in the cache', function() { + expect($this->cache->count())->toBe(0); + + $testClass = (new TestClass()); + $testClass->getRandomNumber(); + expect($this->cache->count())->toBe(1); + + $anotherTestClass = (new TestClass()); + $anotherTestClass->getRandomNumber(); + expect($this->cache->count())->toBe(2); +}); diff --git a/tests/TestClass.php b/tests/TestClass.php index 25d5049..5462226 100644 --- a/tests/TestClass.php +++ b/tests/TestClass.php @@ -4,8 +4,7 @@ class TestClass { - /** @var int */ - protected $randomNumber; + protected int $randomNumber; public function __construct() {