Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
348 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
/.phpunit.result.cache | ||
/build/ | ||
/phpunit.xml | ||
/tests/export/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Astrotomic\Stancy\Contracts; | ||
|
||
interface ExportFactory | ||
{ | ||
public function addSheetList(array $list): void; | ||
|
||
public function addSheetCollectionName(string $name): void; | ||
|
||
public function addFeeds(array $except = []): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Astrotomic\Stancy\Facades; | ||
|
||
use Astrotomic\Stancy\Contracts\ExportFactory as ExportFactoryContract; | ||
use Illuminate\Support\Facades\Facade; | ||
|
||
/** | ||
* @method static void addSheetList(string[] $list) | ||
* @method static void addSheetCollectionName(string $name) | ||
* @method static void addFeeds(string[] $except = []) | ||
* | ||
* @see SitemapFactoryContract | ||
*/ | ||
class ExportFactory extends Facade | ||
{ | ||
protected static function getFacadeAccessor() | ||
{ | ||
return ExportFactoryContract::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Astrotomic\Stancy\Factories; | ||
|
||
use Astrotomic\Stancy\Contracts\ExportFactory as ExportFactoryContract; | ||
use Astrotomic\Stancy\Contracts\Page as PageContract; | ||
use Astrotomic\Stancy\Contracts\PageFactory as PageFactoryContract; | ||
use Astrotomic\Stancy\Traits\ConvertsSheetToPage; | ||
use Illuminate\Contracts\Routing\UrlGenerator as UrlGeneratorContract; | ||
use Illuminate\Support\Str; | ||
use Spatie\Export\Exporter; | ||
use Spatie\Sheets\Facades\Sheets; | ||
|
||
class ExportFactory implements ExportFactoryContract | ||
{ | ||
use ConvertsSheetToPage; | ||
|
||
/** @var Exporter */ | ||
protected $exporter; | ||
|
||
/** @var UrlGeneratorContract */ | ||
protected $urlGenerator; | ||
|
||
public function __construct(PageFactoryContract $pageFactory, Exporter $exporter, UrlGeneratorContract $urlGenerator) | ||
{ | ||
$this->pageFactory = $pageFactory; | ||
$this->exporter = $exporter; | ||
$this->urlGenerator = $urlGenerator; | ||
} | ||
|
||
/** | ||
* @param string[] $list | ||
* | ||
* @return void | ||
*/ | ||
public function addSheetList(array $list): void | ||
{ | ||
foreach ($list as $entry) { | ||
if (Str::contains($entry, ':')) { | ||
[$collection, $path] = explode(':', $entry); | ||
|
||
$this->addPages([$this->sheetToPage(Sheets::collection($collection)->get($path))]); | ||
|
||
continue; | ||
} | ||
|
||
$this->addPages($this->sheetsToPages(Sheets::collection($entry)->all()->all())); | ||
} | ||
} | ||
|
||
public function addSheetCollectionName(string $name): void | ||
{ | ||
$this->addSheetList([$name]); | ||
} | ||
|
||
public function addFeeds(array $except = []): void | ||
{ | ||
collect(config('feed.feeds'))->except($except)->each(function (array $config): void { | ||
$this->exporter->paths([$config['url']]); | ||
}); | ||
} | ||
|
||
/** | ||
* @param PageContract[] $pages | ||
* | ||
* @return void | ||
*/ | ||
protected function addPages(array $pages): void | ||
{ | ||
foreach ($pages as $page) { | ||
$this->exporter->paths([Str::replaceFirst($this->urlGenerator->to('/'), '', $page->getUrl())]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Astrotomic\Stancy\Traits; | ||
|
||
use Astrotomic\Stancy\Contracts\Page as PageContract; | ||
use Astrotomic\Stancy\Contracts\PageFactory as PageFactoryContract; | ||
use Spatie\Sheets\Sheet; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
trait ConvertsSheetToPage | ||
{ | ||
/** @var PageFactoryContract */ | ||
protected $pageFactory; | ||
|
||
/** | ||
* @param Sheet[] $sheets | ||
* | ||
* @return PageContract[] | ||
*/ | ||
protected function sheetsToPages(array $sheets): array | ||
{ | ||
return array_map(function (Sheet $sheet): PageContract { | ||
return $this->sheetToPage($sheet); | ||
}, $sheets); | ||
} | ||
|
||
protected function sheetToPage(Sheet $sheet): PageContract | ||
{ | ||
return $this->pageFactory->makeFromSheet($sheet); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
namespace Astrotomic\Stancy\Tests\Factories; | ||
|
||
use Astrotomic\Stancy\Contracts\Page as PageContract; | ||
use Astrotomic\Stancy\Facades\ExportFactory as ExportFactoryFacade; | ||
use Astrotomic\Stancy\Facades\PageFactory; | ||
use Astrotomic\Stancy\Factories\ExportFactory; | ||
use Astrotomic\Stancy\Tests\TestCase; | ||
use Carbon\Carbon; | ||
use Exception; | ||
use Illuminate\Support\Facades\Route; | ||
use Spatie\Export\Exporter; | ||
use Spatie\Snapshots\MatchesSnapshots; | ||
|
||
final class ExportFactoryTest extends TestCase | ||
{ | ||
use MatchesSnapshots; | ||
|
||
/** @test */ | ||
public function it_can_resolve_instance(): void | ||
{ | ||
static::assertInstanceOf(ExportFactory::class, $this->getExportFactory()); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_use_facade(): void | ||
{ | ||
ExportFactoryFacade::shouldReceive('addSheetList', 'addSheetCollectionName', 'addFeeds'); | ||
|
||
ExportFactoryFacade::addSheetCollectionName('blog'); | ||
ExportFactoryFacade::addSheetList(['blog:first-post', 'blog:second-post']); | ||
ExportFactoryFacade::addFeeds(); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_export_feeds(): void | ||
{ | ||
Carbon::setTestNow('2019-09-25 11:53:14'); | ||
|
||
Route::feeds(); | ||
|
||
$this->getExportFactory()->addFeeds(); | ||
|
||
$this->app->make(Exporter::class)->export(); | ||
|
||
// ToDo: https://github.com/spatie/laravel-export/issues/6 | ||
$filePath = __DIR__.'/../export/feed/blog.atom/index.html'; | ||
|
||
static::assertFileExists($filePath); | ||
static::assertMatchesXmlSnapshot(file_get_contents($filePath)); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_export_pages(): void | ||
{ | ||
Route::get('/', function (): PageContract { | ||
return PageFactory::makeFromSheetName('content', 'yamlFrontMatterPredefined'); | ||
}); | ||
|
||
$this->getExportFactory()->addSheetList(['content:yamlFrontMatterPredefined']); | ||
|
||
$this->app->make(Exporter::class)->export(); | ||
|
||
$filePath = __DIR__.'/../export/index.html'; | ||
|
||
static::assertFileExists($filePath); | ||
// ToDo: https://github.com/spatie/phpunit-snapshot-assertions/pull/76 | ||
// static::assertMatchesHtmlSnapshot(file_get_contents($filePath)); | ||
static::assertEquals('<h1>hello world</h1>', trim(file_get_contents($filePath))); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_export_a_collection(): void | ||
{ | ||
Route::get('/{slug}', function (string $slug): PageContract { | ||
return PageFactory::makeFromSheetName('blog', $slug); | ||
}); | ||
|
||
$this->getExportFactory()->addSheetCollectionName('blog'); | ||
|
||
$this->app->make(Exporter::class)->export(); | ||
|
||
$filePath1 = __DIR__.'/../export/first-post/index.html'; | ||
$filePath2 = __DIR__.'/../export/second-post/index.html'; | ||
|
||
static::assertFileExists($filePath1); | ||
// ToDo: https://github.com/spatie/phpunit-snapshot-assertions/pull/76 | ||
// static::assertMatchesHtmlSnapshot(file_get_contents($filePath1)); | ||
static::assertEquals('<h1>first post</h1>', trim(file_get_contents($filePath1))); | ||
|
||
static::assertFileExists($filePath2); | ||
// ToDo: https://github.com/spatie/phpunit-snapshot-assertions/pull/76 | ||
// static::assertMatchesHtmlSnapshot(file_get_contents($filePath2)); | ||
static::assertEquals('<h1>second post</h1>', trim(file_get_contents($filePath2))); | ||
} | ||
|
||
/** @test */ | ||
public function it_thorws_exception_if_page_data_is_not_instance_of_routable(): void | ||
{ | ||
static::expectException(Exception::class); | ||
static::expectExceptionMessage('The page data has to be instance of Astrotomic\Stancy\Contracts\Routable to allow access to the URL.'); | ||
|
||
$this->getExportFactory()->addSheetList(['content:home']); | ||
|
||
$this->app->make(Exporter::class)->export(); | ||
} | ||
} |
Oops, something went wrong.