Skip to content

Commit 906ff25

Browse files
committed
Add SPIO tests
1 parent 79a07f6 commit 906ff25

File tree

9 files changed

+759
-0
lines changed

9 files changed

+759
-0
lines changed

tests/Factory/DataFactory.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
namespace Tests\Factory;
3+
4+
/*
5+
abstract class CreateFactory
6+
{
7+
8+
abstract public function create();
9+
// abstract
10+
11+
public function get()
12+
{
13+
$data = $this->create();
14+
}
15+
16+
17+
}
18+
*/
19+
20+
interface DataFactory
21+
{
22+
23+
public function set($name, $value);
24+
public function returnData($format) ;
25+
public function reset();
26+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
namespace Factory\DataFactory;
3+
4+
class QuotaDataFactory implements \Tests\Factory\DataFactory
5+
{
6+
7+
private $type = 'spio';
8+
private $qdata;
9+
10+
public function __construct($type = 'spio')
11+
{
12+
$this->type = $type;
13+
$this->setData($type);
14+
}
15+
16+
private function setData($type)
17+
{
18+
if ($type == 'spio')
19+
$this->qdata = $this->createQuotaDataObject();
20+
elseif ($type == 'remote') {
21+
$this->qdata = $this->createRemoteQuotaArray();
22+
}
23+
24+
}
25+
26+
public function createQuotaDataObject()
27+
{
28+
$quota = (object) [
29+
'unlimited' => true,
30+
'monthly' => (object) [
31+
'text' => sprintf(__('%s/month', 'shortpixel-image-optimiser'), 100),
32+
'total' => 100,
33+
'consumed' => 10,
34+
'remaining' => 90,
35+
'renew' => 30,
36+
],
37+
'onetime' => (object) [
38+
'text' => '100',
39+
'total' => 100,
40+
'consumed' => 10,
41+
'remaining' => 90,
42+
],
43+
];
44+
45+
$quota->total = (object) [
46+
'total' => 200,
47+
'consumed' => 20,
48+
'remaining' => 180,
49+
];
50+
51+
return $quota;
52+
}
53+
54+
public function createRemoteQuotaArray()
55+
{
56+
57+
}
58+
59+
public function reset()
60+
{
61+
$this->setData($this->type);
62+
}
63+
64+
public function set($name, $value)
65+
{
66+
if (is_object($this->qdata))
67+
{
68+
if (property_exists($this->qdata, $name))
69+
{
70+
$this->qdata->$name = $value;
71+
}
72+
else {
73+
throw new Exception('SetData - Not existing');
74+
}
75+
}
76+
else { // @todo Implement return array for the remote Quota controller tests.
77+
throw new Exception('SetData - Not object');
78+
}
79+
80+
81+
return $this;
82+
}
83+
84+
public function returnData($format)
85+
{
86+
return clone $this->qdata;
87+
}
88+
89+
90+
}

tests/SPIOTestCase.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
namespace Tests;
3+
use WP_Mock\Tools\TestCase as TestCase;
4+
5+
use ShortPixel\ShortPixelPlugin as ShortPixelPlugin;
6+
use \WP_Mock;
7+
8+
abstract class SPIOTestCase extends TestCase
9+
{
10+
protected $backupGlobalsBlacklist = array('instance');
11+
12+
public function getClassObject()
13+
{
14+
if (method_exists(static::$class, 'getInstance'))
15+
{
16+
return static::$class::getInstance();
17+
}
18+
else {
19+
return new static::$class;
20+
}
21+
22+
trigger_error('GetclassObject did not return something');
23+
}
24+
25+
public function dumpVar($var)
26+
{
27+
fwrite(STDERR, var_export($var, TRUE));
28+
}
29+
30+
public function setUp() : void
31+
{
32+
parent::setUp();
33+
WP_Mock::userFunction('wpSPIO')->andReturn(ShortPixelPlugin::getInstance());
34+
}
35+
36+
}
37+
38+
trait testInstance
39+
{
40+
public function testGetInstance()
41+
{
42+
//WP_Mock::expectFilter('shortpixel/init/permissions');
43+
44+
$property = $this->getInaccessibleProperty(static::$class, 'instance');
45+
46+
$object = static::$class::getInstance();
47+
48+
$this->assertEquals(static::$class, get_class($object));
49+
$this->assertIsObject($object);
50+
51+
$this->assertNotNull($property->getValue());
52+
}
53+
}
54+
55+
// Testing this. So far didn't help much(?)
56+
/*
57+
trait WordPressFunction
58+
{
59+
public function wpSPIO()
60+
{
61+
return ShortPixelPlugin::getInstance();
62+
}
63+
}
64+
*/
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
namespace Tests\Controller;
3+
4+
use ShortPixel\Controller\FrontController;
5+
use Tests\SPIOTestCase;
6+
7+
/**
8+
* Class FrontControllerTest.
9+
*
10+
* @covers \ShortPixel\Controller\FrontController
11+
*/
12+
class FrontControllerTest extends SPIOTestCase
13+
{
14+
public function testConvertImgToPictureAddWebp(): void
15+
{
16+
17+
/** @todo This test is incomplete. */
18+
$this->markTestIncomplete();
19+
}
20+
21+
public function testStartOutputBuffer(): void
22+
{
23+
/** @todo This test is incomplete. */
24+
$this->markTestIncomplete();
25+
}
26+
27+
public function testConvert(): void
28+
{
29+
/** @todo This test is incomplete. */
30+
$this->markTestIncomplete();
31+
}
32+
33+
public function testTestInlineStyle(): void
34+
{
35+
/** @todo This test is incomplete. */
36+
$this->markTestIncomplete();
37+
}
38+
39+
public function testConvertInlineStyle(): void
40+
{
41+
/** @todo This test is incomplete. */
42+
$this->markTestIncomplete();
43+
}
44+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
namespace Tests\Controller;
3+
4+
use ShortPixel\Controller\QuotaController;
5+
use Tests\SPIOTestCase;
6+
7+
/**
8+
* Class QuotaControllerTest.
9+
*
10+
* @covers \ShortPixel\Controller\QuotaController
11+
*/
12+
class QuotaControllerTest extends SPIOTestCase
13+
{
14+
15+
public function testHasQuota(): void
16+
{
17+
/** @todo This test is incomplete. */
18+
$this->markTestIncomplete();
19+
}
20+
21+
public function testGetQuota(): void
22+
{
23+
/** @todo This test is incomplete. */
24+
$this->markTestIncomplete();
25+
}
26+
27+
public function testGetAvailableQuota(): void
28+
{
29+
/** @todo This test is incomplete. */
30+
$this->markTestIncomplete();
31+
}
32+
33+
public function testForceCheckRemoteQuota(): void
34+
{
35+
/** @todo This test is incomplete. */
36+
$this->markTestIncomplete();
37+
}
38+
39+
public function testRemoteValidateKey(): void
40+
{
41+
/** @todo This test is incomplete. */
42+
$this->markTestIncomplete();
43+
}
44+
45+
public function testSetQuotaExceeded(): void
46+
{
47+
/** @todo This test is incomplete. */
48+
$this->markTestIncomplete();
49+
}
50+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
namespace Tests\Model;
3+
//use WP_Mock\Tools\TestCase as TestCase;
4+
use \ShortPixel\Model\AccessModel as AccessModel;
5+
use ShortPixel\Controller\QuotaController as QuotaController;
6+
7+
use Factory\DataFactory\QuotaDataFactory as QuotaDataFactory;
8+
9+
use Tests\SPIOTestCase;
10+
use Tests\testInstance;
11+
use Tests\WordPressFunction;
12+
13+
//use function wpSPI;
14+
15+
use \WP_Mock;
16+
use \Mockery;
17+
18+
class AccessModelTest extends SPIOTestCase
19+
{
20+
use testInstance; // Instanced class, test it.
21+
// use WordPressFunction;
22+
23+
static $class = 'ShortPixel\Model\AccessModel';
24+
25+
public function testNoticeIsAllowed()
26+
{
27+
$fs = wpSPIO()->filesystem();
28+
29+
$notice = new \stdClass;
30+
$accessModel = $this->getClassObject();
31+
32+
$user = $this->wpUser();
33+
WP_Mock::userFunction('wp_get_current_user')->once()->andReturn($user);
34+
WP_Mock::userFunction('wp_get_current_user')->once()->andReturn($user);
35+
36+
$bool = $accessModel->noticeIsAllowed($notice);
37+
38+
$this->assertIsBool($bool);
39+
$this->assertTrue($bool);
40+
41+
$bool = $accessModel->noticeIsAllowed($notice);
42+
$this->assertIsBool($bool);
43+
$this->assertFalse($bool);
44+
}
45+
46+
public function testUserisAllowed()
47+
{
48+
$user = $this->wpUser();
49+
$accessModel = $this->getClassObject();
50+
51+
WP_Mock::userFunction('wp_get_current_user')->once()->andReturn($user);
52+
WP_Mock::userFunction('wp_get_current_user')->once()->andReturn($user);
53+
54+
$bool = $accessModel->userIsAllowed('test');
55+
56+
$this->assertIsBool($bool);
57+
$this->assertTrue($bool);
58+
59+
$bool = $accessModel->userIsAllowed('test');
60+
$this->assertIsBool($bool);
61+
$this->assertFalse($bool);
62+
}
63+
64+
public function testIsFeatureAvailable()
65+
{
66+
$quota = new \stdClass;
67+
68+
$mock = \Mockery::mock('overload:ShortPixel\Controller\QuotaController');
69+
$mock->shouldReceive('getInstance')->andReturn($mock);
70+
71+
$factory = new QuotaDataFactory('spio');
72+
$data1 = $factory->set('unlimited', 'HARK')->returnData('object');
73+
$data2 = $factory->set('unlimited', true)->returnData('object');
74+
75+
$mock->shouldReceive('getQuota')->once()->andReturn($data1);
76+
$mock->shouldReceive('getQuota')->once()->andReturn($data2);
77+
78+
$accessModel = $this->getClassObject();
79+
80+
// Test with something generally available / default
81+
$bool = $accessModel->isFeatureAvailable('webp');
82+
83+
$this->assertisBool($bool);
84+
$this->assertTrue($bool);
85+
86+
// Test with normal quota subscription - avif should be av.
87+
$bool = $accessModel->isFeatureAvailable('avif');
88+
$this->assertisBool($bool);
89+
$this->assertTrue($bool);
90+
91+
// Test with unlimited account - avif should *not* beincluded
92+
//
93+
$bool = $accessModel->isFeatureAvailable('avif');
94+
$this->assertisBool($bool);
95+
$this->assertFalse($bool);
96+
}
97+
98+
private function wpUser()
99+
{
100+
$user = \Mockery::mock(\WP_User::class);
101+
$user->shouldReceive('has_cap')->andReturn(true, false);
102+
return $user;
103+
}
104+
/*
105+
private function mockQuota
106+
{
107+
108+
} */
109+
110+
}

0 commit comments

Comments
 (0)