Add basic way to have running entry

This commit is contained in:
Daniel Siepmann 2020-10-20 21:41:19 +02:00
parent fe0bee63e0
commit 6abb14b3bd
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
15 changed files with 570 additions and 12 deletions

View file

@ -13,7 +13,7 @@
"doctrine/doctrine-migrations-bundle": "^3.0", "doctrine/doctrine-migrations-bundle": "^3.0",
"doctrine/orm": "^2.7", "doctrine/orm": "^2.7",
"phpdocumentor/reflection-docblock": "^5.2", "phpdocumentor/reflection-docblock": "^5.2",
"sensio/framework-extra-bundle": "^5.1", "sensio/framework-extra-bundle": "^5.6",
"symfony/asset": "5.1.*", "symfony/asset": "5.1.*",
"symfony/console": "5.1.*", "symfony/console": "5.1.*",
"symfony/dotenv": "5.1.*", "symfony/dotenv": "5.1.*",

4
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "619de2ebf9720ae1643b2e798c5a7355", "content-hash": "42c5e66445821e40a4194fe5a0a8a7b7",
"packages": [ "packages": [
{ {
"name": "composer/package-versions-deprecated", "name": "composer/package-versions-deprecated",
@ -7855,7 +7855,7 @@
"prefer-stable": false, "prefer-stable": false,
"prefer-lowest": false, "prefer-lowest": false,
"platform": { "platform": {
"php": ">=7.2.5", "php": "^7.3.0",
"ext-ctype": "*", "ext-ctype": "*",
"ext-iconv": "*" "ext-iconv": "*"
}, },

View file

@ -7,7 +7,8 @@ framework:
# Enables session support. Note that the session will ONLY be started if you read or write from it. # 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. # Remove or comment this section to explicitly disable session support.
session: session:
handler_id: null handler_id: 'session.handler.native_file'
# save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%'
cookie_secure: auto cookie_secure: auto
cookie_samesite: lax cookie_samesite: lax

View file

@ -1,3 +1,13 @@
#index: ---
# path: / index:
# controller: App\Controller\DefaultController::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

View file

@ -24,6 +24,9 @@
<filter> <filter>
<whitelist processUncoveredFilesFromWhitelist="true"> <whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory> <directory suffix=".php">src</directory>
<exclude>
<file>src/Kernel.php</file>
</exclude>
</whitelist> </whitelist>
</filter> </filter>

View file

View 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
View 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;
}
}

View 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();
}
}

View file

@ -1,12 +1,17 @@
<!DOCTYPE html> <!doctype html>
<html> <html class="no-js" lang="en-US">
<head> <head>
<meta charset="UTF-8"> <meta charset="utf-8">
<title>{% block title %}Welcome!{% endblock %}</title> <title>Timetracking</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
{% block stylesheets %}{% endblock %} {% block stylesheets %}{% endblock %}
</head> </head>
<body> <body>
{% block body %}{% endblock %} {% block body %}
{% block content %}
{% endblock %}
{% endblock %}
{% block javascripts %}{% endblock %} {% block javascripts %}{% endblock %}
</body> </body>
</html> </html>

View file

@ -0,0 +1,7 @@
{% extends 'base.html.twig' %}
{% block content %}
<h1>Time entries</h1>
{{ include('time/timer.html.twig') }}
{% endblock %}

View 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 %}

View 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');
}
}

View 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());
}
}

View 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();
}
}