From a7a56673013e36710f475f001ce6cbc68f7f62f0 Mon Sep 17 00:00:00 2001
From: Alejandro Celaya <alejandro@alejandrocelaya.com>
Date: Sun, 2 Dec 2018 19:13:49 +0100
Subject: [PATCH] Improved repository tests

---
 CHANGELOG.md                                  |  1 +
 composer.json                                 |  2 +-
 indocker                                      |  7 ++++++
 module/Core/src/Repository/TagRepository.php  |  2 +-
 .../src/Repository/TagRepositoryInterface.php |  2 +-
 .../Repository/ShortUrlRepositoryTest.php     | 22 ++++++++++++++++++-
 .../Repository/VisitRepositoryTest.php        |  2 ++
 7 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index a1da403a..53c3f8d3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -22,6 +22,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 
 * [#267](https://github.com/shlinkio/shlink/issues/267) API responses and the CLI interface is no longer translated and uses english always. Only not found error templates are still translated.
 * [#289](https://github.com/shlinkio/shlink/issues/289) Extracted coding standard rules to a separated package.
+* [#273](https://github.com/shlinkio/shlink/issues/273) Improved code coverage in repository classes.
 
 #### Deprecated
 
diff --git a/composer.json b/composer.json
index 936e4d64..0c82a512 100644
--- a/composer.json
+++ b/composer.json
@@ -119,7 +119,7 @@
 
         "test:pretty": [
             "@test",
-            "phpcov merge build --html build/html"
+            "phpdbg -qrr vendor/bin/phpcov merge build --html build/html"
         ],
         "test:unit:pretty": "phpdbg -qrr vendor/bin/phpunit --coverage-html build/coverage --order-by=random",
 
diff --git a/indocker b/indocker
index 77a06d33..24b8094c 100755
--- a/indocker
+++ b/indocker
@@ -1,2 +1,9 @@
 #!/usr/bin/env bash
+
+# Run docker containers if they are not up yet
+if [[ $(docker ps | grep shlink_swoole) ]]; then :
+else
+    docker-compose up -d
+fi
+
 docker exec -it shlink_swoole /bin/sh -c "$*"
diff --git a/module/Core/src/Repository/TagRepository.php b/module/Core/src/Repository/TagRepository.php
index a4254cad..dec1d8e9 100644
--- a/module/Core/src/Repository/TagRepository.php
+++ b/module/Core/src/Repository/TagRepository.php
@@ -14,7 +14,7 @@ class TagRepository extends EntityRepository implements TagRepositoryInterface
      * @param array $names
      * @return int The number of affected entries
      */
-    public function deleteByName(array $names)
+    public function deleteByName(array $names): int
     {
         if (empty($names)) {
             return 0;
diff --git a/module/Core/src/Repository/TagRepositoryInterface.php b/module/Core/src/Repository/TagRepositoryInterface.php
index 23eb8128..0fa45cc3 100644
--- a/module/Core/src/Repository/TagRepositoryInterface.php
+++ b/module/Core/src/Repository/TagRepositoryInterface.php
@@ -13,5 +13,5 @@ interface TagRepositoryInterface extends ObjectRepository
      * @param array $names
      * @return int The number of affected entries
      */
-    public function deleteByName(array $names);
+    public function deleteByName(array $names): int;
 }
diff --git a/module/Core/test-func/Repository/ShortUrlRepositoryTest.php b/module/Core/test-func/Repository/ShortUrlRepositoryTest.php
index ed03816e..fa6ae825 100644
--- a/module/Core/test-func/Repository/ShortUrlRepositoryTest.php
+++ b/module/Core/test-func/Repository/ShortUrlRepositoryTest.php
@@ -76,6 +76,7 @@ class ShortUrlRepositoryTest extends DatabaseTestCase
         $this->getEntityManager()->flush();
 
         $this->assertEquals($count, $this->repo->countList());
+        $this->assertEquals($count, $this->repo->countList());
     }
 
     /**
@@ -92,7 +93,10 @@ class ShortUrlRepositoryTest extends DatabaseTestCase
         $this->getEntityManager()->persist($foo);
 
         $bar = new ShortUrl('bar');
-        $bar->setShortCode('bar_very_long_text');
+        $visit = new Visit($bar, Visitor::emptyInstance());
+        $this->getEntityManager()->persist($visit);
+        $bar->setShortCode('bar_very_long_text')
+            ->setVisits(new ArrayCollection([$visit]));
         $this->getEntityManager()->persist($bar);
 
         $foo2 = new ShortUrl('foo_2');
@@ -104,6 +108,22 @@ class ShortUrlRepositoryTest extends DatabaseTestCase
         $result = $this->repo->findList(null, null, 'foo', ['bar']);
         $this->assertCount(1, $result);
         $this->assertSame($foo, $result[0]);
+
+        $result = $this->repo->findList();
+        $this->assertCount(3, $result);
+
+        $result = $this->repo->findList(2);
+        $this->assertCount(2, $result);
+
+        $result = $this->repo->findList(2, 1);
+        $this->assertCount(2, $result);
+
+        $result = $this->repo->findList(2, 2);
+        $this->assertCount(1, $result);
+
+        $result = $this->repo->findList(null, null, null, [], ['visits' => 'DESC']);
+        $this->assertCount(3, $result);
+        $this->assertSame($bar, $result[0]);
     }
 
     /**
diff --git a/module/Core/test-func/Repository/VisitRepositoryTest.php b/module/Core/test-func/Repository/VisitRepositoryTest.php
index 452e543e..1841f5a0 100644
--- a/module/Core/test-func/Repository/VisitRepositoryTest.php
+++ b/module/Core/test-func/Repository/VisitRepositoryTest.php
@@ -82,6 +82,8 @@ class VisitRepositoryTest extends DatabaseTestCase
         $this->assertCount(4, $this->repo->findVisitsByShortCode($shortUrl->getShortCode(), new DateRange(
             Chronos::parse('2016-01-03')
         )));
+        $this->assertCount(3, $this->repo->findVisitsByShortCode($shortUrl->getShortCode(), null, 3, 2));
+        $this->assertCount(2, $this->repo->findVisitsByShortCode($shortUrl->getShortCode(), null, 5, 4));
     }
 
     /**