Skip to content

Commit

Permalink
add unit test for truncate_on_update()
Browse files Browse the repository at this point in the history
  • Loading branch information
Tabrisrp committed Jun 19, 2024
1 parent 6ed11db commit 6d5418f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

return [
'testShouldDoNothingWhenVersionUnder3.16.1' => [
'config' => [
'filter' => true,
'new_version' => '3.16.2',
'old_version' => '3.16.1',
'not_completed' => 0,
],
'expected' => false,
],
'testShouldTruncateWhenVersion3.16.1AndHigher' => [
'config' => [
'filter' => true,
'new_version' => '3.16.1',
'old_version' => '3.15.0',
'not_completed' => 0,
],
'expected' => true,
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Tests\Unit\inc\Engine\Media\AboveTheFold\Admin\Controller;

use Mockery;
use WP_Rocket\Engine\Media\AboveTheFold\Context\Context;
use WP_Rocket\Engine\Media\AboveTheFold\Database\Tables\AboveTheFold as ATFTable;
use WP_Rocket\Engine\Media\AboveTheFold\Database\Queries\AboveTheFold as ATFQuery;
use WP_Rocket\Engine\Media\AboveTheFold\Admin\Controller;
use WP_Rocket\Tests\Unit\TestCase;

/**
* Test class covering WP_Rocket\Engine\Media\AboveTheFold\Admin\Controller::truncate_atf
*
* @group ATF
*/
class TestTruncateOnUpdate extends TestCase {
private $controller;
private $query;
private $table;
private $context;

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

$this->query = $this->createMock( ATFQuery::class );
$this->table = $this->createMock( ATFTable::class );
$this->context = Mockery::mock( Context::class );
$this->controller = new Controller( $this->table, $this->query, $this->context );
}

/**
* @dataProvider configTestData
*/
public function testShouldDoExpected( $config, $expected ) {
$this->context->shouldReceive( 'is_allowed' )
->atMost()
->once()
->andReturn( $config['filter'] );

if ( ! $expected ) {
$this->query->expects( $this->never() )
->method( 'get_not_completed_count' );
} else {
$this->query->expects( $this->once() )
->method( 'get_not_completed_count' )
->willReturn( $config['not_completed'] );

$this->table->expects( $this->once() )
->method( 'truncate_atf_table' );
}

$this->controller->truncate_on_update( $config['new_version'], $config['old_version'] );
}
}

0 comments on commit 6d5418f

Please sign in to comment.