From 0bb364a3e4d56fb25be62f3654db4067f7c87a95 Mon Sep 17 00:00:00 2001 From: Morgan Newcomb Date: Fri, 27 Sep 2024 09:14:30 -0400 Subject: [PATCH] add unit test for the JW Player Adapter integration --- tests/Feature/JWPlayer7ForWPAdapterTest.php | 68 +++++++++++++++++++++ tests/Feature/JWPlayerAdapterTest.php | 22 ++++--- 2 files changed, 81 insertions(+), 9 deletions(-) create mode 100644 tests/Feature/JWPlayer7ForWPAdapterTest.php diff --git a/tests/Feature/JWPlayer7ForWPAdapterTest.php b/tests/Feature/JWPlayer7ForWPAdapterTest.php new file mode 100644 index 0000000..e6468ee --- /dev/null +++ b/tests/Feature/JWPlayer7ForWPAdapterTest.php @@ -0,0 +1,68 @@ +fake_request( 'https://api.jwplayer.com/v2/sites/example-api-key/media/*' ) + ->with_response_code( 200 ) + ->with_body( file_get_contents( __DIR__ . '/../Fixtures/jw-player-api-v2-media.json' ) ); + + // Create an instance of the adapter. + $sync_manager = Sync_Manager::init() + ->with_adapter( new JW_Player_7_For_WP() ) + ->with_callback( fn ( $video ) => self::factory()->post->create( + [ + 'post_title' => $video->metadata->title, + 'post_date' => DateTimeImmutable::createFromFormat( DATE_W3C, $video->created )->format( 'Y-m-d H:i:s' ), + 'post_modified' => DateTimeImmutable::createFromFormat( DATE_W3C, $video->last_modified )->format( 'Y-m-d H:i:s' ), + 'meta_input' => [ + 'jwplayer_id' => $video->id, + ], + ] + ) ); + + // Run the sync. + $sync_manager->sync_videos(); + + // Confirm that the sync was successful. + $video_query = new WP_Query( + [ + 'name' => 'example-video', + 'post_status' => 'publish', + 'post_type' => 'post', + ] + ); + $this->assertEquals( 1, $video_query->post_count ); + $this->assertEquals( 'Example Video', $video_query->posts[0]->post_title ); + $this->assertEquals( '2024-01-01 12:00:00', $video_query->posts[0]->post_date ); + $this->assertEquals( '2024-01-01 12:00:00', $video_query->posts[0]->post_date_gmt ); + $this->assertEquals( '2024-01-01 13:00:00', $video_query->posts[0]->post_modified ); + $this->assertEquals( '2024-01-01 13:00:00', $video_query->posts[0]->post_modified_gmt ); + $this->assertEquals( 'ABCD1234', get_post_meta( $video_query->posts[0]->ID, 'jwplayer_id', true ) ); + + // Ensure that the sync time was updated. + $this->assertEquals( '2024-01-01T13:00:00+00:00', get_option( Sync_Manager::LAST_SYNC_OPTION ) ); + } +} diff --git a/tests/Feature/JWPlayerAdapterTest.php b/tests/Feature/JWPlayerAdapterTest.php index bf0ea56..e0aa7f0 100644 --- a/tests/Feature/JWPlayerAdapterTest.php +++ b/tests/Feature/JWPlayerAdapterTest.php @@ -7,7 +7,8 @@ namespace Alley\WP\WP_Video_Sync\Tests\Feature; -use Alley\WP\WP_Video_Sync\Adapters\JW_Player_7_For_WP; +use Alley\WP\WP_Video_Sync\API\JW_Player_API; +use Alley\WP\WP_Video_Sync\Adapters\JW_Player; use Alley\WP\WP_Video_Sync\Sync_Manager; use Alley\WP\WP_Video_Sync\Tests\TestCase; use DateTimeImmutable; @@ -17,21 +18,23 @@ * A test suite for the JW Player adapter. */ class JWPlayerAdapterTest extends TestCase { + /** - * Tests the behavior of the adapter with the JW Player 7 for WP plugin (free and premium). + * Tests the behavior of the adapter with the JW Player. */ - public function test_jw_player_7_for_wp() { - // Fake the class that's used to make the API call. - require_once __DIR__ . '/../Mocks/JWPPP_Dashboard_API.php'; - + public function test_jw_player() { // Fake the API response for the first page of data. - $this->fake_request( 'https://api.jwplayer.com/v2/sites/example-api-key/media/*' ) + $this->fake_request( 'https://api.jwplayer.com/v2/sites/api_key/media*' ) ->with_response_code( 200 ) - ->with_body( file_get_contents( __DIR__ . '/../Fixtures/jw-player-api-v2-media.json' ) ); + ->with_file( __DIR__ . '/../Fixtures/jw-player-api-v2-media.json' ); // Create an instance of the adapter. $sync_manager = Sync_Manager::init() - ->with_adapter( new JW_Player_7_For_WP() ) + ->with_adapter( + new JW_Player( + new JW_Player_API( 'api_key', 'api_secret' ) + ) + ) ->with_callback( fn ( $video ) => self::factory()->post->create( [ 'post_title' => $video->metadata->title, @@ -54,6 +57,7 @@ public function test_jw_player_7_for_wp() { 'post_type' => 'post', ] ); + $this->assertEquals( 1, $video_query->post_count ); $this->assertEquals( 'Example Video', $video_query->posts[0]->post_title ); $this->assertEquals( '2024-01-01 12:00:00', $video_query->posts[0]->post_date );