Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Apr 21, 2022
1 parent 4b22a3f commit aab3fc8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
10 changes: 8 additions & 2 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -76,4 +77,9 @@ public function isEnabled(): bool
{
return $this->enabled;
}

public function count(): int
{
return count($this->values);
}
}
42 changes: 28 additions & 14 deletions tests/OnceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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)
{
Expand All @@ -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;

Expand All @@ -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) {
Expand All @@ -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();
Expand All @@ -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()
{
Expand All @@ -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)
{
Expand All @@ -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()
{
Expand All @@ -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()
{
Expand All @@ -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()
{
Expand Down Expand Up @@ -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()
{
Expand All @@ -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 () {
Expand All @@ -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()
{
Expand All @@ -255,6 +255,7 @@ public static function getName()
});
}
};

$b = new class() {
public static function getName()
{
Expand All @@ -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);
});
3 changes: 1 addition & 2 deletions tests/TestClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

class TestClass
{
/** @var int */
protected $randomNumber;
protected int $randomNumber;

public function __construct()
{
Expand Down

0 comments on commit aab3fc8

Please sign in to comment.