Skip to content

Commit

Permalink
Update XMLTV:
Browse files Browse the repository at this point in the history
- rename into IO/Reader
- add loadXML()
  • Loading branch information
webeweb committed Aug 27, 2019
1 parent 132213f commit 8042cfe
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 49 deletions.
4 changes: 2 additions & 2 deletions doc/index.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
DOCUMENTATION
=============

Parse an XML file:
Read an XML file:

```php
/** @var Tv $tv */
$tv = XMLTV::parseXML("/path/to/file.xml");
$tv = Reader::readXML("/path/to/file.xml");

/** @var Channel $channel */
foreach($tv->getChannels() as $channel) {
Expand Down
73 changes: 73 additions & 0 deletions src/IO/Reader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/*
* This file is part of the xmltv-library package.
*
* (c) 2019 WEBEWEB
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace WBW\Library\XMLTV\IO;

use DOMDocument;
use Exception;
use GuzzleHttp\Client;
use Psr\Log\LoggerInterface;
use WBW\Library\XMLTV\Model\Tv;
use WBW\Library\XMLTV\Parser\Parser;
use WBW\Library\XMLTV\Parser\ParserHelper;

/**
* Reader.
*
* @author webeweb <https://github.com/webeweb/>
* @package WBW\Library\XMLTV\IO
*/
class Reader {

/**
* Load an XML file.
*
* @param string $url The URL.
* @param string $filename The filename The filename.
* @param LoggerInterface|null $logger The logger.
* @return Tv Returns the TV.
* @throws Exception Throws an exception if an error occurs.
*/
public static function loadXML($url, $filename, LoggerInterface $logger = null) {

$stream = fopen($filename, "w");

$client = new Client([
"headers" => [
"Accept" => "text/xml",
"User-Agent" => "webeweb/xmltv-library",
],
"save_to" => $stream,
"synchronous" => true,
]);

$client->request("GET", $url);

return static::readXML($filename, $logger);
}

/**
* Read an XML file.
*
* @param string $filename The filename.
* @param LoggerInterface $logger The logger.
* @return Tv Returns the TV.
*/
public static function readXML($filename, LoggerInterface $logger = null) {

$document = new DOMDocument();
$document->load($filename);

ParserHelper::setLogger($logger);

return Parser::parseTv($document->documentElement);
}
}
39 changes: 0 additions & 39 deletions src/XMLTV.php

This file was deleted.

40 changes: 32 additions & 8 deletions tests/XMLTVTest.php → tests/IO/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,53 @@
* file that was distributed with this source code.
*/

namespace WBW\Library\XMLTV\Tests;
namespace WBW\Library\XMLTV\Tests\IO;

use Exception;
use Psr\Log\LoggerInterface;
use WBW\Library\XMLTV\IO\Reader;
use WBW\Library\XMLTV\Model\Channel;
use WBW\Library\XMLTV\Model\Programme;
use WBW\Library\XMLTV\Model\Tv;
use WBW\Library\XMLTV\XMLTV;
use WBW\Library\XMLTV\Tests\AbstractTestCase;

/**
* XMLTV test.
* Reader test.
*
* @author webeweb <https://github.com/webeweb/>
* @package WBW\Library\XMLTV\Tests
* @package WBW\Library\XMLTV\Tests\IO
*/
class XMLTVTest extends AbstractTestCase {
class ReaderTest extends AbstractTestCase {

/**
* Tests tha parseXML() method.
* Tests the loadXML() method.
*
* @return void
* @throws Exception Throws an exception if an error occurs.
*/
public function testParseXML() {
public function testLoadXML() {

$res = XMLTV::parseXML($this->filename);
// Set a Logger mock.
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();

// Set an URL mock.
$url = "https://raw.githubusercontent.com/webeweb/xmltv-library/master/tests/Fixtures/xmltv.xml";

// Set a filename mock.
$filename = getcwd() . "/xmltv.xml";

$res = Reader::loadXML($url, $filename, $logger);
$this->assertInstanceOf(Tv::class, $res);
}

/**
* Tests tha readXML() method.
*
* @return void
*/
public function testReadXML() {

$res = Reader::readXML($this->filename);
$this->assertInstanceOf(Tv::class, $res);

$this->assertInstanceOf(Channel::class, $res->getChannels()[0]);
Expand Down

0 comments on commit 8042cfe

Please sign in to comment.