Skip to content

Commit

Permalink
Merge pull request #89 from iMattPro/add-tests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
iMattPro committed Mar 6, 2022
2 parents 99f1e50 + 154b2c1 commit 0a81e5c
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 0 deletions.
105 changes: 105 additions & 0 deletions tests/cron_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
*
* phpBB Media Embed PlugIn extension for the phpBB Forum Software package.
*
* @copyright (c) 2022 phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/

namespace phpbb\mediaembed\tests;

class cron_test extends \phpbb_test_case
{
/** @var \phpbb\config\config */
protected $config;

/** @var \phpbb\mediaembed\cron\purge_cache */
protected $cron_task;

/** @var \PHPUnit\Framework\MockObject\MockObject|\phpbb\mediaembed\cache\cache */
protected $cache;

protected function setUp(): void
{
parent::setUp();

$this->config = new \phpbb\config\config([]);
$this->cache = $this->getMockBuilder('\phpbb\mediaembed\cache\cache')
->disableOriginalConstructor()
->getMock();

$this->cron_task = new \phpbb\mediaembed\cron\purge_cache($this->config, $this->cache);
}

/**
* Test the cron task runs correctly
*/
public function test_run()
{
// Get the last_run
$last_run = $this->config['mediaembed_last_gc'];

// Test purge_cache() is called only once
$this->cache->expects(self::once())
->method('purge_mediaembed_cache');

// Run the cron task
$this->cron_task->run();

// Assert the autogroups_last_run value has been updated
self::assertNotEquals($last_run, $this->config['mediaembed_last_gc']);
}

/**
* Data set for test_should_run
*
* @return array Array of test data
*/
public function should_run_data()
{
return [
[time(), false],
[strtotime('23 hours ago'), false],
[strtotime('25 hours ago'), true],
['', true],
[0, true],
[null, true],
];
}

/**
* Test cron task should run after 24 hours
*
* @dataProvider should_run_data
*/
public function test_should_run($time, $expected)
{
// Set the last cron run time
$this->config['mediaembed_last_gc'] = $time;

// Assert we get the expected result from should_run()
self::assertSame($expected, $this->cron_task->should_run());
}

public function is_runnable_data()
{
return [
[true],
[false],
];
}

/**
* Test the cron task is runnable
*
* @dataProvider is_runnable_data
*/
public function test_is_runnable($expected)
{
$this->config['media_embed_enable_cache'] = $expected;

self::assertSame($expected, $this->cron_task->is_runnable());
}
}
55 changes: 55 additions & 0 deletions tests/custom_sites_collection_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace phpbb\mediaembed\tests;

use phpbb\extension\manager;
use phpbb\filesystem\filesystem;
use phpbb\mediaembed\collection\customsitescollection;
use PHPUnit\Framework\MockObject\MockObject;

class custom_sites_collection_test extends \phpbb_test_case
{
/** @var manager|MockObject */
protected $ext_manager;
protected $phpbb_root_path;
protected $php_ext;

protected function setUp(): void
{
parent::setUp();

global $phpbb_root_path, $phpEx;

$this->ext_manager = $this->getMockBuilder('\phpbb\extension\manager')
->disableOriginalConstructor()
->getMock();

$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $phpEx;
}

public function test_get_collection()
{
$finder = new \phpbb\finder(
new filesystem(),
$this->phpbb_root_path,
$this->getMockBuilder('\phpbb\cache\service')->disableOriginalConstructor()->getMock(),
$this->php_ext,
'_ext_finder'
);

$this->ext_manager->expects(self::once())
->method('get_finder')
->willReturn($finder);

$customsitescollection = new customsitescollection($this->ext_manager);

$collection = $customsitescollection->get_collection();

$this->assertContains('phpBB/ext/phpbb/mediaembed/collection/sites/codepen.yml', $collection);
$this->assertContains('phpBB/ext/phpbb/mediaembed/collection/sites/snotr.yml', $collection);
$this->assertContains('phpBB/ext/phpbb/mediaembed/collection/sites/ok.yml', $collection);
$this->assertContains('phpBB/ext/phpbb/mediaembed/collection/sites/videopress.yml', $collection);
$this->assertNotContains('phpBB/ext/phpbb/mediaembed/collection/sites/youtube.yml', $collection);
}
}

0 comments on commit 0a81e5c

Please sign in to comment.