Skip to content

Commit

Permalink
added tests for sort function
Browse files Browse the repository at this point in the history
  • Loading branch information
Fellan-91 committed May 14, 2024
1 parent f1a2583 commit 7969052
Show file tree
Hide file tree
Showing 8 changed files with 324 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tests/phpunit/fixtures/sort/complex-sorted-asc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"id": "9",
"imgUrl": "test.jpeg",
"imgThumbnailUrl": "test-150x150.jpeg",
"title": "archer"
},
{
"id": "1",
"imgUrl": "archer.png",
"imgThumbnailUrl": "archer.png",
"title": "archer"
},
{
"id": "7",
"imgUrl": "archer.png",
"imgThumbnailUrl": "archer.png",
"title": "archer"
},
{
"id": "19",
"imgUrl": "test.gif",
"imgThumbnailUrl": "test-1024x679.jpg",
"title": "test"
},
{
"id": "37",
"imgUrl": "test.png",
"imgThumbnailUrl": "test.png",
"title": "test 1"
},
{
"id": "2",
"imgUrl": "trailer.png",
"imgThumbnailUrl": "trailer.png",
"title": "trailer"
},
{
"id": "25",
"imgUrl": "test.png",
"imgThumbnailUrl": "test.png",
"title": ""
},
{
"id": "10",
"imgUrl": "test.png",
"imgThumbnailUrl": "test.png",
"title": ""
}
]
50 changes: 50 additions & 0 deletions tests/phpunit/fixtures/sort/complex-sorted-desc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"id": "2",
"imgUrl": "trailer.png",
"imgThumbnailUrl": "trailer.png",
"title": "trailer"
},
{
"id": "37",
"imgUrl": "test.png",
"imgThumbnailUrl": "test.png",
"title": "test 1"
},
{
"id": "19",
"imgUrl": "test.gif",
"imgThumbnailUrl": "test-1024x679.jpg",
"title": "test"
},
{
"id": "7",
"imgUrl": "archer.png",
"imgThumbnailUrl": "archer.png",
"title": "archer"
},
{
"id": "1",
"imgUrl": "archer.png",
"imgThumbnailUrl": "archer.png",
"title": "archer"
},
{
"id": "9",
"imgUrl": "test.jpeg",
"imgThumbnailUrl": "test-150x150.jpeg",
"title": "archer"
},
{
"id": "25",
"imgUrl": "test.png",
"imgThumbnailUrl": "test.png",
"title": ""
},
{
"id": "10",
"imgUrl": "test.png",
"imgThumbnailUrl": "test.png",
"title": ""
}
]
50 changes: 50 additions & 0 deletions tests/phpunit/fixtures/sort/complex.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"id": "19",
"imgUrl": "test.gif",
"imgThumbnailUrl": "test-1024x679.jpg",
"title": "test"
},
{
"id": "9",
"imgUrl": "test.jpeg",
"imgThumbnailUrl": "test-150x150.jpeg",
"title": "archer"
},
{
"id": "25",
"imgUrl": "test.png",
"imgThumbnailUrl": "test.png",
"title": ""
},
{
"id": "37",
"imgUrl": "test.png",
"imgThumbnailUrl": "test.png",
"title": "test 1"
},
{
"id": "2",
"imgUrl": "trailer.png",
"imgThumbnailUrl": "trailer.png",
"title": "trailer"
},
{
"id": "10",
"imgUrl": "test.png",
"imgThumbnailUrl": "test.png",
"title": ""
},
{
"id": "1",
"imgUrl": "archer.png",
"imgThumbnailUrl": "archer.png",
"title": "archer"
},
{
"id": "7",
"imgUrl": "archer.png",
"imgThumbnailUrl": "archer.png",
"title": "archer"
}
]
20 changes: 20 additions & 0 deletions tests/phpunit/fixtures/sort/empty-sorted.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"id": "19",
"imgUrl": "test.gif",
"imgThumbnailUrl": "test-1024x679.jpg",
"title": ""
},
{
"id": "9",
"imgUrl": "test.jpeg",
"imgThumbnailUrl": "test-150x150.jpeg",
"title": ""
},
{
"id": "25",
"imgUrl": "test.png",
"imgThumbnailUrl": "test.png",
"title": ""
}
]
20 changes: 20 additions & 0 deletions tests/phpunit/fixtures/sort/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"id": "19",
"imgUrl": "test.gif",
"imgThumbnailUrl": "test-1024x679.jpg",
"title": ""
},
{
"id": "9",
"imgUrl": "test.jpeg",
"imgThumbnailUrl": "test-150x150.jpeg",
"title": ""
},
{
"id": "25",
"imgUrl": "test.png",
"imgThumbnailUrl": "test.png",
"title": ""
}
]
20 changes: 20 additions & 0 deletions tests/phpunit/fixtures/sort/equal-sorted.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"id": "25",
"imgUrl": "test.png",
"imgThumbnailUrl": "test.png",
"title": "test-title"
},
{
"id": "9",
"imgUrl": "test.jpeg",
"imgThumbnailUrl": "test-150x150.jpeg",
"title": "test-title"
},
{
"id": "19",
"imgUrl": "test.gif",
"imgThumbnailUrl": "test-1024x679.jpg",
"title": "test-title"
}
]
20 changes: 20 additions & 0 deletions tests/phpunit/fixtures/sort/equal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"id": "19",
"imgUrl": "test.gif",
"imgThumbnailUrl": "test-1024x679.jpg",
"title": "test-title"
},
{
"id": "9",
"imgUrl": "test.jpeg",
"imgThumbnailUrl": "test-150x150.jpeg",
"title": "test-title"
},
{
"id": "25",
"imgUrl": "test.png",
"imgThumbnailUrl": "test.png",
"title": "test-title"
}
]
94 changes: 94 additions & 0 deletions tests/phpunit/unit/test-sort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Tests for ClassSort
*
* @package Visual Portfolio
*/

