5 Commits 5f9b61f4bf ... ce98e80836

Author SHA1 Message Date
  Hugo Sales ce98e80836 [TESTS] Raise App\Core\DB\UpdateListener test coverage to 100% 2 years ago
  Hugo Sales 75adf2e59f [TESTS] Change relevant tests to use GNUsocialTestCase, so they can access all the needed features 2 years ago
  Hugo Sales 31518f97ee [CORE] Clarify message when calling non existent method in Entity 2 years ago
  Hugo Sales dab822037c [TESTS] Merge datafixtures to allow for using the correct ID in notes, and add group_inbox 2 years ago
  Hugo Sales 79644d1e2b [TESTS] Add GNUsocialTestCase, which initializes our infrastructure when bootKernel is called 2 years ago

+ 2 - 2
src/Core/Entity.php

@@ -36,11 +36,11 @@ abstract class Entity
         if (Formatting::startsWith($name, 'has')) {
             $prop = Formatting::camelCaseToSnakeCase(Formatting::removePrefix($name, 'has'));
             // https://wiki.php.net/rfc/closure_apply#proposal
-            $private_property_accessor = function($prop) { return isset($this->{$prop}); };
+            $private_property_accessor = function ($prop) { return isset($this->{$prop}); };
             $private_property_accessor = $private_property_accessor->bindTo($this, get_called_class());
             return $private_property_accessor($prop);
         }
-        throw new \Exception("Entity::{$name} called with bogus arguments: " . print_r($arguments, true));
+        throw new \Exception('Non existent method ' . get_called_class() . "::{$name} called with arguments: " . print_r($arguments, true));
     }
 
     /**

+ 12 - 1
src/DataFixtures/NicknameFixtures.php

@@ -2,24 +2,35 @@
 
 namespace App\DataFixtures;
 
+use App\Entity\GroupInbox;
 use App\Entity\GSActor;
 use App\Entity\LocalGroup;
 use App\Entity\LocalUser;
+use App\Entity\Note;
 use App\Util\Nickname;
 use Doctrine\Bundle\FixturesBundle\Fixture;
 use Doctrine\Persistence\ObjectManager;
 
-class NicknameFixtures extends Fixture
+class CoreFixtures extends Fixture
 {
     public function load(ObjectManager $manager)
     {
+        $actors         = [];
+        $local_entities = [];
         foreach ([LocalUser::class => ['taken_user', 'setId'], LocalGroup::class => ['taken_group', 'setGroupId']] as $entity => [$nick, $method]) {
             $actor = GSActor::create(['nickname' => $nick, 'normalized_nickname' => Nickname::normalize($nick, check_already_used: false)]);
             $manager->persist($actor);
             $ent = $entity::create(['nickname' => $nick]);
             $ent->{$method}($actor->getId());
+            $local_entities[$nick] = $ent;
             $manager->persist($ent);
+            $actors[$nick] = $actor;
         }
+
+        $note = Note::create(['gsactor_id' => $actors['taken_user']->getId(), 'content' => 'some content']);
+        $manager->persist($note);
+
+        $manager->persist(GroupInbox::create(['group_id' => $local_entities['taken_group']->getGroupId(), 'activity_id' => $note->getId()]));
         $manager->flush();
     }
 }

+ 0 - 16
src/DataFixtures/NoteFixtures.php

@@ -1,16 +0,0 @@
-<?php
-
-namespace App\DataFixtures;
-
-use App\Entity\Note;
-use Doctrine\Bundle\FixturesBundle\Fixture;
-use Doctrine\Persistence\ObjectManager;
-
-class NoteFixtures extends Fixture
-{
-    public function load(ObjectManager $manager)
-    {
-        $manager->persist(Note::create(['gsactor_id' => 1, 'content' => 'some content']));
-        $manager->flush();
-    }
-}

+ 55 - 0
src/Util/GNUsocialTestCase.php

@@ -0,0 +1,55 @@
+<?php
+
+// {{{ License
+// This file is part of GNU social - https://www.gnu.org/software/social
+//
+// GNU social is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// GNU social is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with GNU social.  If not, see <http://www.gnu.org/licenses/>.
+// }}}
+
+/**
+ * String formatting utilities
+ *
+ * @package   GNUsocial
+ * @category  Util
+ *
+ * @author    Hugo Sales <hugo@hsal.es>
+ * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
+ * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
+ */
+
+namespace App\Util;
+
+use App\Core\GNUsocial;
+use Functional as F;
+use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
+
+class GNUsocialTestCase extends WebTestCase
+{
+    private static GNUsocial $social;
+
+    /**
+     * Provide our own initialization for testing
+     */
+    public static function bootKernel(array $options = [])
+    {
+        $kernel    = parent::bootKernel($options);
+        $container = self::$kernel->getContainer()->get('test.service_container');
+        $services  = F\map(
+            (new \ReflectionClass(GNUsocial::class))->getMethod('__construct')->getParameters(),
+            function ($p) use ($container) { return $container->get((string) $p->getType()); }
+        );
+        self::$social = new GNUsocial(...$services);
+        return $kernel;
+    }
+}

+ 20 - 19
tests/Core/DB/UpdateListenerTest.php

@@ -19,36 +19,24 @@
 
 namespace App\Tests\Core\DB;
 
+use App\Core\DB\DB;
 use App\Core\DB\UpdateListener;
-use App\Entity\GSActor;
+use App\Util\GNUsocialTestCase;
 use DateTime;
-use Doctrine\ORM\EntityManager;
+use Doctrine\ORM\EntityManagerInterface;
 use Doctrine\ORM\Event\PreUpdateEventArgs;
-use Doctrine\ORM\Mapping\ClassMetadata;
-use Doctrine\ORM\UnitOfWork;
-use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
 
-class UpdateListenerTest extends KernelTestCase
+class UpdateListenerTest extends GNUsocialTestCase
 {
-    public function testPreUpdate()
+    public function testPreUpdateExists()
     {
         static::bootKernel();
-        $actor = new GSActor();
+        $actor = DB::findOneBy('gsactor', ['nickname' => 'taken_user']);
         $date  = new DateTime('1999-09-23');
         $actor->setModified($date);
         static::assertSame($actor->getModified(), $date);
 
-        $em  = $this->createMock(EntityManager::class);
-        $uow = $this->createMock(UnitOfWork::class);
-        $em->expects(static::once())
-           ->method('getUnitOfWork')
-           ->willReturn($uow);
-
-        $md = $this->createMock(ClassMetadata::class);
-        $em->expects(static::once())
-           ->method('getClassMetadata')
-           ->willReturn($md);
-
+        $em         = static::$container->get(EntityManagerInterface::class);
         $change_set = [];
         $args       = new PreUpdateEventArgs($actor, $em, $change_set);
         $ul         = new UpdateListener();
@@ -56,4 +44,17 @@ class UpdateListenerTest extends KernelTestCase
 
         static::assertNotSame($actor->getModified(), $date);
     }
+
+    public function testPreUpdateDoesNotExist()
+    {
+        static::bootKernel();
+        $group_inbox = DB::dql('select gi from group_inbox gi join local_group lg with gi.group_id = lg.group_id where lg.nickname = :nickname', ['nickname' => 'taken_group'])[0];
+        static::assertTrue(!method_exists($group_inbox, 'setModified'));
+
+        $em         = static::$container->get(EntityManagerInterface::class);
+        $change_set = [];
+        $args       = new PreUpdateEventArgs($group_inbox, $em, $change_set);
+        $ul         = new UpdateListener();
+        static::assertFalse($ul->preUpdate($args));
+    }
 }

+ 2 - 9
tests/Twig/ExtensionTest.php

@@ -34,15 +34,13 @@
 namespace App\Tests\Twig;
 
 use App\Core\DB\DB;
-use App\Core\Event;
 use App\Twig\Extension;
 use App\Twig\Runtime;
+use App\Util\GNUsocialTestCase;
 use DirectoryIterator;
-use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
-use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 use Symfony\Component\HttpFoundation\Request;
 
