From 791119f2b6eedc2da7f39a896161c36290ad1fc2 Mon Sep 17 00:00:00 2001 From: nuxsmin Date: Sun, 22 Jul 2018 22:51:31 +0200 Subject: [PATCH] * [ADD] Unit testing. Work in progress * [MOD] Code refactoring --- .../web/Controllers/TaskController.php | 2 +- lib/SP/Services/Track/TrackService.php | 23 ++- .../Notification/NotificationServiceTest.php | 2 +- test/Services/Plugin/PluginServiceTest.php | 2 +- .../PublicLink/PublicLinkServiceTest.php | 2 +- test/Services/Tag/TagServiceTest.php | 2 +- test/Services/Track/TrackServiceTest.php | 182 ++++++++++++++++++ 7 files changed, 204 insertions(+), 11 deletions(-) create mode 100644 test/Services/Track/TrackServiceTest.php diff --git a/app/modules/web/Controllers/TaskController.php b/app/modules/web/Controllers/TaskController.php index 2d902572..aba5865c 100644 --- a/app/modules/web/Controllers/TaskController.php +++ b/app/modules/web/Controllers/TaskController.php @@ -87,7 +87,7 @@ class TaskController { $task = TaskFactory::create($taskId, Task::genTaskId($taskId)); - TaskFactory::update($task->getTaskId(), TaskFactory::createMessage($task->getTaskId(), 'Prueba Tarea')); + TaskFactory::update($task->getTaskId(), TaskFactory::createMessage($task->getTaskId(), 'Test Task')); echo $task->getTaskId(); } diff --git a/lib/SP/Services/Track/TrackService.php b/lib/SP/Services/Track/TrackService.php index ede4e9dc..3d08e701 100644 --- a/lib/SP/Services/Track/TrackService.php +++ b/lib/SP/Services/Track/TrackService.php @@ -28,6 +28,7 @@ use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\DataModel\TrackData; use SP\Http\Request; +use SP\Repositories\NoSuchItemException; use SP\Repositories\Track\TrackRepository; use SP\Repositories\Track\TrackRequest; use SP\Services\Service; @@ -76,13 +77,15 @@ class TrackService extends Service /** * @param $id int|array * - * @return mixed * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Core\Exceptions\ConstraintException + * @throws NoSuchItemException */ public function delete($id) { - return $this->trackRepository->delete($id); + if ($this->trackRepository->delete($id) === 0) { + throw new NoSuchItemException(__u('Track no encontrado')); + } } /** @@ -91,10 +94,17 @@ class TrackService extends Service * @return TrackData * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException + * @throws NoSuchItemException */ public function getById($id) { - return $this->trackRepository->getById($id)->getData(); + $result = $this->trackRepository->getById($id); + + if ($result->getNumRows() === 0) { + throw new NoSuchItemException(__u('Track no encontrado')); + } + + return $result->getData(); } /** @@ -121,8 +131,6 @@ class TrackService extends Service $attempts = $this->getTracksForClientFromTime($trackRequest); if ($attempts >= self::TIME_TRACKING_MAX_ATTEMPTS) { -// $this->add($trackRequest); - $this->eventDispatcher->notifyEvent('track.delay', new Event($this, EventMessage::factory() ->addDescription(sprintf(__('Intentos excedidos (%d/%d)'), $attempts, self::TIME_TRACKING_MAX_ATTEMPTS)) @@ -161,17 +169,20 @@ class TrackService extends Service /** * @param TrackRequest $trackRequest * + * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function add(TrackRequest $trackRequest) { - $this->trackRepository->add($trackRequest); + $result = $this->trackRepository->add($trackRequest); $this->eventDispatcher->notifyEvent('track.add', new Event($this, EventMessage::factory() ->addDescription($this->request->getClientAddress(true))) ); + + return $result; } /** diff --git a/test/Services/Notification/NotificationServiceTest.php b/test/Services/Notification/NotificationServiceTest.php index d3289aba..7245c37c 100644 --- a/test/Services/Notification/NotificationServiceTest.php +++ b/test/Services/Notification/NotificationServiceTest.php @@ -61,7 +61,7 @@ class NotificationServiceTest extends DatabaseTestCase // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); - // Inicializar el repositorio + // Inicializar el servicio self::$service = $dic->get(NotificationService::class); } diff --git a/test/Services/Plugin/PluginServiceTest.php b/test/Services/Plugin/PluginServiceTest.php index ae1a3b50..b05564b6 100644 --- a/test/Services/Plugin/PluginServiceTest.php +++ b/test/Services/Plugin/PluginServiceTest.php @@ -61,7 +61,7 @@ class PluginServiceTest extends DatabaseTestCase // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); - // Inicializar el repositorio + // Inicializar el servicio self::$service = $dic->get(PluginService::class); } diff --git a/test/Services/PublicLink/PublicLinkServiceTest.php b/test/Services/PublicLink/PublicLinkServiceTest.php index 7ee3a040..12b402a2 100644 --- a/test/Services/PublicLink/PublicLinkServiceTest.php +++ b/test/Services/PublicLink/PublicLinkServiceTest.php @@ -70,7 +70,7 @@ class PublicLinkServiceTest extends DatabaseTestCase // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); - // Inicializar el repositorio + // Inicializar el servicio self::$service = $dic->get(PublicLinkService::class); self::$salt = $dic->get(ConfigData::class)->getPasswordSalt(); diff --git a/test/Services/Tag/TagServiceTest.php b/test/Services/Tag/TagServiceTest.php index 4ed8407e..81dcebce 100644 --- a/test/Services/Tag/TagServiceTest.php +++ b/test/Services/Tag/TagServiceTest.php @@ -61,7 +61,7 @@ class TagServiceTest extends DatabaseTestCase // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); - // Inicializar el repositorio + // Inicializar el servicio self::$service = $dic->get(TagService::class); } diff --git a/test/Services/Track/TrackServiceTest.php b/test/Services/Track/TrackServiceTest.php new file mode 100644 index 00000000..8aa09500 --- /dev/null +++ b/test/Services/Track/TrackServiceTest.php @@ -0,0 +1,182 @@ +. + */ + +namespace SP\Tests\Services\Track; + +use SP\DataModel\TrackData; +use SP\Http\Request; +use SP\Repositories\NoSuchItemException; +use SP\Repositories\Track\TrackRequest; +use SP\Services\Track\TrackService; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Test\DatabaseTestCase; +use function SP\Test\setupContext; + +/** + * Class TrackServiceTest + * + * @package SP\Tests\Services\Track + */ +class TrackServiceTest extends DatabaseTestCase +{ + private static $request; + /** + * @var TrackService + */ + private static $service; + + /** + * @throws \DI\NotFoundException + * @throws \SP\Core\Context\ContextException + * @throws \DI\DependencyException + */ + public static function setUpBeforeClass() + { + $dic = setupContext(); + + self::$dataset = 'syspass_track.xml'; + + // Datos de conexión a la BBDD + self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); + + // Inicializar el servicio + self::$service = $dic->get(TrackService::class); + self::$request = $dic->get(Request::class); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws NoSuchItemException + */ + public function testDelete() + { + self::$service->delete(1); + + $this->assertEquals(5, $this->conn->getRowCount('Track')); + + $this->expectException(NoSuchItemException::class); + + self::$service->delete(10); + } + + /** + * @throws NoSuchItemException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\InvalidArgumentException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testAdd() + { + $data = new TrackRequest(); + $data->setTrackIp('192.168.0.1'); + $data->userId = 1; + $data->time = time(); + $data->source = __METHOD__; + + $this->assertEquals(7, self::$service->add($data)); + + /** @var TrackData $resultData */ + $resultData = self::$service->getById(7); + + $this->assertEquals(7, $resultData->getId()); + $this->assertEquals($data->userId, $resultData->getUserId()); + $this->assertEquals($data->time, $resultData->getTime()); + $this->assertEquals($data->source, $resultData->getSource()); + $this->assertEquals('192.168.0.1', $resultData->getIpv4()); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testGetAll() + { + $data = self::$service->getAll(); + + $this->assertCount(6, $data); + $this->assertInstanceOf(TrackData::class, $data[0]); + $this->assertEquals(1, $data[0]->getId()); + $this->assertEquals(0, $data[0]->getUserId()); + $this->assertEquals('1529145183', $data[0]->getTime()); + $this->assertEquals('login', $data[0]->getSource()); + $this->assertEquals('172.22.0.1', $data[0]->getIpv4()); + $this->assertEquals('', $data[0]->getIpv6()); + } + + /** + * @throws NoSuchItemException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testGetById() + { + $data = self::$service->getById(1); + + $this->assertInstanceOf(TrackData::class, $data); + $this->assertEquals(1, $data->getId()); + $this->assertEquals(0, $data->getUserId()); + $this->assertEquals('1529145183', $data->getTime()); + $this->assertEquals('login', $data->getSource()); + $this->assertEquals('172.22.0.1', $data->getIpv4()); + + $this->expectException(NoSuchItemException::class); + + self::$service->getById(10); + } + + /** + * @throws \SP\Core\Exceptions\InvalidArgumentException + * @throws \Exception + */ + public function testCheckTracking() + { + $this->assertFalse(self::$service->checkTracking(TrackService::getTrackRequest('TEST', self::$request))); + + for ($i = 1; $i <= 10; $i++) { + self::$service->add(TrackService::getTrackRequest('TEST', self::$request)); + } + + $this->assertTrue(self::$service->checkTracking(TrackService::getTrackRequest('TEST', self::$request))); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\InvalidArgumentException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testGetTracksForClientFromTime() + { + $data = new TrackRequest(); + $data->setTrackIp('172.22.0.1'); + $data->time = 1529272367; + $data->source = 'login'; + + $this->assertEquals(3, self::$service->getTracksForClientFromTime($data)); + + $data->time = time(); + + $this->assertEquals(0, self::$service->getTracksForClientFromTime($data)); + } +}