瀏覽代碼

Refactor MediaVideo après tests

François Drouhard 2 年之前
父節點
當前提交
c4bd3fcfcd
共有 2 個文件被更改,包括 140 次插入13 次删除
  1. 26 13
      src/Entity/MediaVideo.php
  2. 114 0
      tests/Entity/MediaVideoTest.php

+ 26 - 13
src/Entity/MediaVideo.php

@@ -40,6 +40,12 @@ class MediaVideo
     
     private $url;
 
+    public function setFromTmdb(string $type, string $identif): void
+    {
+        $this->setType($type);
+        $this->setIdentif($identif);
+    }
+
     public function getId(): ?int
     {
         return $this->id;
@@ -47,17 +53,18 @@ class MediaVideo
 
     public function getUrl(): ?string
     {
-        return $this->url;
+        return $this->url();
     }
 
-    public function setUrl($url): self
+    public function setUrl(string $url): self
     {
         $this->url = $url;
+        $this->extractIdentif();
 
         return $this;
     }
     
-    public function setType($type): self
+    public function setType(string $type): self
     {
         $this->type = $type;
 
@@ -69,7 +76,7 @@ class MediaVideo
         return $this->type;
     }
     
-    public function setIdentif($identif): self
+    public function setIdentif(string $identif): self
     {
         $this->identif = $identif;
 
@@ -126,13 +133,13 @@ class MediaVideo
         $this->setType('vimeo');  // signale qu’il s’agit d’une video vimeo et l’inscrit dans l’attribut $type
     }
 
-    #[ORM\PrePersist()] // Les trois événement suivant s’exécute avant que l’entité soit enregistée
-    #[ORM\PreUpdate()]
-    #[ORM\PreFlush()]
+    //#[ORM\PrePersist()] // Les trois événements suivant s’exécutent avant que l’entité soit enregistrée
+    //#[ORM\PreUpdate()]
+    //#[ORM\PreFlush()]
     
     public function extractIdentif(): void
     {
-        $url = $this->getUrl();  // on récupère l’url
+        $url = $this->url;  // on récupère l’url
 
         if (preg_match("#^(http|https)://video.fdlibre.eu/#", $url))  // Si c'est peertube fdlibre
         {
@@ -158,7 +165,7 @@ class MediaVideo
 
     }
 
-    private function embedUrl()
+    public function embedUrl(): ?string
     {
         $control = $this->getType();  // on récupère le type de la vidéo
         $id = strip_tags($this->getIdentif()); // on récupère son identifiant
@@ -175,10 +182,12 @@ class MediaVideo
         } else if ($control == 'peertube') {
             $embed = "https://video.fdlibre.eu/videos/embed/" . $id;
             return $embed;
+        } else {
+            return null;
         }
     }
 
-    private function url()
+    public function url(): ?string
     {
         $control = $this->getType();  // on récupère le type de la vidéo
         $id = strip_tags($this->getIdentif()); // on récupère son identifiant
@@ -195,11 +204,13 @@ class MediaVideo
         } else if ($control == 'peertube') {
             $embed = "https://video.fdlibre.eu/videos/watch/" . $id;
             return $embed;
+        } else {
+            return null;
         }
     }
 
 
-    public function image()
+    public function image(): ?string
     {
         $control = $this->getType();  // on récupère le type de la vidéo
         $id = strip_tags($this->getIdentif()); // on récupère son identifiant
@@ -214,13 +225,15 @@ class MediaVideo
             $hash = unserialize(file_get_contents("https://vimeo.com/api/v2/video/" . $id . ".php"));
             $image = $hash[0]['thumbnail_small'];
             return $image;
+        } else {
+            return null;
         }
     }
 
 
-    public function video()
+    public function video(): string
     {
-        $video = "<iframe src='".$this->embedUrl()."'  allowfullscreen></iframe>";
+        $video = "<iframe src='".$this->embedUrl()."' allowfullscreen></iframe>";
         return $video;
     }
 }

