refactor: use more automated DI

This commit is contained in:
sualko 2021-05-28 17:34:53 +02:00
Родитель 9d5ff58b01
Коммит e35670038f
11 изменённых файлов: 81 добавлений и 166 удалений

Просмотреть файл

@ -199,25 +199,6 @@ class Application extends App
);
});
$container->registerService('PresenceHandler', function (IContainer $c) {
return new Presence(
$c->query('UserId'),
$c->query('Host'),
$c->query('PresenceMapper'),
$c->query('MessageMapper')
);
});
$container->registerService('MessageHandler', function (IContainer $c) {
return new Message(
$c->query('UserId'),
$c->query('Host'),
$c->query('MessageMapper'),
$c->query('UserProvider'),
$c->query(\OCP\ILogger::class)
);
});
/**
* Config values
*/
@ -248,29 +229,6 @@ class Application extends App
);
});
$container->registerService('RosterPush', function ($c) {
return new RosterPush(
$c->query('ServerContainer')->getUserManager(),
$c->query('ServerContainer')->getUserSession(),
$c->query('Host'),
$c->query('IQRosterPushMapper'),
$c->query('ServerContainer')->getDatabaseConnection(),
$c->query('UserProvider')
);
});
$container->registerService('UserHooks', function ($c) {
return new Hooks(
$c->query('ServerContainer')->getUserManager(),
$c->query('ServerContainer')->getUserSession(),
$c->query('RosterPush'),
$c->query('PresenceMapper'),
$c->query('StanzaMapper'),
$c->query('ServerContainer')->query('GroupManager')
);
});
$container->registerService('UserProvider', function (IContainer $c) {
if (self::contactsStoreApiSupported()) {
return new ContactsStoreUserProvider(
@ -304,47 +262,6 @@ class Application extends App
$c->query(\OCP\IConfig::class)
);
});
/**
* A modified userID for use in OJSXC.
* This is automatically made lowercase.
*/
$container->registerParameter(
'OJSXC_UserId',
self::sanitizeUserId(self::convertToRealUID($container->query('UserId')))
);
/**
* Raw request body
*/
$container->registerService('RawRequest', function ($c) {
return new RawRequest();
});
/**
* Data retriever
*/
$container->registerService('DataRetriever', function ($c) {
return new DataRetriever();
});
/**
* Migrations
*/
$container->registerService('OCA\OJSXC\Migration\RefreshRoster', function (IContainer $c) {
return new RefreshRosterMigration(
$c->query('RosterPush'),
$c->query(\OCP\IConfig::class),
$c->query(\OCP\ILogger::class)
);
});
$container->registerService('OCA\OJSXC\Migration\MigrateConfig', function (IContainer $c) {
return new MigrateConfig(
$c->query('Config')
);
});
}
/**

Просмотреть файл

@ -15,7 +15,6 @@ use OCA\OJSXC\StanzaHandlers\Presence as PresenceHandler;
use OCA\OJSXC\StanzaLogger;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\ILogger;
use OCP\IRequest;
use Sabre\Xml\Reader;
use Sabre\Xml\LibXMLException;
@ -105,9 +104,7 @@ class HttpBindController extends Controller
* @param StanzaMapper $stanzaMapper
* @param IQ $iqHandler
* @param Message $messageHandler
* @param string $host
* @param ILock $lock
* @param ILogger $logger
* @param PresenceHandler $presenceHandler
* @param PresenceMapper $presenceMapper
* @param string $body
@ -122,9 +119,7 @@ class HttpBindController extends Controller
StanzaMapper $stanzaMapper,
IQ $iqHandler,
Message $messageHandler,
$host,
ILock $lock,
ILogger $logger,
PresenceHandler $presenceHandler,
PresenceMapper $presenceMapper,
$body,

Просмотреть файл

@ -9,7 +9,6 @@ use OCP\IGroup;
use OCP\IUserManager;
use OCP\IUser;
use OCP\IUserSession;
class RosterPush
{
@ -24,13 +23,6 @@ class RosterPush
*/
private $iqRosterPushMapper;
private $host;
/**
* @var IUserSession
*/
private $userSession;
/**
* @var IDBConnection
*/
@ -45,23 +37,17 @@ class RosterPush
* RosterPush constructor.
*
* @param IUserManager $userManager
* @param IUserSession $userSession
* @param string $host
* @param IQRosterPushMapper $iqRosterPushMapper
* @param IDBConnection $db
* @param IUserProvider $userProvider
*/
public function __construct(
IUserManager $userManager,
IUserSession $userSession,
$host,
IQRosterPushMapper $iqRosterPushMapper,
IDbConnection $db,
IUserProvider $userProvider
) {
$this->userManager = $userManager;
$this->userSession = $userSession;
$this->host = $host;
$this->iqRosterPushMapper = $iqRosterPushMapper;
$this->db = $db;
$this->userProvider = $userProvider;

Просмотреть файл

@ -33,6 +33,9 @@ class IQ extends StanzaHandler
*/
private $userProvider;
/** @var string */
private $host;
/**
* IQ constructor.
*
@ -42,12 +45,13 @@ class IQ extends StanzaHandler
* @param IConfig $config
* @param IUserProvider $userProvider
*/
public function __construct($userId, $host, IUserManager $userManager, IConfig $config, IUserProvider $userProvider)
public function __construct($userId, string $host, IUserManager $userManager, IConfig $config, IUserProvider $userProvider)
{
parent::__construct($userId, $host);
parent::__construct($userId);
$this->userManager = $userManager;
$this->config = $config;
$this->userProvider = $userProvider;
$this->host = $host;
}

Просмотреть файл

@ -36,11 +36,6 @@ class Message extends StanzaHandler
*/
private $values;
/**
* @var string $msgId
*/
private $msgId;
/**
* @var ILogger $logger
*/
@ -50,13 +45,12 @@ class Message extends StanzaHandler
* Message constructor.
*
* @param string $userId
* @param string $host
* @param MessageMapper $messageMapper
* @param IUserProvider $userProvider
*/
public function __construct($userId, $host, MessageMapper $messageMapper, IUserProvider $userProvider, ILogger $logger)
public function __construct($userId, MessageMapper $messageMapper, IUserProvider $userProvider, ILogger $logger)
{
parent::__construct($userId, $host);
parent::__construct($userId);
$this->messageMapper = $messageMapper;
$this->userProvider = $userProvider;
$this->logger = $logger;

Просмотреть файл

@ -5,8 +5,6 @@ namespace OCA\OJSXC\StanzaHandlers;
use OCA\OJSXC\Db\MessageMapper;
use OCA\OJSXC\Db\PresenceMapper;
use OCA\OJSXC\Exceptions\TerminateException;
use Sabre\Xml\Reader;
use Sabre\Xml\Writer;
use OCA\OJSXC\Db\Presence as PresenceEntity;
/**
@ -31,13 +29,12 @@ class Presence extends StanzaHandler
* Presence constructor.
*
* @param $userId
* @param string $host
* @param PresenceMapper $presenceMapper
* @param MessageMapper $messageMapper
*/
public function __construct($userId, $host, PresenceMapper $presenceMapper, MessageMapper $messageMapper)
public function __construct($userId, PresenceMapper $presenceMapper, MessageMapper $messageMapper)
{
parent::__construct($userId, $host);
parent::__construct($userId);
$this->presenceMapper = $presenceMapper;
$this->messageMapper = $messageMapper;
}

Просмотреть файл

@ -9,7 +9,7 @@ use Sabre\Xml\Writer;
*
* @package OCA\OJSXC\StanzaHandlers
*/
class StanzaHandler
abstract class StanzaHandler
{
/**
@ -17,11 +17,6 @@ class StanzaHandler
*/
protected $userId;
/**
* @var string $host
*/
protected $host;
/**
* @var string $to
*/
@ -31,12 +26,10 @@ class StanzaHandler
* StanzaHandler constructor.
*
* @param string 1$userId
* @param string $host
*/
public function __construct($userId, $host)
public function __construct($userId)
{
$this->userId = $userId;
$this->host = $host;
}
/**

Просмотреть файл

@ -1,13 +1,14 @@
<?php
namespace OCA\OJSXC;
namespace OCA\OJSXC\Tests\Unit;
use OCA\OJSXC\Db\IQRosterPush;
use OCA\OJSXC\Db\IQRosterPushMapper;
use OCA\OJSXC\IUserProvider;
use OCA\OJSXC\RosterPush;
use OCP\IDBConnection;
use OCP\IGroup;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use PHPUnit\Framework\TestCase;
class RosterPushTest extends TestCase
@ -23,11 +24,6 @@ class RosterPushTest extends TestCase
*/
private $userManager;
/**
* @var \PHPUnit_Framework_MockObject_MockObject | IUserSession
*/
private $userSession;
/**
* @var \PHPUnit_Framework_MockObject_MockObject | IQRosterPushMapper
*/
@ -45,24 +41,23 @@ class RosterPushTest extends TestCase
public function setUp(): void
{
$this->userManager = $this->getMockBuilder('OCP\IUserManager')
/** @var IUserManager */
$this->userManager = $this->getMockBuilder(IUserManager::class)
->disableOriginalConstructor()->getMock();
$this->userSession = $this->getMockBuilder('OCP\IUserSession')
/** @var IQRosterPushMapper */
$this->iqRosterPushMapper = $this->getMockBuilder(IQRosterPushMapper::class)
->disableOriginalConstructor()->getMock();
$this->iqRosterPushMapper = $this->getMockBuilder('OCA\OJSXC\Db\IQRosterPushMapper')
/** @var IDbConnection */
$this->db = $this->getMockBuilder(IDbConnection::class)
->disableOriginalConstructor()->getMock();
$this->db = $this->getMockBuilder('OCP\IDbConnection')
->disableOriginalConstructor()->getMock();
$this->userProvider = $this->getMockBuilder('OCA\OJSXC\IUserProvider')->disableOriginalConstructor()->getMock();
/** @var IUserProvider */
$this->userProvider = $this->getMockBuilder(IUserProvider::class)->disableOriginalConstructor()->getMock();
$this->rosterPush = new RosterPush(
$this->userManager,
$this->userSession,
'localhost',
$this->iqRosterPushMapper,
$this->db,
$this->userProvider
@ -74,7 +69,7 @@ class RosterPushTest extends TestCase
/** @var \PHPUnit_Framework_MockObject_MockObject | RosterPush $rosterPush */
$rosterPush = $this->getMockBuilder('OCA\OJSXC\RosterPush')
->setConstructorArgs([$this->userManager, $this->userSession, 'host', $this->iqRosterPushMapper, $this->db, $this->userProvider])
->setConstructorArgs([$this->userManager, $this->iqRosterPushMapper, $this->db, $this->userProvider])
->setMethods(['createOrUpdateRosterItem', 'removeRosterItem'])->getMock();
$user1 = $this->getMockBuilder('OCP\IUser')->getMock();
@ -149,7 +144,7 @@ class RosterPushTest extends TestCase
/** @var \PHPUnit_Framework_MockObject_MockObject | RosterPush $rosterPush */
$rosterPush = $this->getMockBuilder('OCA\OJSXC\RosterPush')
->setConstructorArgs([$this->userManager, $this->userSession, 'host', $this->iqRosterPushMapper, $this->db, $this->userProvider])
->setConstructorArgs([$this->userManager, $this->iqRosterPushMapper, $this->db, $this->userProvider])
->setMethods(['createOrUpdateRosterItem', 'removeRosterItem'])->getMock();
$user1 = $this->getMockBuilder('OCP\IUser')->getMock();
@ -263,6 +258,7 @@ class RosterPushTest extends TestCase
->method('insert')
->with($stanza2);
/** @var IUser $user1 */
$this->rosterPush->createOrUpdateRosterItem($user1);
}

Просмотреть файл

@ -1,13 +1,22 @@
<?php
namespace OCA\OJSXC\Controller;
namespace OCA\OJSXC\Tests\Unit\Controller;
use OCA\OJSXC\Controller\HttpBindController;
use OCA\OJSXC\Db\Presence;
use OCA\OJSXC\Db\PresenceMapper;
use OCA\OJSXC\Db\Stanza;
use OCA\OJSXC\Db\StanzaMapper;
use OCA\OJSXC\Exceptions\TerminateException;
use OCA\OJSXC\Http\XMPPResponse;
use OCA\OJSXC\ILock;
use OCA\OJSXC\NewContentContainer;
use OCA\OJSXC\StanzaHandlers\IQ;
use OCA\OJSXC\StanzaHandlers\Message;
use OCA\OJSXC\StanzaHandlers\Presence as StanzaHandlersPresence;
use OCA\OJSXC\StanzaLogger;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\IRequest;
use PHPUnit\Framework\TestCase;
use Sabre\Xml\Writer;
use PHPUnit_Framework_MockObject_MockObject;
@ -85,17 +94,29 @@ class HttpBindControllerTest extends TestCase
*/
private function setUpController($requestBody)
{
$request = $this->getMockBuilder('OCP\IRequest')->disableOriginalConstructor()->getMock();
$this->stanzaMapper = $this->getMockBuilder('OCA\OJSXC\Db\StanzaMapper')->disableOriginalConstructor()->getMock();
$this->presenceMapper = $this->getMockBuilder('OCA\OJSXC\Db\PresenceMapper')->disableOriginalConstructor()->getMock();
/** @var IRequest */
$request = $this->getMockBuilder(IRequest::class)->disableOriginalConstructor()->getMock();
$this->iqHandler = $this->getMockBuilder('OCA\OJSXC\StanzaHandlers\IQ')->disableOriginalConstructor()->getMock();
$this->messageHandler = $this->getMockBuilder('OCA\OJSXC\StanzaHandlers\Message')->disableOriginalConstructor()->getMock();
$this->presenceHandler = $this->getMockBuilder('OCA\OJSXC\StanzaHandlers\Presence')->disableOriginalConstructor()->getMock();
$this->lock = $this->getMockBuilder('OCA\OJSXC\ILock')->disableOriginalConstructor()->getMock();
$this->newContentContainer = $this->getMockBuilder('OCA\OJSXC\NewContentContainer')->disableOriginalConstructor()->getMock();
/** @var StanzaMapper */
$this->stanzaMapper = $this->getMockBuilder(StanzaMapper::class)->disableOriginalConstructor()->getMock();
$logger = \OC::$server->getLogger();
/** @var PresenceMapper */
$this->presenceMapper = $this->getMockBuilder(PresenceMapper::class)->disableOriginalConstructor()->getMock();
/** @var IQ */
$this->iqHandler = $this->getMockBuilder(IQ::class)->disableOriginalConstructor()->getMock();
/** @var Message */
$this->messageHandler = $this->getMockBuilder(Message::class)->disableOriginalConstructor()->getMock();
/** @var StanzaHandlersPresence */
$this->presenceHandler = $this->getMockBuilder(StanzaHandlersPresence::class)->disableOriginalConstructor()->getMock();
/** @var ILock */
$this->lock = $this->getMockBuilder(ILock::class)->disableOriginalConstructor()->getMock();
/** @var NewContentContainer */
$this->newContentContainer = $this->getMockBuilder(NewContentContainer::class)->disableOriginalConstructor()->getMock();
$this->controller = new HttpBindController(
'ojsxc',
@ -104,9 +125,7 @@ class HttpBindControllerTest extends TestCase
$this->stanzaMapper,
$this->iqHandler,
$this->messageHandler,
'localhost',
$this->lock,
$logger,
$this->presenceHandler,
$this->presenceMapper,
$requestBody,

Просмотреть файл

@ -1,11 +1,12 @@
<?php
namespace OCA\OJSXC\StanzaHandlers;
namespace OCA\OJSXC\Tests\Unit\StanzaHandlers;
use OCA\OJSXC\Db\Message as MessageEntity;
use PHPUnit\Framework\TestCase;
use OCA\OJSXC\Db\MessageMapper;
use OCA\OJSXC\IUserProvider;
use OCA\OJSXC\StanzaHandlers\Message;
use OCP\ILogger;
use PHPUnit_Framework_MockObject_MockObject;
@ -46,10 +47,17 @@ class MessageTest extends TestCase
{
$this->host = 'localhost';
$this->userId = 'john';
$this->messageMapper = $this->getMockBuilder('OCA\OJSXC\Db\MessageMapper')->disableOriginalConstructor()->getMock();
$this->userProvider = $this->getMockBuilder('OCA\OJSXC\IUserProvider')->disableOriginalConstructor()->getMock();
$this->logger = $this->getMockBuilder('OCP\ILogger')->disableOriginalConstructor()->getMock();
$this->message = new Message($this->userId, $this->host, $this->messageMapper, $this->userProvider, $this->logger);
/** @var MessageMapper */
$this->messageMapper = $this->getMockBuilder(MessageMapper::class)->disableOriginalConstructor()->getMock();
/** @var IUserProvider */
$this->userProvider = $this->getMockBuilder(IUserProvider::class)->disableOriginalConstructor()->getMock();
/** @var ILogger */
$this->logger = $this->getMockBuilder(ILogger::class)->disableOriginalConstructor()->getMock();
$this->message = new Message($this->userId, $this->messageMapper, $this->userProvider, $this->logger);
}
public function messageProvider()

Просмотреть файл

@ -1,9 +1,11 @@
<?php
namespace OCA\OJSXC\StanzaHandlers;
namespace OCA\OJSXC\Tests\Unit\StanzaHandlers;
use OCA\OJSXC\Db\MessageMapper;
use OCA\OJSXC\StanzaHandlers\Presence;
use OCA\OJSXC\Db\Presence as PresenceEntity;
use OCA\OJSXC\Db\PresenceMapper;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;
@ -32,10 +34,14 @@ class PresenceTest extends TestCase
{
$this->host = 'localhost';
$this->userId = 'john';
$this->presenceMapper = $this->getMockBuilder('OCA\OJSXC\Db\PresenceMapper')->disableOriginalConstructor()->getMock();
$this->messageMapper = $this->getMockBuilder('OCA\OJSXC\Db\MessageMapper')->disableOriginalConstructor()->getMock();
$this->presence = new Presence($this->userId, $this->host, $this->presenceMapper, $this->messageMapper);
/** @var PresenceMapper */
$this->presenceMapper = $this->getMockBuilder(PresenceMapper::class)->disableOriginalConstructor()->getMock();
/** @var MessageMapper */
$this->messageMapper = $this->getMockBuilder(MessageMapper::class)->disableOriginalConstructor()->getMock();
$this->presence = new Presence($this->userId, $this->presenceMapper, $this->messageMapper);
}
public function handleProvider()