-class ExtensionTest extends KernelTestCase
+class ExtensionTest extends GNUsocialTestCase
 {
     public function testIconsExtension()
     {
@@ -96,12 +94,7 @@ class ExtensionTest extends KernelTestCase
     public function testGetNoteActions()
     {
         static::bootKernel();
-        DB::setManager(self::$kernel->getContainer()->get('doctrine.orm.entity_manager'));
-        DB::initTableMap();
 
-        $container = self::$kernel->getContainer()->get('test.service_container');
-        $edi       = $container->get(EventDispatcherInterface::class);
-        Event::setDispatcher($edi);
         $req     = $this->createMock(Request::class);
         $runtime = new Runtime;
         $runtime->setRequest($req);

+ 2 - 14
tests/Util/CommonTest.php

@@ -20,24 +20,20 @@
 namespace App\Tests\Util;
 
 use App\Core\DB\DB;
-use App\Core\Event;
-use App\Core\Router\Router;
 use App\Core\Security;
 use App\Entity\GSActor;
 use App\Entity\LocalUser;
 use App\Util\Common;
 use App\Util\Exception\NoLoggedInUser;
+use App\Util\GNUsocialTestCase;
 use Doctrine\ORM\EntityManager;
 use Doctrine\ORM\Mapping\ClassMetadataFactory;
 use Doctrine\ORM\Mapping\ClassMetadataInfo;
 use Jchook\AssertThrows\AssertThrows;
-use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
 use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
-use Symfony\Component\EventDispatcher\EventDispatcherInterface;
-use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
 use Symfony\Component\Security\Core\Security as SSecurity;
 
-class CommonTest extends WebTestCase
+class CommonTest extends GNUsocialTestCase
 {
     use AssertThrows;
 
@@ -73,8 +69,6 @@ class CommonTest extends WebTestCase
     public function testUserAndActorGetters()
     {
         $client = static::createClient();
-        $sec    = $this->getMockBuilder(SSecurity::class)->setConstructorArgs([self::$kernel->getContainer()])->getMock();
-        Security::setHelper($sec, null);
         static::assertNull(Common::user());
         static::assertThrows(NoLoggedInUser::class, fn () => Common::ensureLoggedIn());
         static::assertFalse(Common::isLoggedIn());
@@ -112,12 +106,6 @@ class CommonTest extends WebTestCase
     {
         static::bootKernel();
 
-        $router           = static::$container->get('router');
-        $url_gen          = static::$container->get(UrlGeneratorInterface::class);
-        $event_dispatcher = static::$container->get(EventDispatcherInterface::class);
-        Router::setRouter($router, $url_gen);
-        Event::setDispatcher($event_dispatcher);
-
         static::assertTrue(Common::isSystemPath('login'));
         static::assertFalse(Common::isSystemPath('non-existent-path'));
     }

+ 2 - 7
tests/Util/NicknameTest.php

@@ -19,7 +19,6 @@
 
 namespace App\Tests\Util;
 
-use App\Core\DB\DB;
 use App\Entity\GSActor;
 use App\Util\Common;
 use App\Util\Exception\NicknameEmptyException;
@@ -28,12 +27,12 @@ use App\Util\Exception\NicknameReservedException;
 use App\Util\Exception\NicknameTakenException;
 use App\Util\Exception\NicknameTooLongException;
 use App\Util\Exception\NicknameTooShortException;
+use App\Util\GNUsocialTestCase;
 use App\Util\Nickname;
 use Jchook\AssertThrows\AssertThrows;
-use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
 use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
 
-class NicknameTest extends WebTestCase
+class NicknameTest extends GNUsocialTestCase
 {
     use AssertThrows;
 
@@ -57,8 +56,6 @@ class NicknameTest extends WebTestCase
         static::assertThrows(NicknameReservedException::class, fn () => Nickname::normalize('this_nickname_is_reserved', check_already_used: false));
 
         static::bootKernel();
-        DB::setManager(self::$kernel->getContainer()->get('doctrine.orm.entity_manager'));
-        DB::initTableMap();
         static::assertSame('foobar', Nickname::normalize('foobar', check_already_used: true));
         static::assertThrows(NicknameTakenException::class, fn () => Nickname::normalize('taken_user', check_already_used: true));
     }
@@ -95,8 +92,6 @@ class NicknameTest extends WebTestCase
     public function testCheckTaken()
     {
         static::bootKernel();
-        DB::setManager(self::$kernel->getContainer()->get('doctrine.orm.entity_manager'));
-        DB::initTableMap();
 
         static::assertNull(Nickname::checkTaken('not_taken_user'));
         static::assertTrue(Nickname::checkTaken('taken_user') instanceof GSActor);