2017-11-11 07:22:27 +01:00
< ? php
namespace Todaymade\Daux\Tree ;
2015-11-11 00:09:47 +01:00
2017-11-11 07:22:27 +01:00
use PHPUnit\Framework\TestCase ;
2020-04-22 22:24:52 +02:00
use Todaymade\Daux\ConfigBuilder ;
2015-11-11 00:09:47 +01:00
2017-11-11 07:22:27 +01:00
class ContentTest extends TestCase
2015-11-11 00:09:47 +01:00
{
2016-07-27 21:32:51 +02:00
protected function createContent ( $content )
{
2019-12-05 21:25:58 +01:00
$config = ConfigBuilder :: withMode () -> build ();
2016-07-27 23:27:51 +02:00
$dir = new Directory ( new Root ( $config ), '' );
2015-11-11 00:09:47 +01:00
$obj = new Content ( $dir , '' );
$obj -> setContent ( $content );
return $obj ;
}
public function providerTestAttributes ()
{
2016-07-27 21:32:51 +02:00
return [
[ 'This is content' , [], 'This is content' ],
2016-07-28 00:14:26 +02:00
[ " --- \n title: This is a simple title \n --- \n This is content \n " , [ 'title' => 'This is a simple title' ], 'This is content' ],
[ " --- \n title: This is a simple title \n tags: \n - One \n - Second Tag \n --- \n This is content \n " , [ 'title' => 'This is a simple title' , 'tags' => [ 'One' , 'Second Tag' ]], 'This is content' ],
2016-07-27 21:32:51 +02:00
[ 'title: This is only metatada, no content' , [], 'title: This is only metatada, no content' ],
2016-07-28 00:14:26 +02:00
[ " --- \n title: This is almost only metadata \n --- \n " , [ 'title' => 'This is almost only metadata' ], '' ],
2016-04-28 23:33:26 +02:00
[ " # Some content \n \n hi \n ```yml \n value: true \n ``` \n ---- \n Follow up " , [], " # Some content \n \n hi \n ```yml \n value: true \n ``` \n ---- \n Follow up " ],
2016-07-27 21:32:51 +02:00
];
2015-11-11 00:09:47 +01:00
}
/**
* @ dataProvider providerTestAttributes
2020-04-22 22:24:52 +02:00
*
* @ param mixed $content
* @ param mixed $attributes
* @ param mixed $finalContent
2015-11-11 00:09:47 +01:00
*/
2016-04-28 23:33:26 +02:00
public function testAttributes ( $content , $attributes , $finalContent )
2015-11-11 00:09:47 +01:00
{
$obj = $this -> createContent ( $content );
$this -> assertEquals ( $attributes , $obj -> getAttribute ());
2016-04-28 23:33:26 +02:00
$this -> assertEquals ( $finalContent , trim ( $obj -> getContent ()));
2015-11-11 00:09:47 +01:00
}
public function testNoAttributes ()
{
2016-07-28 00:14:26 +02:00
$content = " This is a content with a separator \n --- \n this wasn't an attribute " ;
2015-11-11 00:09:47 +01:00
$obj = $this -> createContent ( $content );
$this -> assertEquals ( $content , $obj -> getContent ());
}
public function testContentPreserved ()
{
2016-07-28 00:14:26 +02:00
$content = " this was an attribute, but also a separator \n --- \n and it works " ;
$with_attribute = " --- \n title: a title \n --- \n $content " ;
2015-11-11 00:09:47 +01:00
$obj = $this -> createContent ( $with_attribute );
$this -> assertEquals ( $content , $obj -> getContent ());
$this -> assertEquals ( 'a title' , $obj -> getAttribute ( 'title' ));
}
}