Add basic way to have running entry
This commit is contained in:
parent
fe0bee63e0
commit
6abb14b3bd
15 changed files with 570 additions and 12 deletions
|
@ -13,7 +13,7 @@
|
|||
"doctrine/doctrine-migrations-bundle": "^3.0",
|
||||
"doctrine/orm": "^2.7",
|
||||
"phpdocumentor/reflection-docblock": "^5.2",
|
||||
"sensio/framework-extra-bundle": "^5.1",
|
||||
"sensio/framework-extra-bundle": "^5.6",
|
||||
"symfony/asset": "5.1.*",
|
||||
"symfony/console": "5.1.*",
|
||||
"symfony/dotenv": "5.1.*",
|
||||
|
|
4
composer.lock
generated
4
composer.lock
generated
|
@ -4,7 +4,7 @@
|
|||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "619de2ebf9720ae1643b2e798c5a7355",
|
||||
"content-hash": "42c5e66445821e40a4194fe5a0a8a7b7",
|
||||
"packages": [
|
||||
{
|
||||
"name": "composer/package-versions-deprecated",
|
||||
|
@ -7855,7 +7855,7 @@
|
|||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": {
|
||||
"php": ">=7.2.5",
|
||||
"php": "^7.3.0",
|
||||
"ext-ctype": "*",
|
||||
"ext-iconv": "*"
|
||||
},
|
||||
|
|
|
@ -7,7 +7,8 @@ framework:
|
|||
# Enables session support. Note that the session will ONLY be started if you read or write from it.
|
||||
# Remove or comment this section to explicitly disable session support.
|
||||
session:
|
||||
handler_id: null
|
||||
handler_id: 'session.handler.native_file'
|
||||
# save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%'
|
||||
cookie_secure: auto
|
||||
cookie_samesite: lax
|
||||
|
||||
|
|
|
@ -1,3 +1,13 @@
|
|||
#index:
|
||||
# path: /
|
||||
# controller: App\Controller\DefaultController::index
|
||||
---
|
||||
index:
|
||||
path: /
|
||||
controller: App\Controller\TimeController::index
|
||||
|
||||
startNewEntry:
|
||||
path: /start
|
||||
controller: App\Controller\TimeController::start
|
||||
methods: POST
|
||||
stopRunningEntry:
|
||||
path: /stop
|
||||
controller: App\Controller\TimeController::stop
|
||||
methods: POST
|
||||
|
|
|
@ -24,6 +24,9 @@
|
|||
<filter>
|
||||
<whitelist processUncoveredFilesFromWhitelist="true">
|
||||
<directory suffix=".php">src</directory>
|
||||
<exclude>
|
||||
<file>src/Kernel.php</file>
|
||||
</exclude>
|
||||
</whitelist>
|
||||
</filter>
|
||||
|
||||
|
|
0
src/Controller/.gitignore
vendored
0
src/Controller/.gitignore
vendored
67
src/Controller/TimeController.php
Normal file
67
src/Controller/TimeController.php
Normal file
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
/*
|
||||
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
use App\Service\ActiveEntry;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class TimeController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @var ActiveEntry
|
||||
*/
|
||||
private $activeEntry;
|
||||
|
||||
public function __construct(ActiveEntry $activeEntry)
|
||||
{
|
||||
$this->activeEntry = $activeEntry;
|
||||
}
|
||||
|
||||
public function index(): Response
|
||||
{
|
||||
return $this->render('time/index.html.twig', [
|
||||
'entry' => $this->activeEntry->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function start(Request $request): Response
|
||||
{
|
||||
$this->activeEntry->startNew(
|
||||
$request->request->get('title')
|
||||
);
|
||||
|
||||
return $this->redirectToRoute('index');
|
||||
}
|
||||
|
||||
public function stop(): Response
|
||||
{
|
||||
if ($this->activeEntry->get()->isRunning() === false) {
|
||||
return $this->redirectToRoute('index');
|
||||
}
|
||||
|
||||
$this->activeEntry->stopRunning();
|
||||
|
||||
return $this->redirectToRoute('index');
|
||||
}
|
||||
}
|
73
src/Entity/Entry.php
Normal file
73
src/Entity/Entry.php
Normal file
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
/*
|
||||
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
class Entry
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $title = '';
|
||||
|
||||
/**
|
||||
* @var \DateTimeImmutable|null
|
||||
*/
|
||||
private $start;
|
||||
|
||||
/**
|
||||
* @var \DateTimeImmutable|null
|
||||
*/
|
||||
private $stop;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $running = false;
|
||||
|
||||
public function __construct(
|
||||
string $title = ''
|
||||
) {
|
||||
$this->title = $title;
|
||||
}
|
||||
|
||||
public function start(): void
|
||||
{
|
||||
$this->start = new \DateTimeImmutable();
|
||||
$this->running = true;
|
||||
}
|
||||
|
||||
public function stop(): void
|
||||
{
|
||||
$this->stop = new \DateTimeImmutable();
|
||||
$this->running = false;
|
||||
}
|
||||
|
||||
public function isRunning(): bool
|
||||
{
|
||||
return $this->running;
|
||||
}
|
||||
|
||||
public function getTitle(): string
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
}
|
62
src/Service/ActiveEntry.php
Normal file
62
src/Service/ActiveEntry.php
Normal file
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
/*
|
||||
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
use App\Entity\Entry;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
|
||||
class ActiveEntry
|
||||
{
|
||||
/**
|
||||
* @var SessionInterface
|
||||
*/
|
||||
private $session;
|
||||
|
||||
public function __construct(SessionInterface $session)
|
||||
{
|
||||
$this->session = $session;
|
||||
}
|
||||
|
||||
public function startNew(
|
||||
string $title
|
||||
): Entry {
|
||||
$entry = new Entry(
|
||||
$title
|
||||
);
|
||||
$entry->start();
|
||||
|
||||
$this->session->set('runningEntry', $entry);
|
||||
|
||||
return $entry;
|
||||
}
|
||||
|
||||
public function stopRunning(): void
|
||||
{
|
||||
$this->get()->stop();
|
||||
$this->session->remove('runningEntry');
|
||||
}
|
||||
|
||||
public function get(): Entry
|
||||
{
|
||||
return $this->session->get('runningEntry') ?? new Entry();
|
||||
}
|
||||
}
|
|
@ -1,12 +1,17 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<!doctype html>
|
||||
<html class="no-js" lang="en-US">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>{% block title %}Welcome!{% endblock %}</title>
|
||||
<meta charset="utf-8">
|
||||
<title>Timetracking</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
{% block stylesheets %}{% endblock %}
|
||||
</head>
|
||||
|
||||
<body>
|
||||
{% block body %}{% endblock %}
|
||||
{% block body %}
|
||||
{% block content %}
|
||||
{% endblock %}
|
||||
{% endblock %}
|
||||
{% block javascripts %}{% endblock %}
|
||||
</body>
|
||||
</html>
|
||||
|
|
7
templates/time/index.html.twig
Normal file
7
templates/time/index.html.twig
Normal file
|
@ -0,0 +1,7 @@
|
|||
{% extends 'base.html.twig' %}
|
||||
|
||||
{% block content %}
|
||||
<h1>Time entries</h1>
|
||||
|
||||
{{ include('time/timer.html.twig') }}
|
||||
{% endblock %}
|
11
templates/time/timer.html.twig
Normal file
11
templates/time/timer.html.twig
Normal file
|
@ -0,0 +1,11 @@
|
|||
{% if entry.isRunning %}
|
||||
<form action="{{ path('stopRunningEntry') }}" method="POST">
|
||||
<input name="title" type="input" value="{{ entry.title }}">
|
||||
<button type="submit">Stop</button>
|
||||
</form>
|
||||
{% else %}
|
||||
<form action="{{ path('startNewEntry') }}" method="POST">
|
||||
<input name="title" type="input" placeholder="Start new with title" value="{{ entry.title }}">
|
||||
<button type="submit">Start</button>
|
||||
</form>
|
||||
{% endif %}
|
98
tests/Functional/Controller/TimeControllerTest.php
Normal file
98
tests/Functional/Controller/TimeControllerTest.php
Normal file
|
@ -0,0 +1,98 @@
|
|||
<?php
|
||||
|
||||
namespace App\Tests\Functional\Controller;
|
||||
|
||||
/*
|
||||
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
/**
|
||||
* @covers App\Controller\TimeController
|
||||
* @uses App\Entity\Entry
|
||||
* @uses App\Service\ActiveEntry
|
||||
*/
|
||||
class TimeControllerTest extends WebTestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function rootCanBeOpened()
|
||||
{
|
||||
$client = static::createClient();
|
||||
$crawler = $client->request('GET', '/');
|
||||
|
||||
$this->assertResponseIsSuccessful();
|
||||
$this->assertSelectorTextContains('h1', 'Time entries');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function newEntryCanBeStarted()
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
$client->request('GET', '/');
|
||||
|
||||
$client->submitForm('Start', ['title' => 'Test Entry']);
|
||||
$this->assertResponseRedirects('/');
|
||||
|
||||
$client->request('GET', '/');
|
||||
$this->assertResponseIsSuccessful();
|
||||
$this->assertInputValueSame('title', 'Test Entry');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function runningEntryCanBeStopped()
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
$client->request('GET', '/');
|
||||
|
||||
$client->submitForm('Start', ['title' => 'Test Entry']);
|
||||
|
||||
$client->request('GET', '/');
|
||||
$client->submitForm('Stop');
|
||||
$this->assertResponseRedirects('/');
|
||||
|
||||
$client->request('GET', '/');
|
||||
$this->assertResponseIsSuccessful();
|
||||
$this->assertInputValueSame('title', '');
|
||||
$this->assertSelectorTextSame('button', 'Start');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function noneRunningEntryCanBeStopped()
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
$client->request('POST', '/stop');
|
||||
$this->assertResponseRedirects('/');
|
||||
|
||||
$client->request('GET', '/');
|
||||
$this->assertResponseIsSuccessful();
|
||||
$this->assertInputValueSame('title', '');
|
||||
$this->assertSelectorTextSame('button', 'Start');
|
||||
}
|
||||
}
|
98
tests/Unit/Entity/EntryTest.php
Normal file
98
tests/Unit/Entity/EntryTest.php
Normal file
|
@ -0,0 +1,98 @@
|
|||
<?php
|
||||
|
||||
namespace App\Tests\Entity;
|
||||
|
||||
/*
|
||||
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
use App\Entity\Entry;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers App\Entity\Entry
|
||||
*/
|
||||
class EntryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function canBeCreated()
|
||||
{
|
||||
$subject = new Entry();
|
||||
|
||||
static::assertInstanceOf(Entry::class, $subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function canBeCreatedWithTitle()
|
||||
{
|
||||
$subject = new Entry('Example title');
|
||||
|
||||
static::assertInstanceOf(Entry::class, $subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function returnsProvidedTitle()
|
||||
{
|
||||
$subject = new Entry('Example title');
|
||||
|
||||
static::assertSame('Example title', $subject->getTitle());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function canBeStarted()
|
||||
{
|
||||
$subject = new Entry();
|
||||
|
||||
$subject->start();
|
||||
|
||||
static::assertTrue($subject->isRunning());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function canBeStopped()
|
||||
{
|
||||
$subject = new Entry();
|
||||
|
||||
$subject->start();
|
||||
$subject->stop();
|
||||
|
||||
static::assertFalse($subject->isRunning());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function canBeStoppedEvenIfNotRunning()
|
||||
{
|
||||
$subject = new Entry();
|
||||
|
||||
$subject->stop();
|
||||
|
||||
static::assertFalse($subject->isRunning());
|
||||
}
|
||||
}
|
123
tests/Unit/Service/ActiveEntryTest.php
Normal file
123
tests/Unit/Service/ActiveEntryTest.php
Normal file
|
@ -0,0 +1,123 @@
|
|||
<?php
|
||||
|
||||
namespace App\Tests\Service;
|
||||
|
||||
/*
|
||||
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
use App\Entity\Entry;
|
||||
use App\Service\ActiveEntry;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
|
||||
|
||||
/**
|
||||
* @covers App\Service\ActiveEntry
|
||||
*/
|
||||
class ActiveEntryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function canBeCreated()
|
||||
{
|
||||
$session = $this->prophesize(SessionInterface::class);
|
||||
$subject = new ActiveEntry(
|
||||
$session->reveal()
|
||||
);
|
||||
|
||||
static::assertInstanceOf(ActiveEntry::class, $subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function returnsEmptyEntryAsDefault()
|
||||
{
|
||||
$session = $this->prophesize(SessionInterface::class);
|
||||
$subject = new ActiveEntry(
|
||||
$session->reveal()
|
||||
);
|
||||
|
||||
$entry = $subject->get();
|
||||
|
||||
static::assertInstanceOf(Entry::class, $entry);
|
||||
static::assertSame('', $entry->getTitle());
|
||||
static::assertFalse($entry->isRunning());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function canStartANewEntry()
|
||||
{
|
||||
$session = $this->prophesize(SessionInterface::class);
|
||||
$subject = new ActiveEntry(
|
||||
$session->reveal()
|
||||
);
|
||||
|
||||
$subject->startNew('Example title');
|
||||
|
||||
$session->set('runningEntry', Argument::that(function (Entry $entry) {
|
||||
return $entry->getTitle() === 'Example title'
|
||||
&& $entry->isRunning()
|
||||
;
|
||||
}))->shouldBeCalled();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function returnsStartedEntry()
|
||||
{
|
||||
$session = $this->prophesize(SessionInterface::class);
|
||||
$entry = $this->prophesize(Entry::class);
|
||||
$subject = new ActiveEntry(
|
||||
$session->reveal()
|
||||
);
|
||||
|
||||
$session->set('runningEntry', Argument::type(Entry::class))->shouldBeCalled();
|
||||
$session->get('runningEntry')->willReturn($entry->reveal())->shouldBeCalled();
|
||||
|
||||
$subject->startNew('Example title');
|
||||
|
||||
static::assertSame($entry->reveal(), $subject->get());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function stopsRunningEntry()
|
||||
{
|
||||
$session = $this->prophesize(SessionInterface::class);
|
||||
$entry = $this->prophesize(Entry::class);
|
||||
$subject = new ActiveEntry(
|
||||
$session->reveal()
|
||||
);
|
||||
|
||||
$session->set('runningEntry', Argument::type(Entry::class))->shouldBeCalled();
|
||||
$session->get('runningEntry')->willReturn($entry->reveal())->shouldBeCalled();
|
||||
|
||||
$subject->startNew('Example title');
|
||||
$subject->stopRunning();
|
||||
|
||||
$session->remove('runningEntry')->shouldBeCalled();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue