2.0 #1

Merged
mathiaspalmqvist merged 10 commits from 2.0 into main 2025-07-04 19:22:40 +00:00
7 changed files with 121 additions and 4 deletions
Showing only changes of commit 6efcb252e1 - Show all commits

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
<?php
arch('it will not use debugging functions')
->expect(['dd', 'dump', 'ray'])
->each->not->toBeUsed();
+34
View File
@@ -0,0 +1,34 @@
<?php
use Mattitja\Cision\Cision;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
it('can fetch press releases from the RSS feed', function () {
Http::fake([
'news.cision.com/*' => Http::response(file_get_contents(__DIR__.'/Fixtures/rss-press.xml')),
]);
$cision = new Cision('test-company');
$data = $cision->press();
expect($data)->toBeArray();
expect($data)->toHaveKey('items');
expect($data['items'])->not->toBeEmpty();
$item = $data['items'][0];
expect($item)->toHaveKeys(['guid', 'title', 'description', 'link', 'created_at']);
});
it('can fetch and parse full article content', function () {
Http::fake([
'news.cision.com/*' => Http::response(file_get_contents(__DIR__.'/Fixtures/article.html')),
]);
$cision = new Cision();
$content = $cision->fetchContent('https://news.cision.com/se/example/r/test,c123456');
expect($content)->toHaveKeys(['header', 'published_at', 'content']);
expect($content['header'])->not->toBeEmpty();
expect($content['content'])->toContain('<p');
});
+12
View File
@@ -0,0 +1,12 @@
<article>
<div>
<h1>Test Article Header</h1>
<time pubdate="2025-07-04 07:55:00Z" datetime="2025-07-04 07:55:00Z">fre, jul 04, 2025 09:55 CET</time>
<p><strong>Lead:</strong> This is a test article paragraph.</p>
<p><em>Italic sentence.</em></p>
<ul>
<li><strong>Key:</strong> Value</li>
</ul>
</div>
</article>
+13
View File
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>Test Press Releases</title>
<item>
<guid>test-guid</guid>
<title>Test Press Title</title>
<description>This is a test press release description.</description>
<link>https://news.cision.com/se/example/r/test,c123456</link>
<pubDate>Fri, 04 Jul 2025 07:55:00 GMT</pubDate>
</item>
</channel>
</rss>
+5
View File
@@ -0,0 +1,5 @@
<?php
use Mattitja\Cision\Tests\TestCase;
uses(TestCase::class)->in(__DIR__);
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace Mattitja\Cision\Tests;
use Illuminate\Database\Eloquent\Factories\Factory;
use Mattitja\Cision\CisionServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;
class TestCase extends Orchestra
{
protected function setUp(): void
{
parent::setUp();
Factory::guessFactoryNamesUsing(
fn (string $modelName) => 'Mattitja\\Cision\\Database\\Factories\\'.class_basename($modelName).'Factory'
);
}
protected function getPackageProviders($app)
{
return [
CisionServiceProvider::class,
];
}
public function getEnvironmentSetUp($app)
{
config()->set('database.default', 'testing');
/*
foreach (\Illuminate\Support\Facades\File::allFiles(__DIR__ . '/database/migrations') as $migration) {
(include $migration->getRealPath())->up();
}
*/
}
}
+15 -4
View File
@@ -114,30 +114,41 @@ class Cision
return false;
}
// Suppress XML parsing warnings
libxml_use_internal_errors(true);
$xml = simplexml_load_string($data, "SimpleXMLElement", LIBXML_NOCDATA);
libxml_clear_errors();
if ($xml === false) {
return false;
}
// Convert XML to JSON then to array
$json = json_encode($xml);
$rows = json_decode($json, true);
// Validate structure
if (!isset($rows['channel']['item'])) {
return false;
}
$items = array_map(fn ($item) => [
// Ensure 'item' is always an array
$items = $rows['channel']['item'];
$items = isset($items[0]) ? $items : [$items];
// Format the result
$formatted = array_map(fn ($item) => [
'guid' => $item['guid'],
'title' => $item['title'],
'description' => $item['description'],
'link' => $item['link'],
'created_at' => Carbon::parse($item['pubDate'])->addHours(2)->format('Y-m-d H:i:s'),
], $rows['channel']['item']);
], $items);
return [
'items' => $items,
'items' => $formatted,
'previous_page' => $page > 1 ? $page - 1 : false,
'next_page' => count($items) === 24 ? $page + 1 : false,
'next_page' => count($formatted) === 24 ? $page + 1 : false,
];
}
}