|
| 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