2021-11-09 03:22:07 +08:00
< ? php
/*
* This file is part of Flarum .
*
* For detailed copyright and license information , please view the
* LICENSE file that was distributed with this source code .
*/
namespace Flarum\PackageManager\Tests\integration ;
use Flarum\Foundation\Paths ;
2021-11-10 00:38:47 +08:00
use Flarum\PackageManager\Composer\ComposerAdapter ;
2021-11-25 01:25:43 +08:00
use Flarum\PackageManager\Composer\ComposerJson ;
2021-11-09 03:22:07 +08:00
use Flarum\PackageManager\Extension\ExtensionUtils ;
use Flarum\Testing\integration\RetrievesAuthorizedUsers ;
use Illuminate\Support\Arr ;
use Psr\Http\Message\ResponseInterface ;
use Symfony\Component\Console\Input\StringInput ;
class TestCase extends \Flarum\Testing\integration\TestCase
{
use RetrievesAuthorizedUsers ;
protected function setUp () : void
{
parent :: setUp ();
$this -> extension ( 'flarum-package-manager' , 'flarum-tags' );
2021-11-25 01:25:43 +08:00
$tmp = realpath ( $this -> tmpDir ());
2021-11-09 03:22:07 +08:00
$this -> app () -> getContainer () -> instance ( 'flarum.paths' , new Paths ([
'base' => $tmp ,
'public' => " $tmp /public " ,
'storage' => " $tmp /storage " ,
'vendor' => " $tmp /vendor " ,
]));
}
protected function assertExtension ( string $id , bool $exists )
{
$installed = json_decode ( file_get_contents ( $this -> app () -> getContainer () -> make ( Paths :: class ) -> vendor . '/composer/installed.json' ), true );
$installedExtensions = Arr :: where ( $installed [ 'packages' ] ? ? $installed , function ( array $package ) {
return $package [ 'type' ] === 'flarum-extension' ;
});
$installedExtensionIds = array_map ( function ( string $name ) {
return ExtensionUtils :: nameToId ( $name );
}, Arr :: pluck ( $installedExtensions , 'name' ));
if ( $exists ) {
$this -> assertTrue ( in_array ( $id , $installedExtensionIds ), " Failed asserting that extension $id is installed " );
} else {
$this -> assertFalse ( in_array ( $id , $installedExtensionIds ), " Failed asserting that extension $id is not installed " );
}
}
protected function assertExtensionExists ( string $id )
{
$this -> assertExtension ( $id , true );
}
protected function assertExtensionNotExists ( string $id )
{
$this -> assertExtension ( $id , false );
}
2021-11-25 01:25:43 +08:00
protected function assertPackageVersion ( string $packageName , string $version )
{
$composerJson = $this -> app () -> getContainer () -> make ( ComposerJson :: class ) -> get ();
$this -> assertArrayHasKey ( $packageName , $composerJson [ 'require' ], " $packageName is not required. " );
$this -> assertEquals ( $version , $composerJson [ 'require' ][ $packageName ], " Expected $packageName to be $version , found { $composerJson [ 'require' ][ $packageName ] } instead. " );
}
2021-11-09 03:22:07 +08:00
protected function requireExtension ( string $package )
{
$this -> composer ( " require $package " );
}
protected function removeExtension ( string $package )
{
$this -> composer ( " remove $package " );
}
protected function composer ( string $command ) : void
{
2021-11-10 00:38:47 +08:00
/** @var ComposerAdapter $composer */
$composer = $this -> app () -> getContainer () -> make ( ComposerAdapter :: class );
$composer -> run ( new StringInput ( $command ));
2021-11-09 03:22:07 +08:00
}
2021-11-25 01:25:43 +08:00
protected function errorDetails ( ResponseInterface $response ) : array
2021-11-09 03:22:07 +08:00
{
2021-11-25 01:25:43 +08:00
$json = json_decode (( string ) $response -> getBody (), true );
2021-11-09 03:22:07 +08:00
2021-11-25 01:25:43 +08:00
return $json [ 'errors' ] ? ( $json [ 'errors' ][ 0 ] ? ? []) : [];
2021-11-09 03:22:07 +08:00
}
}