Skip to content

Commit

Permalink
adjust tests
Browse files Browse the repository at this point in the history
Signed-off-by: dartcafe <[email protected]>
  • Loading branch information
dartcafe authored and artonge committed Apr 24, 2024
1 parent 561fda8 commit f0d4e09
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 119 deletions.
16 changes: 16 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@

use OCA\Polls\AppConstants;
use OCA\Polls\Dashboard\PollWidget;
use OCA\Polls\Db\CommentMapper;
use OCA\Polls\Db\LogMapper;
use OCA\Polls\Db\OptionMapper;
use OCA\Polls\Db\PollMapper;
use OCA\Polls\Db\SubscriptionMapper;
use OCA\Polls\Db\UserMapper;
use OCA\Polls\Db\VoteMapper;
use OCA\Polls\Event\CommentAddEvent;
use OCA\Polls\Event\CommentDeleteEvent;
use OCA\Polls\Event\CommentEvent;
Expand Down Expand Up @@ -64,14 +71,23 @@
use OCA\Polls\Listener\UserDeletedListener;
use OCA\Polls\Listener\VoteListener;
use OCA\Polls\Middleware\RequestAttributesMiddleware;
use OCA\Polls\Model\Settings\AppSettings;
use OCA\Polls\Notification\Notifier;
use OCA\Polls\Provider\SearchProvider;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Group\Events\GroupDeletedEvent;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\ISession;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\User\Events\UserDeletedEvent;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

