mirror of
https://codeberg.org/demostf/api.git
synced 2026-06-03 09:54:17 +02:00
delete static demo when changing demo url
This commit is contained in:
parent
df83a46e20
commit
611e7c3415
5 changed files with 138 additions and 21 deletions
|
|
@ -93,7 +93,7 @@ class Container {
|
|||
$this->baseUrl,
|
||||
new HeaderParser(),
|
||||
new Parser(new RawParser($this->parserUrl)),
|
||||
new DemoStore($this->storeRoot, $this->storeUrl),
|
||||
$this->getDemoStore(),
|
||||
$this->getUserProvider(),
|
||||
$this->getDemoProvider(),
|
||||
new DemoSaver(
|
||||
|
|
@ -106,6 +106,10 @@ class Container {
|
|||
);
|
||||
}
|
||||
|
||||
public function getDemoStore(): DemoStore {
|
||||
return new DemoStore($this->storeRoot, $this->storeUrl);
|
||||
}
|
||||
|
||||
public function getUserProvider(): UserProvider {
|
||||
return new UserProvider($this->connection, $this->generator);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API\Controllers;
|
||||
|
||||
use Demostf\API\Demo\DemoStore;
|
||||
use Demostf\API\Providers\ChatProvider;
|
||||
use Demostf\API\Providers\DemoListProvider;
|
||||
use Demostf\API\Providers\DemoProvider;
|
||||
|
|
@ -21,18 +22,22 @@ class DemoController extends BaseController {
|
|||
|
||||
private $editKey;
|
||||
|
||||
private $store;
|
||||
|
||||
public function __construct(
|
||||
Request $request,
|
||||
Response $response,
|
||||
DemoProvider $demoProvider,
|
||||
ChatProvider $chatProvider,
|
||||
DemoListProvider $demoListProvider,
|
||||
DemoStore $store,
|
||||
string $editKey
|
||||
) {
|
||||
parent::__construct($request, $response);
|
||||
$this->demoProvider = $demoProvider;
|
||||
$this->chatProvider = $chatProvider;
|
||||
$this->demoListProvider = $demoListProvider;
|
||||
$this->store = $store;
|
||||
$this->editKey = $editKey;
|
||||
}
|
||||
|
||||
|
|
@ -113,6 +118,10 @@ class DemoController extends BaseController {
|
|||
$existingHash = $demo->getHash();
|
||||
if ($existingHash === '' || $existingHash === $hash) {
|
||||
$this->demoProvider->setDemoUrl((int) $id, $backend, $url, $path);
|
||||
|
||||
if ($demo->getBackend() === 'static') {
|
||||
$this->store->remove($demo);
|
||||
}
|
||||
} else {
|
||||
throw new \InvalidArgumentException('Invalid demo hash');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,4 +39,10 @@ class DemoStore {
|
|||
private function getUrl(string $name): string {
|
||||
return 'https://' . $this->webRoot . $this->getPrefix($name) . $name;
|
||||
}
|
||||
|
||||
public function remove(Demo $demo) {
|
||||
if (file_exists($demo->getPath())) {
|
||||
unlink($demo->getPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ $demoController = new Controllers\DemoController(
|
|||
$container->getDemoProvider(),
|
||||
$container->getChatProvider(),
|
||||
$container->getDemoListProvider(),
|
||||
$container->getDemoStore(),
|
||||
$container->getEditKey()
|
||||
);
|
||||
$authController = new Controllers\AuthController(
|
||||
|
|
|
|||
|
|
@ -5,11 +5,15 @@ declare(strict_types=1);
|
|||
namespace Demostf\API\Test\Controllers;
|
||||
|
||||
use Demostf\API\Controllers\DemoController;
|
||||
use Demostf\API\Demo\Demo;
|
||||
use Demostf\API\Demo\DemoStore;
|
||||
use Demostf\API\Providers\ChatProvider;
|
||||
use Demostf\API\Providers\DemoListProvider;
|
||||
use Demostf\API\Providers\DemoProvider;
|
||||
|
||||
class DemoControllerTest extends ControllerTest {
|
||||
/** @var DemoStore|\PHPUnit_Framework_MockObject_MockObject $demoStore */
|
||||
private $demoStore;
|
||||
/** @var DemoProvider|\PHPUnit_Framework_MockObject_MockObject $demoProvider */
|
||||
private $demoProvider;
|
||||
/** @var ChatProvider|\PHPUnit_Framework_MockObject_MockObject $chatProvider */
|
||||
|
|
@ -20,20 +24,26 @@ class DemoControllerTest extends ControllerTest {
|
|||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->demoStore = $this->createMock(DemoStore::class);
|
||||
$this->demoProvider = $this->createMock(DemoProvider::class);
|
||||
$this->chatProvider = $this->createMock(ChatProvider::class);
|
||||
$this->demoListProvider = $this->createMock(DemoListProvider::class);
|
||||
}
|
||||
|
||||
public function testGetBasicList() {
|
||||
$controller = new DemoController(
|
||||
$this->getRequest(),
|
||||
private function getController(array $get = [], array $post = [], array $files = []) {
|
||||
return new DemoController(
|
||||
$this->getRequest($get, $post, $files),
|
||||
$this->getResponse(),
|
||||
$this->demoProvider,
|
||||
$this->chatProvider,
|
||||
$this->demoListProvider,
|
||||
''
|
||||
$this->demoStore,
|
||||
'supersecretkey'
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetBasicList() {
|
||||
$controller = $this->getController();
|
||||
|
||||
$this->demoListProvider->expects($this->once())
|
||||
->method('listDemos')
|
||||
|
|
@ -45,14 +55,7 @@ class DemoControllerTest extends ControllerTest {
|
|||
}
|
||||
|
||||
public function testGetListPageASC() {
|
||||
$controller = new DemoController(
|
||||
$this->getRequest(['page' => '3', 'order' => 'ASC']),
|
||||
$this->getResponse(),
|
||||
$this->demoProvider,
|
||||
$this->chatProvider,
|
||||
$this->demoListProvider,
|
||||
''
|
||||
);
|
||||
$controller = $this->getController(['page' => '3', 'order' => 'ASC']);
|
||||
|
||||
$this->demoListProvider->expects($this->once())
|
||||
->method('listDemos')
|
||||
|
|
@ -64,14 +67,7 @@ class DemoControllerTest extends ControllerTest {
|
|||
}
|
||||
|
||||
public function testListFilterBackend() {
|
||||
$controller = new DemoController(
|
||||
$this->getRequest(['backend' => 'foo']),
|
||||
$this->getResponse(),
|
||||
$this->demoProvider,
|
||||
$this->chatProvider,
|
||||
$this->demoListProvider,
|
||||
''
|
||||
);
|
||||
$controller = $this->getController(['backend' => 'foo']);
|
||||
|
||||
$this->demoListProvider->expects($this->once())
|
||||
->method('listDemos')
|
||||
|
|
@ -81,4 +77,105 @@ class DemoControllerTest extends ControllerTest {
|
|||
$controller->listDemos();
|
||||
$this->assertEquals('["dummy"]', $this->getResponseData());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage Invalid key
|
||||
*/
|
||||
public function testSetDemoUrlInvalidKey() {
|
||||
$controller = $this->getController([], [
|
||||
'hash' => 'foo',
|
||||
'backend' => 'bar',
|
||||
'path' => '/bar',
|
||||
'url' => 'http://bar/',
|
||||
'key' => 'invalid',
|
||||
]);
|
||||
|
||||
$controller->setDemoUrl('1');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage Invalid demo hash
|
||||
*/
|
||||
public function testSetDemoUrlInvalidHash() {
|
||||
$controller = $this->getController([], [
|
||||
'hash' => 'invalidhash',
|
||||
'backend' => 'bar',
|
||||
'path' => '/bar',
|
||||
'url' => 'http://bar/',
|
||||
'key' => 'supersecretkey',
|
||||
]);
|
||||
|
||||
$demo = $this->createConfiguredMock(Demo::class, [
|
||||
'getHash' => 'validhash',
|
||||
]);
|
||||
|
||||
$this->demoProvider
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with(1, true)
|
||||
->willReturn($demo);
|
||||
|
||||
$controller->setDemoUrl('1');
|
||||
}
|
||||
|
||||
public function testSetDemoUrlNonStatic() {
|
||||
$controller = $this->getController([], [
|
||||
'hash' => 'validhash',
|
||||
'backend' => 'bar',
|
||||
'path' => '/bar',
|
||||
'url' => 'http://bar/',
|
||||
'key' => 'supersecretkey',
|
||||
]);
|
||||
|
||||
$demo = $this->createConfiguredMock(Demo::class, [
|
||||
'getHash' => 'validhash',
|
||||
'getBackend' => 'foo',
|
||||
]);
|
||||
|
||||
$this->demoProvider->expects($this->once())
|
||||
->method('get')
|
||||
->with(1, true)
|
||||
->willReturn($demo);
|
||||
|
||||
$this->demoProvider->expects($this->once())
|
||||
->method('setDemoUrl')
|
||||
->with(1, 'bar', 'http://bar/', '/bar');
|
||||
|
||||
$this->demoStore->expects($this->never())
|
||||
->method('remove');
|
||||
|
||||
$controller->setDemoUrl('1');
|
||||
}
|
||||
|
||||
public function testSetDemoUrlStatic() {
|
||||
$controller = $this->getController([], [
|
||||
'hash' => 'validhash',
|
||||
'backend' => 'bar',
|
||||
'path' => '/bar',
|
||||
'url' => 'http://bar/',
|
||||
'key' => 'supersecretkey',
|
||||
]);
|
||||
|
||||
$demo = $this->createConfiguredMock(Demo::class, [
|
||||
'getHash' => 'validhash',
|
||||
'getBackend' => 'static',
|
||||
]);
|
||||
|
||||
$this->demoProvider->expects($this->once())
|
||||
->method('get')
|
||||
->with(1, true)
|
||||
->willReturn($demo);
|
||||
|
||||
$this->demoProvider->expects($this->once())
|
||||
->method('setDemoUrl')
|
||||
->with(1, 'bar', 'http://bar/', '/bar');
|
||||
|
||||
$this->demoStore->expects($this->once())
|
||||
->method('remove')
|
||||
->with($demo);
|
||||
|
||||
$controller->setDemoUrl('1');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue