Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Commit

Permalink
V0.4.2 PHPUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
MarquandT committed Dec 11, 2023
1 parent bcebd70 commit 3507ed6
Show file tree
Hide file tree
Showing 24 changed files with 482 additions and 73 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
###> mediashare/time-tracking ###
APP_VERSION=0.4.0
APP_VERSION=0.4.2
###< mediashare/time-tracking ###

###> symfony/framework-bundle ###
Expand Down
6 changes: 3 additions & 3 deletions .env.local.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

// This file was generated by running "composer dump-env prod"
// This file was generated by running "composer dump-env dev"

return array (
'APP_ENV' => 'prod',
'APP_VERSION' => '0.4.0',
'APP_ENV' => 'dev',
'APP_VERSION' => '0.4.2',
'APP_SECRET' => '91739cd3f0af5ce014a7db0d451160e2805aa31b',
);
6 changes: 6 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# define your env variables for the test env here
KERNEL_CLASS='App\Kernel'
APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999
PANTHER_APP_ENV=panther
PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ symfony.lock
.vscode
.idea
###< mediashare/time-tracking ###

###> symfony/phpunit-bridge ###
.phpunit.result.cache
/phpunit.xml
###< symfony/phpunit-bridge ###

###> phpunit/phpunit ###
/phpunit.xml
.phpunit.result.cache
###< phpunit/phpunit ###
23 changes: 23 additions & 0 deletions bin/phpunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env php
<?php

if (!ini_get('date.timezone')) {
ini_set('date.timezone', 'UTC');
}

if (is_file(dirname(__DIR__).'/vendor/phpunit/phpunit/phpunit')) {
if (PHP_VERSION_ID >= 80000) {
require dirname(__DIR__).'/vendor/phpunit/phpunit/phpunit';
} else {
define('PHPUNIT_COMPOSER_INSTALL', dirname(__DIR__).'/vendor/autoload.php');
require PHPUNIT_COMPOSER_INSTALL;
PHPUnit\TextUI\Command::main();
}
} else {
if (!is_file(dirname(__DIR__).'/vendor/symfony/phpunit-bridge/bin/simple-phpunit.php')) {
echo "Unable to find the `simple-phpunit.php` script in `vendor/symfony/phpunit-bridge/bin/`.\n";
exit(1);
}

require dirname(__DIR__).'/vendor/symfony/phpunit-bridge/bin/simple-phpunit.php';
}
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,11 @@
"time-tracking": {
"console": "bin/time-tracking"
}
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"symfony/browser-kit": "^6.4",
"symfony/css-selector": "^6.4",
"symfony/phpunit-bridge": "^7.0"
}
}
23 changes: 0 additions & 23 deletions config/bootstrap.php

This file was deleted.

38 changes: 38 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="tests/bootstrap.php"
convertDeprecationsToExceptions="false"
>
<php>
<ini name="display_errors" value="1" />
<ini name="error_reporting" value="-1" />
<server name="APP_ENV" value="test" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
<server name="SYMFONY_PHPUNIT_VERSION" value="9.6" />
</php>

<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>

<extensions>
</extensions>
</phpunit>
9 changes: 4 additions & 5 deletions src/Command/ArchiveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {
);

// Tracking
$trackingService = new TrackingService($config);
$tracking = $trackingService->archiveTracking($input->getOption('stop'));
$trackingService = new TrackingService($config, createItIfNotExist: !$input->getOption('stop'));
$tracking = $trackingService->archiveTracking();

// Update tracking data file
$serializerService = new SerializerService();
Expand All @@ -66,9 +66,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$input->getOption('config-datetime-format'),
$lastTrackingDirectory,
$config->getTrackingId() === $lastTrackingId
? (new \DateTime())->format(
$input->getOption('config-datetime-format') ?? $configService->getLastDateTimeFormat()
) : $lastTrackingId
? (new \DateTime())->format('YmdHis')
: $lastTrackingId
,
);

Expand Down
7 changes: 3 additions & 4 deletions src/Command/RemoveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
);

// Tracking
$trackingService = new TrackingService($config);
$trackingService = new TrackingService($config, createItIfNotExist: false);
$tracking = $trackingService->getTracking(createItIfNotExist: false);
$trackingService->removeTracking();

Expand All @@ -61,9 +61,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$input->getOption('config-datetime-format'),
$lastTrackingDirectory,
$config->getTrackingId() === $lastTrackingId
? (new \DateTime())->format(
$input->getOption('config-datetime-format') ?? $configService->getLastDateTimeFormat()
) : $lastTrackingId
? (new \DateTime())->format('YmdHis')
: $lastTrackingId
,
);

Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Commit.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Commit {

private string|null $id = null;
private string $message = '';
private string $duration = '00:00:00';

private StepCollection $steps;

public function __construct() {
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Tracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function setId(string $id): self {
return $this;
}

public function getId(): string {
public function getId(): string|null {
return $this->id;
}

Expand Down
9 changes: 9 additions & 0 deletions src/Service/CommitService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function createCommit(?string $message = null, ?string $duration = null):
$commit->addStep(
$this->stepService->createStepWithCustomDuration(
$duration,
($lastStep = $this->tracking->getSteps()?->last())?->getEndDate()
? $lastStep->getStartDate()
: null
)
);
else:
Expand Down Expand Up @@ -55,6 +58,9 @@ public function createCommit(?string $message = null, ?string $duration = null):
return $this->tracking;
}

/**
* @throws CommitNotFoundException
*/
public function editCommit(
string $id,
string|false $message = false,
Expand Down Expand Up @@ -102,6 +108,9 @@ public function editCommit(
return $this->tracking;
}

/**
* @throws CommitNotFoundException
*/
public function removeCommit(
string $id,
): Tracking {
Expand Down
16 changes: 13 additions & 3 deletions src/Service/ConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,19 @@ public function getLastTrackingDirectory(): string {
}

public function getLastTrackingId(string $trackingDirectory): string|null {
return $this->getLastConfig()->getTrackingId()
?? (new TrackingService(new Config(trackingDirectory: $trackingDirectory)))->getTrackings()?->last()?->getId()
;
try {
if ($lastTrackingIdByConfig = $this->getLastConfig()->getTrackingId()):
return $lastTrackingIdByConfig;
endif;

return (new TrackingService(new Config(trackingDirectory: $trackingDirectory)))
->getTrackings()?->last()?->getId();

} catch (\Exception $exception) {

}

return null;
}

private function getLastConfig(): Config {
Expand Down
4 changes: 3 additions & 1 deletion src/Service/SerializerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public function __construct() {
}

/**
* Convert json file to Tracking Object
* Convert json file to Entity object
* @throws FileNotFoundException
* @throws JsonDecodeException
*/
public function read(string $filepath, string $className): Tracking|Config {
if (!$this->filesystem->exists($filepath)):
Expand Down
60 changes: 37 additions & 23 deletions src/Service/TrackingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@
class TrackingService {
private SerializerService $serializerService;
private Filesystem $filesystem;
private StepService $stepService;
private Tracking $tracking;

public function __construct(
private Config $config,
bool $createItIfNotExist = true
) {
$this->serializerService = new SerializerService();
$this->filesystem = new Filesystem();

$this->stepService = new StepService();

$this->tracking = $this->getTracking($createItIfNotExist);
}

/**
Expand All @@ -41,28 +48,24 @@ public function getTrackings(): TrackingCollection {
public function getTracking(bool $createItIfNotExist = true): Tracking {
$trackingExist = $this->filesystem->exists($filepath = $this->getTrackingFilepath());
if (!$trackingExist && $createItIfNotExist):
return $this->createTracking();
return $this->tracking = $this->createTracking();
elseif (!$trackingExist):
throw new TrackingNotFoundException();
endif;

return $this->serializerService->read($filepath, Tracking::class);
return $this->tracking = $this->serializerService->read($filepath, Tracking::class);
}

public function createTracking(array $data = []): Tracking {
if (!array_keys($data, 'id')):
$data = array_merge($data, [
'id' => $this->config->getTrackingId()
?? (new \DateTime())->format('YmdHis')
]);
endif;

/** @var Tracking $tracking */
$tracking = $this->serializerService->arrayToEntity($data, Tracking::class);

if (!$tracking->getId()):
$tracking->setId($this->config->getTrackingId() ?? (new \DateTime())->format('YmdHis'));
endif;

if ($tracking->isRun() && !$tracking->getSteps()?->last()?->getEndDate()):
$stepService = new StepService($tracking);
$tracking->addStep($stepService->createStep());
$tracking->addStep($this->stepService->createStep());
endif;

$this->serializerService->writeTracking($this->getTrackingFilepath(), $tracking);
Expand All @@ -72,27 +75,36 @@ public function createTracking(array $data = []): Tracking {

public function startTracking(
string|false $name = false,
string|null $duration = null,
string|false $duration = false,
): Tracking {
$tracking = $this->getTracking()->setRun(true);
$tracking = $this->tracking->setRun(true);
$tracking->setName($name !== false ? $name : $tracking->getName());

if ($duration):
$firstStep = $tracking->getSteps()->first();
$tracking->getSteps()->clear();
$tracking->addStep($this
->stepService
->createStepWithCustomDuration(
$duration,
$firstStep?->getStartDate(),
)
);
endif;

if (!$tracking->getStartDate() || !($lastStep = $tracking->getSteps()?->last()) || $lastStep->getEndDate()):
$stepService = new StepService();
$tracking
->addStep(
$duration
? $stepService->createStepWithCustomDuration($duration)
: $stepService->createStep()
$this->stepService->createStep()
);
endif;

return $tracking;
}

public function stopTracking(bool $createItIfNotExist = true): Tracking {
$tracking = $this->getTracking($createItIfNotExist);
$tracking->setRun(false);
public function stopTracking(): Tracking {
$tracking = $this->tracking
->setRun(false);

if (($lastStep = $tracking->getSteps()?->last()) && !$lastStep->getEndDate()):
$tracking
Expand All @@ -106,10 +118,9 @@ public function stopTracking(bool $createItIfNotExist = true): Tracking {
return $tracking;
}

public function archiveTracking(bool $stop = false): Tracking {
$stop ? $this->stopTracking(createItIfNotExist: false) : null;
public function archiveTracking(): Tracking {
return $this
->getTracking()
->stopTracking()
->setArchived(true);
}

Expand All @@ -121,6 +132,9 @@ public function removeTracking(): self {
return $this;
}

/**
* @throws TrackingNotFoundException
*/
public function getTrackingFilepath(): string {
if (!$this->config->getTrackingId()):
throw new TrackingNotFoundException();
Expand Down
Loading

0 comments on commit 3507ed6

Please sign in to comment.