Browse Source

Ajout tests

Clément K 4 years ago
parent
commit
27c1514140
2 changed files with 22 additions and 1 deletions
  1. 3 1
      src/Controller/HomeController.php
  2. 19 0
      tests/HomeTest.php

+ 3 - 1
src/Controller/HomeController.php

@@ -66,7 +66,9 @@ class HomeController extends AbstractController
                     }
                     $urls[0][$i] = "~[a-z]+://\S+~";
                 }
-                $text = preg_replace($urls[0], $urls[1], $text);
+                if (!empty($urls[0])) {
+                    $text = preg_replace($urls[0], $urls[1], $text);
+                }
                 for($i=0; $i < sizeof($mentions[0]); $i++) {
                     $user = $repository_profile->findOneBy(array('username' => $mentions[1][$i]));
                     if ($user) {

+ 19 - 0
tests/HomeTest.php

@@ -0,0 +1,19 @@
+<?php
+use PHPUnit\Framework\TestCase;
+class HomeTest extends TestCase
+{
+    public function testRegexMention()
+    {
+        $text = 'Salut @nathan';
+        $mentions = array();
+        preg_match_all("~@([a-zA-Z0-9_]*)~", $text, $mentions);
+        self::assertEquals($mentions[0][0], "@nathan");
+    }
+    public function testRegexHashtag()
+    {
+        $text = 'Salut #nathan';
+        $mentions = array();
+        preg_match_all("~#([a-zA-Z0-9_]*)~", $text, $mentions);
+        self::assertEquals($mentions[0][0], "#nathan");
+    }
+}