/**
* Sort test case.
*/
class ClassSort extends WP_UnitTestCase {
/**
* Get fixture for test.
*
* @param string $filename - name of json file
* @return string
*/
public function get_json_array( $filename ) {
$filepath = dirname( dirname( __FILE__ ) ) . '/fixtures/sort/' . $filename . '.json';
$json = file_get_contents( $filepath );
// Decode the JSON file
$json_data = json_decode( $json, true );

return $json_data;
}

/**
* Test for sort array with equal elements.
*
* @return void
*/
public function test_sort_equal() {
// Read the JSON file
$equal_array = $this->get_json_array( 'equal' );
$equal_sorted_array = $this->get_json_array( 'equal-sorted' );
$sorted_array_asc = Visual_Portfolio_Get::sort_array_by_field( $equal_array, 'title', 'asc' );
$sorted_array_desc = Visual_Portfolio_Get::sort_array_by_field( $equal_array, 'title', 'desc' );

$this->assertEquals(
$equal_array,
$sorted_array_asc
);

$this->assertEquals(
$equal_sorted_array,
$sorted_array_desc
);
}

/**
* Test for sort array with empty elements.
*
* @return void
*/
public function test_sort_empty() {
$empty_array = $this->get_json_array( 'empty' );
$empty_sorted_array = $this->get_json_array( 'empty-sorted' );
$sorted_array_asc = Visual_Portfolio_Get::sort_array_by_field( $empty_array, 'title', 'asc' );
$sorted_array_desc = Visual_Portfolio_Get::sort_array_by_field( $empty_array, 'title', 'desc' );

$this->assertEquals(
$empty_sorted_array,
$sorted_array_asc
);

$this->assertEquals(
$empty_sorted_array,
$sorted_array_desc
);
}

/**
* Test for sort array with complex elements.
*
* @return void
*/
public function test_sort_complex() {
$complex_array = $this->get_json_array( 'complex' );
$complex_sorted_array_asc = $this->get_json_array( 'complex-sorted-asc' );
$complex_sorted_array_desc = $this->get_json_array( 'complex-sorted-desc' );
$sorted_array_asc = Visual_Portfolio_Get::sort_array_by_field( $complex_array, 'title', 'asc' );
$sorted_array_desc = Visual_Portfolio_Get::sort_array_by_field( $complex_array, 'title', 'desc' );

$this->assertEquals(
$complex_sorted_array_asc,
$sorted_array_asc
);

$this->assertEquals(
$complex_sorted_array_desc,
$sorted_array_desc
);
}
}

0 comments on commit 7969052

Please sign in to comment.