+ 114 - 0
tests/Entity/MediaVideoTest.php

@@ -0,0 +1,114 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Tests\Entity;
+
+use App\Entity\MediaVideo;
+use PHPUnit\Framework\TestCase;
+
+class MediaVideoTest extends TestCase
+{
+    /**
+     * @dataProvider urlsForMedia
+     */
+    public function testByUrl(
+        string $url,
+        string $expectedUrl,
+        string $expectedEmbedUrl,
+        string $expectedType,
+        string $expectedIdentif,
+        bool $isPathImageExists
+    ): void
+    {
+        $media = new MediaVideo();
+        $media->setUrl($url);
+        $this->assertEquals($expectedUrl, $media->getUrl());
+        $this->assertEquals($expectedEmbedUrl, $media->embedUrl());
+        $this->assertEquals($expectedType, $media->getType());
+        $this->assertEquals($expectedIdentif, $media->getIdentif());
+        $this->assertNull($media->getId());
+        $this->assertEquals("<iframe src='".$expectedEmbedUrl."' allowfullscreen></iframe>", $media->video());
+        if ($isPathImageExists) {
+            $this->assertIsString($media->image());
+        } else {
+            $this->assertNull($media->image());
+        }
+    }
+
+    /**
+     * @dataProvider urlsForMedia
+     */
+    public function testFromTmdb(
+        string $url,
+        string $expectedUrl,
+        string $expectedEmbedUrl,
+        string $expectedType,
+        string $expectedIdentif,
+        bool $isPathImageExists
+    ): void
+    {
+        $media2 = new MediaVideo();
+        $media2->setFromTmdb($expectedType, $expectedIdentif);
+        $this->assertEquals($expectedUrl, $media2->getUrl());
+        $this->assertEquals($expectedEmbedUrl, $media2->embedUrl());
+        $this->assertEquals($expectedType, $media2->getType());
+        $this->assertEquals($expectedIdentif, $media2->getIdentif());
+        $this->assertNull($media2->getId());
+        $this->assertEquals("<iframe src='".$expectedEmbedUrl."' allowfullscreen></iframe>", $media2->video());
+        if ($isPathImageExists) {
+            $this->assertIsString($media2->image());
+        } else {
+            $this->assertNull($media2->image());
+        }
+
+    }
+
+
+    public function urlsForMedia()
+    {
+        return [
+            [
+                'https://www.youtube.com/watch?v=jZm8eSX7MTc',
+                'https://www.youtube.com/watch?v=jZm8eSX7MTc',
+                'https://www.youtube-nocookie.com/embed/jZm8eSX7MTc',
+                'youtube',
+                'jZm8eSX7MTc',
+                true
+            ],
+            [
+                'https://www.dailymotion.com/video/x8efziw',
+                'https://www.dailymotion.com/video/x8efziw',
+                'https://www.dailymotion.com/embed/video/x8efziw',
+                'dailymotion',
+                'x8efziw',
+                true
+            ],
+            [
+                'https://vimeo.com/48130434',
+                'https://vimeo.com/48130434',
+                'https://player.vimeo.com/video/48130434',
+                'vimeo',
+                '48130434',
+                true
+            ],
+            [
+                'https://video.fdlibre.eu/videos/watch/eb38ccf6-62ce-4b52-874c-beaacc896612',
+                'https://video.fdlibre.eu/videos/watch/eb38ccf6-62ce-4b52-874c-beaacc896612',
+                'https://video.fdlibre.eu/videos/embed/eb38ccf6-62ce-4b52-874c-beaacc896612',
+                'peertube',
+                'eb38ccf6-62ce-4b52-874c-beaacc896612',
+                false
+            ],
+            [
+                'https://youtu.be/jZm8eSX7MTc',
+                'https://www.youtube.com/watch?v=jZm8eSX7MTc',
+                'https://www.youtube-nocookie.com/embed/jZm8eSX7MTc',
+                'youtube',
+                'jZm8eSX7MTc',
+                true
+            ],
+
+        ];
+    }
+
+}