From d0faa8f8de157e162bfc42a232d7b242b39155db Mon Sep 17 00:00:00 2001 From: Frank Verhoeven Date: Mon, 8 Jun 2020 16:34:40 +0200 Subject: [PATCH] Allow Symfony ^4.4 --- .gitignore | 4 ++ .travis.yml | 12 +++--- composer.json | 12 ++++-- phpcs.xml.dist | 16 ++++++++ phpunit.xml.dist | 2 +- psalm.xml | 15 +++++++ src/StashStore.php | 60 +++++++++------------------- tests/StashStoreTest.php | 84 ++++++++++++++++++++-------------------- 8 files changed, 111 insertions(+), 94 deletions(-) create mode 100644 phpcs.xml.dist create mode 100644 psalm.xml diff --git a/.gitignore b/.gitignore index 3a9875b..7e09a19 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ /vendor/ composer.lock +.DS_Store +.idea +.phpcs-cache +.phpunit.result.cache diff --git a/.travis.yml b/.travis.yml index b701942..3071f90 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,10 @@ language: php + php: - - '7.0' - - '7.1' - - '7.2' + - '7.2' + - '7.3' + - '7.4' + before_script: - - composer self-update - - composer install --prefer-source + - composer self-update + - composer install --prefer-source diff --git a/composer.json b/composer.json index 6010043..759ac5d 100644 --- a/composer.json +++ b/composer.json @@ -3,12 +3,14 @@ "description": "Stash storage implementation for the Symfony Lock component", "type": "library", "require": { - "php": "^7.0", - "symfony/lock": "^3.4", + "php": "^7.2", + "symfony/lock": "^3.4 || ^4.4", "tedivm/stash": "^0.15" }, "require-dev": { - "phpunit/phpunit": "~4.8" + "phpunit/phpunit": "^8.5", + "myonlinestore/coding-standard": "^2.0", + "vimeo/psalm": "^3.11" }, "license": "MIT", "authors": [ @@ -19,7 +21,9 @@ ], "minimum-stability": "stable", "autoload": { - "psr-4": { "Sandwich\\Symfony\\Lock\\Store\\": "src" } + "psr-4": { + "Sandwich\\Symfony\\Lock\\Store\\": "src" + } }, "autoload-dev": { "psr-4": { diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 0000000..d773b99 --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,16 @@ + + + + + + + + + + src + tests + + + diff --git a/phpunit.xml.dist b/phpunit.xml.dist index c1112ce..0ca7b06 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,7 +8,7 @@ convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" - syntaxCheck="false"> +> ./tests/ diff --git a/psalm.xml b/psalm.xml new file mode 100644 index 0000000..4e9226b --- /dev/null +++ b/psalm.xml @@ -0,0 +1,15 @@ + + + + + + + + + diff --git a/src/StashStore.php b/src/StashStore.php index 8dc0837..9b11881 100644 --- a/src/StashStore.php +++ b/src/StashStore.php @@ -1,4 +1,5 @@ initialTtl = $initialTtl; } - /** - * @inheritdoc - */ - public function save(Key $key) + public function save(Key $key): void { $item = $this->pool->getItem((string) $key); @@ -55,24 +46,21 @@ public function save(Key $key) $this->putOffExpiration($key, $this->initialTtl); } - /** - * @inheritdoc - */ - public function waitAndSave(Key $key) + public function waitAndSave(Key $key): void { throw new InvalidArgumentException( - sprintf('The store "%s" does not supports blocking locks.', get_class($this)) + \sprintf('The store "%s" does not supports blocking locks.', static::class) ); } /** * @inheritdoc */ - public function putOffExpiration(Key $key, $ttl) + public function putOffExpiration(Key $key, $ttl): void { if ($ttl < 1) { throw new InvalidArgumentException( - sprintf('%s() expects a TTL greater or equals to 1. Got %s.', __METHOD__, $ttl) + \sprintf('%s() expects a TTL greater or equals to 1. Got %s.', __METHOD__, $ttl) ); } @@ -84,17 +72,14 @@ public function putOffExpiration(Key $key, $ttl) } $item->set($token); - $item->setTTL((int) ceil($ttl)); + $item->setTTL((int) \ceil($ttl)); if (!$item->save()) { throw new LockConflictedException(); } } - /** - * @inheritdoc - */ - public function delete(Key $key) + public function delete(Key $key): void { $item = $this->pool->getItem((string) $key); @@ -110,10 +95,7 @@ public function delete(Key $key) $this->pool->deleteItem((string) $key); } - /** - * @inheritdoc - */ - public function exists(Key $key) + public function exists(Key $key): bool { $item = $this->pool->getItem((string) $key); @@ -122,18 +104,14 @@ public function exists(Key $key) /** * Retrieve an unique token for the given key. - * - * @param Key $key - * - * @return string */ - private function getToken(Key $key) + private function getToken(Key $key): string { - if (!$key->hasState(__CLASS__)) { - $token = base64_encode(random_bytes(32)); - $key->setState(__CLASS__, $token); + if (!$key->hasState(self::class)) { + $token = \base64_encode(\random_bytes(32)); + $key->setState(self::class, $token); } - return $key->getState(__CLASS__); + return $key->getState(self::class); } } diff --git a/tests/StashStoreTest.php b/tests/StashStoreTest.php index 2562b50..c351967 100644 --- a/tests/StashStoreTest.php +++ b/tests/StashStoreTest.php @@ -1,7 +1,9 @@ pool = $this->getMock(PoolInterface::class); + $this->pool = $this->createMock(PoolInterface::class); $this->stashStore = new StashStore($this->pool); } - public function testConstructWillThrowAnExceptionForInvalidTtl() + public function testConstructWillThrowAnExceptionForInvalidTtl(): void { - $this->setExpectedException(InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); new StashStore($this->pool, -1); } - public function testSaveWillPutOffExpirationIfItemExists() + public function testSaveWillPutOffExpirationIfItemExists(): void { $key = new Key('foo'); $key->setState(StashStore::class, 'some-token'); - $item = $this->getMock(ItemInterface::class); + $item = $this->createMock(ItemInterface::class); $item->expects(self::exactly(2))->method('isMiss')->willReturn(false); $item->expects(self::once())->method('get')->willReturn('another-token'); $item->expects(self::never())->method('set'); @@ -46,16 +44,16 @@ public function testSaveWillPutOffExpirationIfItemExists() $this->pool->expects(self::exactly(2))->method('getItem')->with('foo')->willReturn($item); - $this->setExpectedException(LockConflictedException::class); + $this->expectException(LockConflictedException::class); $this->stashStore->save($key); } - public function testSaveWillPutOffExpirationIfSaveFails() + public function testSaveWillPutOffExpirationIfSaveFails(): void { $key = new Key('foo'); $key->setState(StashStore::class, 'some-token'); - $item = $this->getMock(ItemInterface::class); + $item = $this->createMock(ItemInterface::class); $item->expects(self::exactly(2))->method('isMiss')->willReturn(true); $item->expects(self::exactly(2))->method('set')->with('some-token'); $item->expects(self::once())->method('setTTL')->with(300); @@ -63,16 +61,16 @@ public function testSaveWillPutOffExpirationIfSaveFails() $this->pool->expects(self::exactly(2))->method('getItem')->with('foo')->willReturn($item); - $this->setExpectedException(LockConflictedException::class); + $this->expectException(LockConflictedException::class); $this->stashStore->save($key); } - public function testSaveWillReturnUponSuccess() + public function testSaveWillReturnUponSuccess(): void { $key = new Key('foo'); $key->setState(StashStore::class, 'some-token'); - $item = $this->getMock(ItemInterface::class); + $item = $this->createMock(ItemInterface::class); $item->expects(self::once())->method('isMiss')->willReturn(true); $item->expects(self::once())->method('set')->with('some-token'); $item->expects(self::once())->method('save')->willReturn(true); @@ -82,24 +80,24 @@ public function testSaveWillReturnUponSuccess() $this->stashStore->save($key); } - public function testWaitAndSaveWillThrowException() + public function testWaitAndSaveWillThrowException(): void { - $this->setExpectedException(InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); $this->stashStore->waitAndSave(new Key('foo')); } - public function testPutOffExpirationWillThrowExceptionForInvalidTtl() + public function testPutOffExpirationWillThrowExceptionForInvalidTtl(): void { - $this->setExpectedException(InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); $this->stashStore->putOffExpiration(new Key('foo'), -1); } - public function testPutOffExpirationWillThrowExceptionIfItemExistsWithOtherToken() + public function testPutOffExpirationWillThrowExceptionIfItemExistsWithOtherToken(): void { $key = new Key('foo'); $key->setState(StashStore::class, 'some-token'); - $item = $this->getMock(ItemInterface::class); + $item = $this->createMock(ItemInterface::class); $item->expects(self::once())->method('isMiss')->willReturn(false); $item->expects(self::once())->method('get')->willReturn('another-token'); $item->expects(self::never())->method('set'); @@ -107,16 +105,16 @@ public function testPutOffExpirationWillThrowExceptionIfItemExistsWithOtherToken $this->pool->expects(self::once())->method('getItem')->with('foo')->willReturn($item); - $this->setExpectedException(LockConflictedException::class); + $this->expectException(LockConflictedException::class); $this->stashStore->putOffExpiration($key, 10); } - public function testPutOffExpirationWillThrowExceptionIfItemSaveFails() + public function testPutOffExpirationWillThrowExceptionIfItemSaveFails(): void { $key = new Key('foo'); $key->setState(StashStore::class, 'some-token'); - $item = $this->getMock(ItemInterface::class); + $item = $this->createMock(ItemInterface::class); $item->expects(self::once())->method('isMiss')->willReturn(false); $item->expects(self::once())->method('get')->willReturn('some-token'); $item->expects(self::once())->method('set')->with('some-token'); @@ -125,16 +123,16 @@ public function testPutOffExpirationWillThrowExceptionIfItemSaveFails() $this->pool->expects(self::once())->method('getItem')->with('foo')->willReturn($item); - $this->setExpectedException(LockConflictedException::class); + $this->expectException(LockConflictedException::class); $this->stashStore->putOffExpiration($key, 10); } - public function testPutOffExpirationWillExtendItem() + public function testPutOffExpirationWillExtendItem(): void { $key = new Key('foo'); $key->setState(StashStore::class, 'some-token'); - $item = $this->getMock(ItemInterface::class); + $item = $this->createMock(ItemInterface::class); $item->expects(self::once())->method('isMiss')->willReturn(false); $item->expects(self::once())->method('get')->willReturn('some-token'); $item->expects(self::once())->method('set')->with('some-token'); @@ -146,12 +144,12 @@ public function testPutOffExpirationWillExtendItem() $this->stashStore->putOffExpiration($key, 10); } - public function testDeleteWillDoNothingIfItemDoesNotExists() + public function testDeleteWillDoNothingIfItemDoesNotExists(): void { $key = new Key('foo'); $key->setState(StashStore::class, 'some-token'); - $item = $this->getMock(ItemInterface::class); + $item = $this->createMock(ItemInterface::class); $item->expects(self::once())->method('isMiss')->willReturn(true); $this->pool->expects(self::once())->method('getItem')->with('foo')->willReturn($item); @@ -160,12 +158,12 @@ public function testDeleteWillDoNothingIfItemDoesNotExists() $this->stashStore->delete($key); } - public function testDeleteWillDoNothingIfItemIsNotOwnedByKey() + public function testDeleteWillDoNothingIfItemIsNotOwnedByKey(): void { $key = new Key('foo'); $key->setState(StashStore::class, 'some-token'); - $item = $this->getMock(ItemInterface::class); + $item = $this->createMock(ItemInterface::class); $item->expects(self::once())->method('isMiss')->willReturn(false); $item->expects(self::once())->method('get')->willReturn('another-token'); @@ -175,12 +173,12 @@ public function testDeleteWillDoNothingIfItemIsNotOwnedByKey() $this->stashStore->delete($key); } - public function testDeleteWillDeleteItem() + public function testDeleteWillDeleteItem(): void { $key = new Key('foo'); $key->setState(StashStore::class, 'some-token'); - $item = $this->getMock(ItemInterface::class); + $item = $this->createMock(ItemInterface::class); $item->expects(self::once())->method('isMiss')->willReturn(false); $item->expects(self::once())->method('get')->willReturn('some-token'); @@ -190,9 +188,9 @@ public function testDeleteWillDeleteItem() $this->stashStore->delete($key); } - public function testExistsWillReturnFalseIfItemDoesNotExist() + public function testExistsWillReturnFalseIfItemDoesNotExist(): void { - $item = $this->getMock(ItemInterface::class); + $item = $this->createMock(ItemInterface::class); $item->expects(self::once())->method('isMiss')->willReturn(true); $this->pool->expects(self::once())->method('getItem')->with('foo')->willReturn($item); @@ -200,12 +198,12 @@ public function testExistsWillReturnFalseIfItemDoesNotExist() self::assertFalse($this->stashStore->exists(new Key('foo'))); } - public function testExistsWillReturnFalseIfItemExistsButIsNotOwnedByKey() + public function testExistsWillReturnFalseIfItemExistsButIsNotOwnedByKey(): void { $key = new Key('foo'); $key->setState(StashStore::class, 'some-token'); - $item = $this->getMock(ItemInterface::class); + $item = $this->createMock(ItemInterface::class); $item->expects(self::once())->method('isMiss')->willReturn(false); $item->expects(self::once())->method('get')->willReturn('another-token'); @@ -214,12 +212,12 @@ public function testExistsWillReturnFalseIfItemExistsButIsNotOwnedByKey() self::assertFalse($this->stashStore->exists($key)); } - public function testExistsWillReturnTrueIfItemExistsAndIsOwnedByKey() + public function testExistsWillReturnTrueIfItemExistsAndIsOwnedByKey(): void { $key = new Key('foo'); $key->setState(StashStore::class, 'some-token'); - $item = $this->getMock(ItemInterface::class); + $item = $this->createMock(ItemInterface::class); $item->expects(self::once())->method('isMiss')->willReturn(false); $item->expects(self::once())->method('get')->willReturn('some-token');