/**
* @psalm-api
Expand Down
3 changes: 2 additions & 1 deletion lib/Controller/PollController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OCA\Polls\Service\PollService;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use OCP\Server;

/**
* @psalm-api
Expand All @@ -55,7 +56,7 @@ public function __construct(
*/
public function list(): JSONResponse {
return $this->response(function () {
$appSettings = new AppSettings;
$appSettings = Server::get(AppSettings::class);
return [
'list' => $this->pollService->list(),
'pollCreationAllowed' => $appSettings->getPollCreationAllowed(),
Expand Down
6 changes: 2 additions & 4 deletions lib/Cron/JanitorCron.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OCA\Polls\Model\Settings\AppSettings;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use OCP\Server;

/**
* @psalm-api
Expand All @@ -52,10 +53,7 @@ public function __construct(
) {
parent::__construct($time);
parent::setInterval(86400); // run once a day
$this->logMapper = $logMapper;
$this->pollMapper = $pollMapper;
$this->watchMapper = $watchMapper;
$this->appSettings = new AppSettings;
$this->appSettings = Server::get(AppSettings::class);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/Db/Share.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

use JsonSerializable;
use OCA\Polls\AppConstants;
use OCA\Polls\Helper\Container;
use OCA\Polls\Model\Settings\AppSettings;
use OCP\IURLGenerator;
use OCP\Server;

/**
* @method int getId()
Expand Down Expand Up @@ -145,8 +145,8 @@ public function __construct() {
$this->addType('locked', 'int');
$this->addType('reminderSent', 'int');
$this->addType('deleted', 'int');
$this->urlGenerator = Container::queryClass(IURLGenerator::class);
$this->appSettings = new AppSettings;
$this->urlGenerator = Server::get(IURLGenerator::class);
$this->appSettings = Server::get(AppSettings::class);
}

/**
Expand Down
19 changes: 6 additions & 13 deletions lib/Helper/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,27 @@
use OCA\Polls\Db\Share;
use OCA\Polls\Db\ShareMapper;
use OCP\App\IAppManager;
use OCP\AppFramework\App;
use OCP\IL10N;
use OCP\L10N\IFactory;
use Psr\Container\ContainerInterface;
use OCP\Server;

abstract class Container {
public static function getContainer(): ContainerInterface {
$app = new App(AppConstants::APP_ID);
return $app->getContainer();
}

public static function queryClass(string $class): mixed {
return self::getContainer()->get($class);
return Server::get($class);
}

public static function queryPoll(int $pollId): Poll {
return self::queryClass(PollMapper::class)->find($pollId);
return Server::get(PollMapper::class)->find($pollId);
}

public static function findShare(int $pollId, string $userId): Share {
return self::queryClass(ShareMapper::class)
->findByPollAndUser($pollId, $userId);
return Server::get(ShareMapper::class)->findByPollAndUser($pollId, $userId);
}

public static function getL10N(?string $lang = null): IL10N {
return self::queryClass(IFactory::class)->get(AppConstants::APP_ID, $lang);
return Server::get(IFactory::class)->get(AppConstants::APP_ID, $lang);
}
public static function isAppEnabled(string $app): bool {
return self::queryClass(IAppManager::class)->isEnabledForUser($app);
return Server::get(IAppManager::class)->isEnabledForUser($app);
}
}
1 change: 0 additions & 1 deletion lib/Model/Acl.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public function __construct(
private ?Share $share = null,
) {
$this->pollId = null;
$this->appSettings = new AppSettings;
}

/**
Expand Down
16 changes: 7 additions & 9 deletions lib/Model/Settings/AppSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

use JsonSerializable;
use OCA\Polls\AppConstants;
use OCA\Polls\Helper\Container;
use OCA\Polls\Model\Group\Group;
use OCP\IConfig;
use OCP\IGroupManager;
Expand Down Expand Up @@ -64,16 +63,15 @@ class AppSettings implements JsonSerializable {
public const SETTING_UPDATE_TYPE_PERIODIC_POLLING = 'periodicPolling';
public const SETTING_UPDATE_TYPE_DEFAULT = self::SETTING_UPDATE_TYPE_NO_POLLING;

private IConfig $config;
private IGroupManager $groupManager;
private IUserSession $session;
private string $userId = '';

public function __construct(
private IConfig $config,
private IGroupManager $groupManager,
private IUserSession $session,

public function __construct() {
$this->config = Container::queryClass(IConfig::class);
$this->session = Container::queryClass(IUserSession::class);
$this->userId = Container::queryClass(IUserSession::class)->getUser()?->getUId() ?? '';
$this->groupManager = Container::queryClass(IGroupManager::class);
) {
$this->userId = $this->session->getUser()?->getUId() ?? '';
}

// Getters
Expand Down
11 changes: 6 additions & 5 deletions lib/Model/UserBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IUserSession;
use OCP\Server;
use OCP\Share\IShare;

class UserBase implements \JsonSerializable {
Expand Down Expand Up @@ -89,11 +90,11 @@ public function __construct(
) {
$this->icon = 'icon-share';
$this->l10n = Container::getL10N();
$this->groupManager = Container::queryClass(IGroupManager::class);
$this->timeZone = Container::queryClass(IDateTimeZone::class);
$this->userMapper = Container::queryClass(UserMapper::class);
$this->userSession = Container::queryClass(IUserSession::class);
$this->appSettings = Container::queryClass(AppSettings::class);
$this->groupManager = Server::get(IGroupManager::class);
$this->timeZone = Server::get(IDateTimeZone::class);
$this->userMapper = Server::get(UserMapper::class);
$this->userSession = Server::get(IUserSession::class);
$this->appSettings = Server::get(AppSettings::class);
}

public function getId(): string {
Expand Down
6 changes: 2 additions & 4 deletions lib/Service/SettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@
use OCA\Polls\Model\Settings\AppSettings;

class SettingsService {
private AppSettings $appSettings;


/**
* @psalm-suppress PossiblyUnusedMethod
*/
public function __construct() {
$this->appSettings = new AppSettings;
public function __construct(private AppSettings $appSettings) {
}

/**
Expand Down
8 changes: 3 additions & 5 deletions lib/Service/WatchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,17 @@
use OCP\ISession;

class WatchService {
private AppSettings $appSettings;
private Watch $watch;


/**
* @psalm-suppress PossiblyUnusedMethod
*/
public function __construct(
private ISession $session,
private WatchMapper $watchMapper,
private Acl $acl,
private AppSettings $appSettings,
private Watch $watch,
) {
$this->appSettings = new AppSettings;
$this->watch = new Watch;
}

/**
Expand Down
14 changes: 9 additions & 5 deletions tests/Unit/Db/CommentMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,33 @@
namespace OCA\Polls\Tests\Unit\Db;

use League\FactoryMuffin\Faker\Facade as Faker;
use OCP\IDBConnection;
use OCA\Polls\Tests\Unit\UnitTestCase;

use OCA\Polls\Db\Comment;
use OCA\Polls\Db\CommentMapper;
use OCA\Polls\Db\Poll;
use OCA\Polls\Db\PollMapper;
use OCP\Server;

class CommentMapperTest extends UnitTestCase {
private IDBConnection $con;
private ISession $session;
private CommentMapper $commentMapper;
private PollMapper $pollMapper;
/** @var Poll[] $polls */
private array $polls = [];
/** @var Comment[] $comments */
private array $comments = [];

/**
* {@inheritDoc}
*/
protected function setUp(): void {
parent::setUp();
$this->con = Server::get(IDBConnection::class);
$this->commentMapper = new CommentMapper($this->con);
$this->pollMapper = new PollMapper($this->con);
$this->session = Server::get(ISession::class);
$this->session->set('ncPollsUserId', 'TestUser');

$this->commentMapper = Server::get(CommentMapper::class);
$this->pollMapper = Server::get(PollMapper::class);

$this->polls = [
$this->fm->instance('OCA\Polls\Db\Poll')
Expand Down
11 changes: 5 additions & 6 deletions tests/Unit/Db/LogMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,23 @@
use OCA\Polls\Db\LogMapper;
use OCA\Polls\Db\PollMapper;
use OCA\Polls\Tests\Unit\UnitTestCase;
use OCP\IDBConnection;
use OCP\Server;

class LogMapperTest extends UnitTestCase {
private IDBConnection $con;
private LogMapper $logMapper;
private PollMapper $pollMapper;
private array $polls = [];
/** @var Log[] $logs*/
private array $logs = [];
/** @var Poll[] $polls*/
private array $polls = [];

/**
* {@inheritDoc}
*/
protected function setUp(): void {
parent::setUp();
$this->con = Server::get(IDBConnection::class);
$this->logMapper = new LogMapper($this->con);
$this->pollMapper = new PollMapper($this->con);
$this->logMapper = Server::get(LogMapper::class);
$this->pollMapper = Server::get(PollMapper::class);

$this->polls = [
$this->fm->instance('OCA\Polls\Db\Poll')
Expand Down
26 changes: 5 additions & 21 deletions tests/Unit/Db/OptionMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,21 @@

namespace OCA\Polls\Db;

use OCP\IDBConnection;
use OCA\Polls\Tests\Unit\UnitTestCase;

use OCA\Polls\Db\PollMapper;
use OCA\Polls\Db\Option;
use OCA\Polls\Db\OptionMapper;
use OCP\IGroupManager;
use OCA\Polls\Db\Vote;
use OCA\Polls\Db\VoteMapper;
use OCP\ISession;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Server;
use Psr\Log\LoggerInterface;

class OptionMapperTest extends UnitTestCase {
private IDBConnection $con;
private IGroupManager $groupManager;
private ISession $session;
private IUserManager $userManager;
private IUserSession $userSession;
private LoggerInterface $logger;
private OptionMapper $optionMapper;
private VoteMapper $voteMapper;
private PollMapper $pollMapper;
private UserMapper $userMapper;
/** @var Poll[] $polls */
private array $polls = [];
/** @var Option[] $options */
Expand All @@ -62,19 +53,12 @@ class OptionMapperTest extends UnitTestCase {
*/
protected function setUp(): void {
parent::setUp();
$this->con = Server::get(IDBConnection::class);
$this->logger = Server::get(LoggerInterface::class);
$this->groupManager = Server::get(IGroupManager::class);
$this->session = Server::get(ISession::class);
$this->userManager = Server::get(IUserManager::class);
$this->userSession = Server::get(IUserSession::class);
$this->session->set('ncPollsUserId', 'TestUser');


$this->userMapper = new UserMapper($this->con, $this->groupManager, $this->session, $this->userSession, $this->userManager, $this->logger);
$this->voteMapper = new VoteMapper($this->con, $this->userMapper, $this->logger);
$this->optionMapper = new OptionMapper($this->con, $this->session, $this->userMapper);
$this->pollMapper = new PollMapper($this->con);
$this->voteMapper = Server::get(VoteMapper::class);
$this->optionMapper = Server::get(OptionMapper::class);
$this->pollMapper = Server::get(PollMapper::class);

$this->polls = [
$this->fm->instance('OCA\Polls\Db\Poll')
Expand Down
6 changes: 2 additions & 4 deletions tests/Unit/Db/PollMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,22 @@
namespace OCA\Polls\Tests\Unit\Db;

use League\FactoryMuffin\Faker\Facade as Faker;
use OCP\IDBConnection;
use OCA\Polls\Db\Poll;
use OCA\Polls\Db\PollMapper;
use OCA\Polls\Tests\Unit\UnitTestCase;
use OCP\Server;

class PollMapperTest extends UnitTestCase {
private IDBConnection $con;
private PollMapper $pollMapper;
/** @var Poll[] $polls*/
private array $polls = [];

/**
* {@inheritDoc}
*/
protected function setUp(): void {
parent::setUp();
$this->con = Server::get(IDBConnection::class);
$this->pollMapper = new PollMapper($this->con);
$this->pollMapper = Server::get(PollMapper::class);

$this->polls = [
$this->fm->instance('OCA\Polls\Db\Poll'),
Expand Down
Loading

0 comments on commit f0d4e09

Please sign in to comment.