2015-12-24 14:24:18 +00:00
< ? php
2023-12-31 17:21:09 +00:00
namespace Tests\Wallabag\CoreBundle\Import ;
2015-12-24 14:24:18 +00:00
2022-09-01 18:54:56 +00:00
use Doctrine\ORM\EntityManager ;
use Doctrine\ORM\UnitOfWork ;
2017-07-01 07:52:38 +00:00
use M6Web\Component\RedisMock\RedisMockFactory ;
2015-12-30 11:23:51 +00:00
use Monolog\Handler\TestHandler ;
2017-07-01 07:52:38 +00:00
use Monolog\Logger ;
2017-12-16 21:17:42 +00:00
use PHPUnit\Framework\TestCase ;
2022-09-01 18:54:56 +00:00
use Predis\Client ;
2016-09-09 19:02:03 +00:00
use Simpleue\Queue\RedisQueue ;
2022-09-01 18:54:56 +00:00
use Symfony\Component\EventDispatcher\EventDispatcher ;
2024-02-08 22:23:11 +00:00
use Symfony\Component\HttpClient\MockHttpClient ;
use Symfony\Component\HttpClient\Response\MockResponse ;
2017-07-01 07:52:38 +00:00
use Wallabag\CoreBundle\Entity\Config ;
use Wallabag\CoreBundle\Entity\Entry ;
2023-12-30 22:32:36 +00:00
use Wallabag\CoreBundle\Entity\User ;
2022-09-01 18:54:56 +00:00
use Wallabag\CoreBundle\Helper\ContentProxy ;
use Wallabag\CoreBundle\Helper\TagsAssigner ;
2023-12-31 17:21:09 +00:00
use Wallabag\CoreBundle\Import\PocketImport ;
2023-12-31 20:13:25 +00:00
use Wallabag\CoreBundle\Redis\Producer ;
2022-09-01 18:54:56 +00:00
use Wallabag\CoreBundle\Repository\EntryRepository ;
2015-12-30 11:23:51 +00:00
2017-12-16 21:17:42 +00:00
class PocketImportTest extends TestCase
2015-12-24 14:24:18 +00:00
{
protected $token ;
protected $user ;
protected $em ;
2015-12-30 11:23:51 +00:00
protected $contentProxy ;
protected $logHandler ;
2017-05-27 20:08:14 +00:00
protected $tagsAssigner ;
protected $uow ;
2015-12-24 14:24:18 +00:00
public function testInit ()
{
$pocketImport = $this -> getPocketImport ();
2017-07-01 07:52:38 +00:00
$this -> assertSame ( 'Pocket' , $pocketImport -> getName ());
2015-12-31 10:24:46 +00:00
$this -> assertNotEmpty ( $pocketImport -> getUrl ());
2017-07-01 07:52:38 +00:00
$this -> assertSame ( 'import.pocket.description' , $pocketImport -> getDescription ());
2015-12-24 14:24:18 +00:00
}
public function testOAuthRequest ()
{
2024-02-08 22:23:11 +00:00
$mockHttpClient = new MockHttpClient ([ new MockResponse ( json_encode ([ 'code' => 'wunderbar_code' ]), [ 'response_headers' => [ 'Content-Type: application/json' ]])]);
2015-12-24 14:24:18 +00:00
$pocketImport = $this -> getPocketImport ();
2024-02-08 22:23:11 +00:00
$pocketImport -> setClient ( $mockHttpClient );
2015-12-24 14:24:18 +00:00
2015-12-30 11:23:51 +00:00
$code = $pocketImport -> getRequestToken ( 'http://0.0.0.0/redirect' );
2015-12-24 14:24:18 +00:00
2017-07-01 07:52:38 +00:00
$this -> assertSame ( 'wunderbar_code' , $code );
2015-12-30 11:23:51 +00:00
}
public function testOAuthRequestBadResponse ()
{
2024-02-08 22:23:11 +00:00
$mockHttpClient = new MockHttpClient ([ new MockResponse ( '' , [ 'http_code' => 403 ])]);
2015-12-30 11:23:51 +00:00
$pocketImport = $this -> getPocketImport ();
2024-02-08 22:23:11 +00:00
$pocketImport -> setClient ( $mockHttpClient );
2015-12-30 11:23:51 +00:00
$code = $pocketImport -> getRequestToken ( 'http://0.0.0.0/redirect' );
$this -> assertFalse ( $code );
$records = $this -> logHandler -> getRecords ();
2020-06-15 11:37:50 +00:00
$this -> assertStringContainsString ( 'PocketImport: Failed to request token' , $records [ 0 ][ 'message' ]);
2017-07-01 07:52:38 +00:00
$this -> assertSame ( 'ERROR' , $records [ 0 ][ 'level_name' ]);
2015-12-24 14:24:18 +00:00
}
public function testOAuthAuthorize ()
{
2024-02-08 22:23:11 +00:00
$mockHttpClient = new MockHttpClient ([ new MockResponse ( json_encode ([ 'access_token' => 'wunderbar_token' ]), [ 'response_headers' => [ 'Content-Type: application/json' ]])]);
2015-12-24 14:24:18 +00:00
$pocketImport = $this -> getPocketImport ();
2024-02-08 22:23:11 +00:00
$pocketImport -> setClient ( $mockHttpClient );
2015-12-24 14:24:18 +00:00
2015-12-30 11:23:51 +00:00
$res = $pocketImport -> authorize ( 'wunderbar_code' );
2015-12-24 14:24:18 +00:00
2015-12-30 11:23:51 +00:00
$this -> assertTrue ( $res );
2017-07-01 07:52:38 +00:00
$this -> assertSame ( 'wunderbar_token' , $pocketImport -> getAccessToken ());
2015-12-24 14:24:18 +00:00
}
2015-12-30 11:23:51 +00:00
public function testOAuthAuthorizeBadResponse ()
{
2024-02-08 22:23:11 +00:00
$mockHttpClient = new MockHttpClient ([ new MockResponse ( '' , [ 'http_code' => 403 ])]);
2015-12-30 11:23:51 +00:00
$pocketImport = $this -> getPocketImport ();
2024-02-08 22:23:11 +00:00
$pocketImport -> setClient ( $mockHttpClient );
2015-12-30 11:23:51 +00:00
$res = $pocketImport -> authorize ( 'wunderbar_code' );
$this -> assertFalse ( $res );
$records = $this -> logHandler -> getRecords ();
2020-06-15 11:37:50 +00:00
$this -> assertStringContainsString ( 'PocketImport: Failed to authorize client' , $records [ 0 ][ 'message' ]);
2017-07-01 07:52:38 +00:00
$this -> assertSame ( 'ERROR' , $records [ 0 ][ 'level_name' ]);
2015-12-30 11:23:51 +00:00
}
/**
* Will sample results from https :// getpocket . com / developer / docs / v3 / retrieve .
*/
2015-12-24 14:24:18 +00:00
public function testImport ()
{
2024-02-08 22:23:11 +00:00
$mockHttpClient = new MockHttpClient ([
new MockResponse ( json_encode ([ 'access_token' => 'wunderbar_token' ]), [ 'response_headers' => [ 'Content-Type: application/json' ]]),
new MockResponse ( <<< 'JSON'
2017-10-24 20:55:40 +00:00
{
" status " : 1 ,
" list " : {
" 229279689 " : {
" item_id " : " 229279689 " ,
" resolved_id " : " 229279689 " ,
" given_url " : " http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview " ,
" given_title " : " The Massive Ryder Cup Preview - The Triangle Blog - Grantland " ,
" favorite " : " 1 " ,
" status " : " 1 " ,
" time_added " : " 1473020899 " ,
" time_updated " : " 1473020899 " ,
" time_read " : " 0 " ,
" time_favorited " : " 0 " ,
" sort_id " : 0 ,
" resolved_title " : " The Massive Ryder Cup Preview " ,
" resolved_url " : " http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview " ,
" excerpt " : " The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them. " ,
" is_article " : " 1 " ,
" is_index " : " 0 " ,
" has_video " : " 1 " ,
" has_image " : " 1 " ,
" word_count " : " 3197 " ,
" images " : {
" 1 " : {
" item_id " : " 229279689 " ,
" image_id " : " 1 " ,
" src " : " http://a.espncdn.com/combiner/i?img=/photo/2012/0927/grant_g_ryder_cr_640.jpg&w=640&h=360 " ,
" width " : " 0 " ,
" height " : " 0 " ,
" credit " : " Jamie Squire/Getty Images " ,
" caption " : " "
2015-12-30 11:23:51 +00:00
}
},
2017-10-24 20:55:40 +00:00
" videos " : {
" 1 " : {
" item_id " : " 229279689 " ,
" video_id " : " 1 " ,
" src " : " http://www.youtube.com/v/Er34PbFkVGk?version=3&hl=en_US&rel=0 " ,
" width " : " 420 " ,
" height " : " 315 " ,
" type " : " 1 " ,
" vid " : " Er34PbFkVGk "
}
},
" tags " : {
" grantland " : {
" item_id " : " 1147652870 " ,
" tag " : " grantland "
},
" Ryder Cup " : {
" item_id " : " 1147652870 " ,
" tag " : " Ryder Cup "
}
2015-12-30 11:23:51 +00:00
}
2017-10-24 20:55:40 +00:00
},
" 229279690 " : {
" item_id " : " 229279689 " ,
" resolved_id " : " 229279689 " ,
" given_url " : " http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview " ,
" given_title " : " The Massive Ryder Cup Preview - The Triangle Blog - Grantland " ,
" favorite " : " 1 " ,
" status " : " 1 " ,
" time_added " : " 1473020899 " ,
" time_updated " : " 1473020899 " ,
" time_read " : " 0 " ,
" time_favorited " : " 0 " ,
" sort_id " : 1 ,
" resolved_title " : " The Massive Ryder Cup Preview " ,
" resolved_url " : " http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview " ,
" excerpt " : " The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them. " ,
" is_article " : " 1 " ,
" is_index " : " 0 " ,
" has_video " : " 0 " ,
" has_image " : " 0 " ,
" word_count " : " 3197 "
2015-12-30 11:23:51 +00:00
}
}
2017-10-24 20:55:40 +00:00
}
JSON
2024-02-08 22:23:11 +00:00
, [ 'response_headers' => [ 'Content-Type: application/json' ]]),
]);
2015-12-30 11:23:51 +00:00
2016-11-02 06:10:23 +00:00
$pocketImport = $this -> getPocketImport ( 'ConsumerKey' , 1 );
2015-12-30 11:23:51 +00:00
2022-09-01 18:54:56 +00:00
$entryRepo = $this -> getMockBuilder ( EntryRepository :: class )
2015-12-30 11:23:51 +00:00
-> disableOriginalConstructor ()
-> getMock ();
$entryRepo -> expects ( $this -> exactly ( 2 ))
2016-01-15 14:28:22 +00:00
-> method ( 'findByUrlAndUserId' )
2015-12-30 11:23:51 +00:00
-> will ( $this -> onConsecutiveCalls ( false , true ));
$this -> em
2016-02-19 13:22:20 +00:00
-> expects ( $this -> exactly ( 2 ))
2015-12-30 11:23:51 +00:00
-> method ( 'getRepository' )
2016-02-19 13:22:20 +00:00
-> willReturn ( $entryRepo );
2015-12-30 11:23:51 +00:00
2019-01-28 05:03:16 +00:00
$this -> em
-> expects ( $this -> any ())
-> method ( 'persist' )
-> with ( $this -> callback ( function ( $persistedEntry ) {
2020-06-15 11:37:50 +00:00
return ( bool ) $persistedEntry -> isArchived () && ( bool ) $persistedEntry -> isStarred ();
2019-01-28 05:03:16 +00:00
}));
2016-03-04 09:04:51 +00:00
$entry = new Entry ( $this -> user );
2015-12-30 11:23:51 +00:00
$this -> contentProxy
-> expects ( $this -> once ())
-> method ( 'updateEntry' )
-> willReturn ( $entry );
2024-02-08 22:23:11 +00:00
$pocketImport -> setClient ( $mockHttpClient );
2015-12-30 11:23:51 +00:00
$pocketImport -> authorize ( 'wunderbar_code' );
$res = $pocketImport -> import ();
$this -> assertTrue ( $res );
2017-07-01 07:52:38 +00:00
$this -> assertSame ([ 'skipped' => 1 , 'imported' => 1 , 'queued' => 0 ], $pocketImport -> getSummary ());
2015-12-30 11:23:51 +00:00
}
2016-03-04 09:04:51 +00:00
/**
* Will sample results from https :// getpocket . com / developer / docs / v3 / retrieve .
*/
public function testImportAndMarkAllAsRead ()
{
2024-02-08 22:23:11 +00:00
$mockHttpClient = new MockHttpClient ([
new MockResponse ( json_encode ([ 'access_token' => 'wunderbar_token' ]), [ 'response_headers' => [ 'Content-Type: application/json' ]]),
new MockResponse ( <<< 'JSON'
2017-10-24 20:55:40 +00:00
{
" status " : 1 ,
" list " : {
" 229279689 " : {
" item_id " : " 229279689 " ,
" resolved_id " : " 229279689 " ,
" given_url " : " http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview " ,
" given_title " : " The Massive Ryder Cup Preview - The Triangle Blog - Grantland " ,
" favorite " : " 1 " ,
" status " : " 1 " ,
" time_added " : " 1473020899 " ,
" time_updated " : " 1473020899 " ,
" time_read " : " 0 " ,
" time_favorited " : " 0 " ,
" sort_id " : 0 ,
" excerpt " : " The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them. " ,
" is_article " : " 1 " ,
" has_video " : " 1 " ,
" has_image " : " 1 " ,
" word_count " : " 3197 "
},
" 229279690 " : {
" item_id " : " 229279689 " ,
" resolved_id " : " 229279689 " ,
" given_url " : " http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview/2 " ,
" given_title " : " The Massive Ryder Cup Preview - The Triangle Blog - Grantland " ,
" favorite " : " 1 " ,
" status " : " 0 " ,
" time_added " : " 1473020899 " ,
" time_updated " : " 1473020899 " ,
" time_read " : " 0 " ,
" time_favorited " : " 0 " ,
" sort_id " : 1 ,
" excerpt " : " The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them. " ,
" is_article " : " 1 " ,
" has_video " : " 0 " ,
" has_image " : " 0 " ,
" word_count " : " 3197 "
2016-03-04 09:04:51 +00:00
}
}
2017-10-24 20:55:40 +00:00
}
JSON
2024-02-08 22:23:11 +00:00
, [ 'response_headers' => [ 'Content-Type: application/json' ]]),
]);
2016-03-04 09:04:51 +00:00
2016-11-02 06:10:23 +00:00
$pocketImport = $this -> getPocketImport ( 'ConsumerKey' , 2 );
2016-03-04 09:04:51 +00:00
2022-09-01 18:54:56 +00:00
$entryRepo = $this -> getMockBuilder ( EntryRepository :: class )
2016-03-04 09:04:51 +00:00
-> disableOriginalConstructor ()
-> getMock ();
$entryRepo -> expects ( $this -> exactly ( 2 ))
-> method ( 'findByUrlAndUserId' )
-> will ( $this -> onConsecutiveCalls ( false , false ));
$this -> em
-> expects ( $this -> exactly ( 2 ))
-> method ( 'getRepository' )
-> willReturn ( $entryRepo );
// check that every entry persisted are archived
$this -> em
-> expects ( $this -> any ())
-> method ( 'persist' )
2016-03-08 14:22:35 +00:00
-> with ( $this -> callback ( function ( $persistedEntry ) {
2020-06-15 11:37:50 +00:00
return ( bool ) $persistedEntry -> isArchived ();
2016-03-04 09:04:51 +00:00
}));
$entry = new Entry ( $this -> user );
$this -> contentProxy
-> expects ( $this -> exactly ( 2 ))
-> method ( 'updateEntry' )
-> willReturn ( $entry );
2024-02-08 22:23:11 +00:00
$pocketImport -> setClient ( $mockHttpClient );
2016-03-04 09:04:51 +00:00
$pocketImport -> authorize ( 'wunderbar_code' );
$res = $pocketImport -> setMarkAsRead ( true ) -> import ();
$this -> assertTrue ( $res );
2017-07-01 07:52:38 +00:00
$this -> assertSame ([ 'skipped' => 0 , 'imported' => 2 , 'queued' => 0 ], $pocketImport -> getSummary ());
2016-03-04 09:04:51 +00:00
}
2016-09-09 16:02:29 +00:00
/**
* Will sample results from https :// getpocket . com / developer / docs / v3 / retrieve .
*/
public function testImportWithRabbit ()
{
$body = <<< 'JSON'
{
" item_id " : " 229279689 " ,
" resolved_id " : " 229279689 " ,
" given_url " : " http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview " ,
" given_title " : " The Massive Ryder Cup Preview - The Triangle Blog - Grantland " ,
" favorite " : " 1 " ,
" status " : " 1 " ,
2016-09-09 18:45:30 +00:00
" time_added " : " 1473020899 " ,
" time_updated " : " 1473020899 " ,
" time_read " : " 0 " ,
" time_favorited " : " 0 " ,
" sort_id " : 0 ,
2016-09-09 16:02:29 +00:00
" resolved_title " : " The Massive Ryder Cup Preview " ,
" resolved_url " : " http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview " ,
" excerpt " : " The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them. " ,
" is_article " : " 1 " ,
" has_video " : " 0 " ,
" has_image " : " 0 " ,
" word_count " : " 3197 "
}
JSON ;
2024-02-08 22:23:11 +00:00
$mockHttpClient = new MockHttpClient ([
new MockResponse ( json_encode ([ 'access_token' => 'wunderbar_token' ]), [ 'response_headers' => [ 'Content-Type: application/json' ]]),
new MockResponse ( <<< JSON
2017-10-24 20:55:40 +00:00
{
" status " : 1 ,
" list " : {
" 229279690 " : $body
2016-09-09 16:02:29 +00:00
}
2017-10-24 20:55:40 +00:00
}
JSON
2024-02-08 22:23:11 +00:00
, [ 'response_headers' => [ 'Content-Type: application/json' ]]),
]);
2016-09-09 16:02:29 +00:00
$pocketImport = $this -> getPocketImport ();
2022-09-01 18:54:56 +00:00
$entryRepo = $this -> getMockBuilder ( EntryRepository :: class )
2016-09-09 16:02:29 +00:00
-> disableOriginalConstructor ()
-> getMock ();
$entryRepo -> expects ( $this -> never ())
-> method ( 'findByUrlAndUserId' );
$this -> em
-> expects ( $this -> never ())
-> method ( 'getRepository' );
$entry = new Entry ( $this -> user );
$this -> contentProxy
-> expects ( $this -> never ())
-> method ( 'updateEntry' );
2022-09-01 18:54:56 +00:00
$producer = $this -> getMockBuilder ( \OldSound\RabbitMqBundle\RabbitMq\Producer :: class )
2016-09-09 16:02:29 +00:00
-> disableOriginalConstructor ()
-> getMock ();
$bodyAsArray = json_decode ( $body , true );
// because with just use `new User()` so it doesn't have an id
$bodyAsArray [ 'userId' ] = null ;
$producer
-> expects ( $this -> once ())
-> method ( 'publish' )
-> with ( json_encode ( $bodyAsArray ));
2024-02-08 22:23:11 +00:00
$pocketImport -> setClient ( $mockHttpClient );
2016-09-09 19:02:03 +00:00
$pocketImport -> setProducer ( $producer );
2016-09-09 16:02:29 +00:00
$pocketImport -> authorize ( 'wunderbar_code' );
$res = $pocketImport -> setMarkAsRead ( true ) -> import ();
$this -> assertTrue ( $res );
2017-07-01 07:52:38 +00:00
$this -> assertSame ([ 'skipped' => 0 , 'imported' => 0 , 'queued' => 1 ], $pocketImport -> getSummary ());
2016-09-09 16:02:29 +00:00
}
2016-09-09 19:02:03 +00:00
/**
* Will sample results from https :// getpocket . com / developer / docs / v3 / retrieve .
*/
public function testImportWithRedis ()
{
$body = <<< 'JSON'
{
" item_id " : " 229279689 " ,
" resolved_id " : " 229279689 " ,
" given_url " : " http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview " ,
" given_title " : " The Massive Ryder Cup Preview - The Triangle Blog - Grantland " ,
" favorite " : " 1 " ,
" status " : " 1 " ,
" time_added " : " 1473020899 " ,
" time_updated " : " 1473020899 " ,
" time_read " : " 0 " ,
" time_favorited " : " 0 " ,
" sort_id " : 0 ,
" resolved_title " : " The Massive Ryder Cup Preview " ,
" resolved_url " : " http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview " ,
" excerpt " : " The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them. " ,
" is_article " : " 1 " ,
" has_video " : " 0 " ,
" has_image " : " 0 " ,
" word_count " : " 3197 "
}
JSON ;
2024-02-08 22:23:11 +00:00
$mockHttpClient = new MockHttpClient ([
new MockResponse ( json_encode ([ 'access_token' => 'wunderbar_token' ]), [ 'response_headers' => [ 'Content-Type: application/json' ]]),
new MockResponse ( <<< JSON
2017-10-24 20:55:40 +00:00
{
" status " : 1 ,
" list " : {
" 229279690 " : $body
2016-09-09 19:02:03 +00:00
}
2017-10-24 20:55:40 +00:00
}
JSON
2024-02-08 22:23:11 +00:00
, [ 'response_headers' => [ 'Content-Type: application/json' ]]),
]);
2016-09-09 19:02:03 +00:00
$pocketImport = $this -> getPocketImport ();
2022-09-01 18:54:56 +00:00
$entryRepo = $this -> getMockBuilder ( EntryRepository :: class )
2016-09-09 19:02:03 +00:00
-> disableOriginalConstructor ()
-> getMock ();
$entryRepo -> expects ( $this -> never ())
-> method ( 'findByUrlAndUserId' );
$this -> em
-> expects ( $this -> never ())
-> method ( 'getRepository' );
$entry = new Entry ( $this -> user );
$this -> contentProxy
-> expects ( $this -> never ())
-> method ( 'updateEntry' );
$factory = new RedisMockFactory ();
2022-09-01 18:54:56 +00:00
$redisMock = $factory -> getAdapter ( Client :: class , true );
2016-09-09 19:02:03 +00:00
$queue = new RedisQueue ( $redisMock , 'pocket' );
$producer = new Producer ( $queue );
2024-02-08 22:23:11 +00:00
$pocketImport -> setClient ( $mockHttpClient );
2016-09-09 19:02:03 +00:00
$pocketImport -> setProducer ( $producer );
$pocketImport -> authorize ( 'wunderbar_code' );
$res = $pocketImport -> setMarkAsRead ( true ) -> import ();
$this -> assertTrue ( $res );
2017-07-01 07:52:38 +00:00
$this -> assertSame ([ 'skipped' => 0 , 'imported' => 0 , 'queued' => 1 ], $pocketImport -> getSummary ());
2016-09-09 19:02:03 +00:00
$this -> assertNotEmpty ( $redisMock -> lpop ( 'pocket' ));
}
2015-12-30 11:23:51 +00:00
public function testImportBadResponse ()
{
2024-02-08 22:23:11 +00:00
$mockHttpClient = new MockHttpClient ([
new MockResponse ( json_encode ([ 'access_token' => 'wunderbar_token' ]), [ 'response_headers' => [ 'Content-Type: application/json' ]]),
new MockResponse ( '' , [ 'http_code' => 403 ]),
]);
2015-12-24 14:24:18 +00:00
$pocketImport = $this -> getPocketImport ();
2024-02-08 22:23:11 +00:00
$pocketImport -> setClient ( $mockHttpClient );
2015-12-30 11:23:51 +00:00
$pocketImport -> authorize ( 'wunderbar_code' );
$res = $pocketImport -> import ();
2015-12-24 14:24:18 +00:00
2015-12-30 11:23:51 +00:00
$this -> assertFalse ( $res );
2015-12-24 14:24:18 +00:00
2015-12-30 11:23:51 +00:00
$records = $this -> logHandler -> getRecords ();
2020-06-15 11:37:50 +00:00
$this -> assertStringContainsString ( 'PocketImport: Failed to import' , $records [ 0 ][ 'message' ]);
2017-07-01 07:52:38 +00:00
$this -> assertSame ( 'ERROR' , $records [ 0 ][ 'level_name' ]);
2015-12-24 14:24:18 +00:00
}
2016-08-19 21:52:19 +00:00
public function testImportWithExceptionFromGraby ()
{
2024-02-08 22:23:11 +00:00
$mockHttpClient = new MockHttpClient ([
new MockResponse ( json_encode ([ 'access_token' => 'wunderbar_token' ]), [ 'response_headers' => [ 'Content-Type: application/json' ]]),
new MockResponse ( <<< 'JSON'
2017-10-24 20:55:40 +00:00
{
" status " : 1 ,
" list " : {
" 229279689 " : {
" status " : " 1 " ,
" favorite " : " 1 " ,
" resolved_url " : " http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview "
2016-08-19 21:52:19 +00:00
}
}
2017-10-24 20:55:40 +00:00
}
2020-06-15 11:37:50 +00:00
2017-10-24 20:55:40 +00:00
JSON
2024-02-08 22:23:11 +00:00
, [ 'response_headers' => [ 'Content-Type: application/json' ]]),
]);
2016-08-19 21:52:19 +00:00
2016-11-02 06:10:23 +00:00
$pocketImport = $this -> getPocketImport ( 'ConsumerKey' , 1 );
2016-08-19 21:52:19 +00:00
2022-09-01 18:54:56 +00:00
$entryRepo = $this -> getMockBuilder ( EntryRepository :: class )
2016-08-19 21:52:19 +00:00
-> disableOriginalConstructor ()
-> getMock ();
$entryRepo -> expects ( $this -> once ())
-> method ( 'findByUrlAndUserId' )
-> will ( $this -> onConsecutiveCalls ( false , true ));
$this -> em
-> expects ( $this -> once ())
-> method ( 'getRepository' )
-> willReturn ( $entryRepo );
$entry = new Entry ( $this -> user );
$this -> contentProxy
-> expects ( $this -> once ())
-> method ( 'updateEntry' )
-> will ( $this -> throwException ( new \Exception ()));
2024-02-08 22:23:11 +00:00
$pocketImport -> setClient ( $mockHttpClient );
2016-08-19 21:52:19 +00:00
$pocketImport -> authorize ( 'wunderbar_code' );
$res = $pocketImport -> import ();
$this -> assertTrue ( $res );
2017-07-01 07:52:38 +00:00
$this -> assertSame ([ 'skipped' => 0 , 'imported' => 1 , 'queued' => 0 ], $pocketImport -> getSummary ());
}
private function getPocketImport ( $consumerKey = 'ConsumerKey' , $dispatched = 0 )
{
$this -> user = new User ();
$config = new Config ( $this -> user );
$config -> setPocketConsumerKey ( 'xxx' );
$this -> user -> setConfig ( $config );
2022-09-01 18:54:56 +00:00
$this -> contentProxy = $this -> getMockBuilder ( ContentProxy :: class )
2017-07-01 07:52:38 +00:00
-> disableOriginalConstructor ()
-> getMock ();
2022-09-01 18:54:56 +00:00
$this -> tagsAssigner = $this -> getMockBuilder ( TagsAssigner :: class )
2017-07-01 07:52:38 +00:00
-> disableOriginalConstructor ()
-> getMock ();
2022-09-01 18:54:56 +00:00
$this -> em = $this -> getMockBuilder ( EntityManager :: class )
2017-07-01 07:52:38 +00:00
-> disableOriginalConstructor ()
-> getMock ();
2022-09-01 18:54:56 +00:00
$this -> uow = $this -> getMockBuilder ( UnitOfWork :: class )
2017-07-01 07:52:38 +00:00
-> disableOriginalConstructor ()
-> getMock ();
$this -> em
-> expects ( $this -> any ())
-> method ( 'getUnitOfWork' )
-> willReturn ( $this -> uow );
$this -> uow
-> expects ( $this -> any ())
-> method ( 'getScheduledEntityInsertions' )
-> willReturn ([]);
2022-09-01 18:54:56 +00:00
$dispatcher = $this -> getMockBuilder ( EventDispatcher :: class )
2017-07-01 07:52:38 +00:00
-> disableOriginalConstructor ()
-> getMock ();
$dispatcher
-> expects ( $this -> exactly ( $dispatched ))
-> method ( 'dispatch' );
$this -> logHandler = new TestHandler ();
$logger = new Logger ( 'test' , [ $this -> logHandler ]);
2022-08-27 18:57:18 +00:00
$pocket = new PocketImport ( $this -> em , $this -> contentProxy , $this -> tagsAssigner , $dispatcher , $logger );
$pocket -> setUser ( $this -> user );
2017-07-01 07:52:38 +00:00
return $pocket ;
2016-08-19 21:52:19 +00:00
}
2015-12-24 14:24:18 +00:00
}