Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Moved truncenator to it’s own file, same for it’s tests. Set up a tes…

…t bootstrap to autoload stuff. Made template classes for Posse Helpers and their tests
  • Loading branch information...
commit ddae8294979d8cf0292e7b398cc940ad03291304 1 parent 5d42414
Barnaby Walters authored
84 BarnabyWalters/Helpers/Helpers.php
@@ -52,90 +52,6 @@ public static function authorFromUri($uri) {
52 52 }
53 53
54 54 /**
55   - * ~ THE TRUNCENATOR ~
56   - *
57   - * Takes a string (tweet-like note) and some config params, produces a truncated version to spec.
58   - *
59   - * @param string $string The string to be truncated
60   - * @param int $length The maximum length of the output
61   - * @param string $ellipsis The string to append in the case of truncation
62   - * @param string $uri The canonical URI of the post, to be added to the end
63   - * @param int $urilen Treat any URLs as if they were this length
64   - * @param bool $parens If trucation is not required, surround the canon. link with parens (())
65   - * @param int $hashtags The number of hashtags present in the text to preserve if trucation occurs
66   - *
67   - * @return string The truncated string
68   - * @todo A lot of this functionality is not properly implemented
69   - */
70   - public static function truncate($string, $length = 140, $uri = null, $urilen = null, $parens = true, $ellipsis = '…', $hastags = 1) {
71   - mb_internal_encoding('UTF-8');
72   -
73   - // Figure out total append length if truncation occurs
74   - $append = $ellipsis;
75   - if (!empty($uri))
76   - $append .= ' ' . $uri;
77   -
78   - // if $urilen is set, create array of URIs within the text and replace them with dummy text @ $urilen chars
79   - if (is_int($urilen)) {
80   - $uris = array();
81   - foreach (Helpers::findUrls($string, $tidy = false) as $key => $url) {
82   - $dummy = 'URL' . $key;
83   - $dummy .= str_repeat('X', $urilen - mb_strlen($dummy));
84   - $uris[$dummy] = $url;
85   - $string = str_replace($url, $dummy, $string);
86   - }
87   - }
88   -
89   - // Truncate string to nearest WB below that length
90   - $matches = array();
91   - $words = array();
92   - preg_match_all('/\b\w+\b/', $string, $matches, PREG_OFFSET_CAPTURE);
93   - foreach ($matches[0] as $match) {
94   - // For each match
95   - $words[] = array($match[1], $match[0]);
96   - }
97   - // $words = {[offset, 'string'], [offset, 'string'] •••}
98   -
99   - $maxplainlen = $length - Helpers::uriMbStrlen($append, $urilen);
100   -
101   - // See if truncation will happen
102   - if (Helpers::uriMbStrlen($string, $urilen) > $maxplainlen) {
103   - foreach ($words as $key => $word) {
104   - // Is the current word the first to cross $maxplainlen?
105   - if ($word[0] > $maxplainlen or $word[0] + mb_strlen($word[1]) > $maxplainlen) {
106   - // Yes. The current word and all words after it must be removed
107   - $plaintargetlen = $words[$key - 1][0] + mb_strlen($words[$key - 1][1]);
108   - break;
109   - }
110   - }
111   -
112   - if (!isset($plaintargetlen))
113   - $plaintargetlen = $maxplainlen;
114   -
115   - // Truncate string
116   - $truncatedplain = mb_substr($string, 0, $plaintargetlen);
117   -
118   - // Add the append
119   - $trunc = $truncatedplain . $append;
120   - }
121   - else {
122   - // If no trucation required, just append the URL
123   - // TODO: if adding the space and brackets will push over the edge, remove enough words to compensate
124   - // TODO: write edge-case test to cover that scenario
125   - $trunc = $string . ' (' . $uri . ')';
126   - }
127   -
128   - // if $urilen set, expand dummies into full URIs
129   - if (is_int($urilen)) {
130   - foreach ($uris as $dummy => $uri) {
131   - $trunc = str_replace($dummy, $uri, $trunc);
132   - }
133   - }
134   -
135   - return $trunc;
136   - }
137   -
138   - /**
139 55 * Replace <img> elements with their @href
140 56 *
141 57 * Finds all img elements and replaces them with the value of their @href.
14 BarnabyWalters/Posse/Helpers.php
... ... @@ -0,0 +1,14 @@
  1 +<?php
  2 +
  3 +namespace BarnabyWalters\Posse;
  4 +
  5 +/**
  6 + * Description of Helpers
  7 + *
  8 + * @author barnabywalters
  9 + */
  10 +class Helpers {
  11 +
  12 +}
  13 +
  14 +// EOF
99 BarnabyWalters/Posse/Truncenator.php
... ... @@ -0,0 +1,99 @@
  1 +<?php
  2 +
  3 +namespace BarnabyWalters\Posse;
  4 +
  5 +use BarnabyWalters\Helpers\Helpers as H;
  6 +
  7 +/**
  8 + * THE TRUNCENATOR
  9 + *
  10 + * @author Barnaby Walters
  11 + */
  12 +class Truncenator {
  13 +
  14 + /**
  15 + * ~ THE TRUNCENATOR ~
  16 + *
  17 + * Takes a string (tweet-like note) and some config params, produces a truncated version to spec.
  18 + *
  19 + * @param string $string The string to be truncated
  20 + * @param int $length The maximum length of the output
  21 + * @param string $ellipsis The string to append in the case of truncation
  22 + * @param string $uri The canonical URI of the post, to be added to the end
  23 + * @param int $urilen Treat any URLs as if they were this length
  24 + * @param bool $parens If trucation is not required, surround the canon. link with parens (())
  25 + * @param int $hashtags The number of hashtags present in the text to preserve if trucation occurs
  26 + *
  27 + * @return string The truncated string
  28 + * @todo A lot of this functionality is not properly implemented
  29 + */
  30 + public static function truncate($string, $length = 140, $uri = null, $urilen = null, $parens = true, $ellipsis = '…', $hastags = 1) {
  31 + mb_internal_encoding('UTF-8');
  32 +
  33 + // Figure out total append length if truncation occurs
  34 + $append = $ellipsis;
  35 + if (!empty($uri))
  36 + $append .= ' ' . $uri;
  37 +
  38 + // if $urilen is set, create array of URIs within the text and replace them with dummy text @ $urilen chars
  39 + if (is_int($urilen)) {
  40 + $uris = array();
  41 + foreach (H::findUrls($string, $tidy = false) as $key => $url) {
  42 + $dummy = 'URL' . $key;
  43 + $dummy .= str_repeat('X', $urilen - mb_strlen($dummy));
  44 + $uris[$dummy] = $url;
  45 + $string = str_replace($url, $dummy, $string);
  46 + }
  47 + }
  48 +
  49 + // Truncate string to nearest WB below that length
  50 + $matches = array();
  51 + $words = array();
  52 + preg_match_all('/\b\w+\b/', $string, $matches, PREG_OFFSET_CAPTURE);
  53 + foreach ($matches[0] as $match) {
  54 + // For each match
  55 + $words[] = array($match[1], $match[0]);
  56 + }
  57 + // $words = {[offset, 'string'], [offset, 'string'] •••}
  58 +
  59 + $maxplainlen = $length - H::uriMbStrlen($append, $urilen);
  60 +
  61 + // See if truncation will happen
  62 + if (H::uriMbStrlen($string, $urilen) > $maxplainlen) {
  63 + foreach ($words as $key => $word) {
  64 + // Is the current word the first to cross $maxplainlen?
  65 + if ($word[0] > $maxplainlen or $word[0] + mb_strlen($word[1]) > $maxplainlen) {
  66 + // Yes. The current word and all words after it must be removed
  67 + $plaintargetlen = $words[$key - 1][0] + mb_strlen($words[$key - 1][1]);
  68 + break;
  69 + }
  70 + }
  71 +
  72 + if (!isset($plaintargetlen))
  73 + $plaintargetlen = $maxplainlen;
  74 +
  75 + // Truncate string
  76 + $truncatedplain = mb_substr($string, 0, $plaintargetlen);
  77 +
  78 + // Add the append
  79 + $trunc = $truncatedplain . $append;
  80 + }
  81 + else {
  82 + // If no trucation required, just append the URL
  83 + // TODO: if adding the space and brackets will push over the edge, remove enough words to compensate
  84 + // TODO: write edge-case test to cover that scenario
  85 + $trunc = $string . ' (' . $uri . ')';
  86 + }
  87 +
  88 + // if $urilen set, expand dummies into full URIs
  89 + if (is_int($urilen)) {
  90 + foreach ($uris as $dummy => $uri) {
  91 + $trunc = str_replace($dummy, $uri, $trunc);
  92 + }
  93 + }
  94 +
  95 + return $trunc;
  96 + }
  97 +}
  98 +
  99 +// EOF
6 nbproject/project.properties
... ... @@ -1,8 +1,8 @@
1 1 include.path=${php.global.include.path}
2 2 php.version=PHP_54
3   -phpunit.bootstrap=
4   -phpunit.bootstrap.create.tests=false
5   -phpunit.configuration=
  3 +phpunit.bootstrap=tests/bootstrap.php
  4 +phpunit.bootstrap.create.tests=true
  5 +phpunit.configuration=phpunit.xml
6 6 phpunit.run.test.files=true
7 7 phpunit.script=
8 8 phpunit.suite=
21 phpunit.xml
... ... @@ -1,4 +1,5 @@
1 1 <phpunit backupGlobals="true"
  2 + bootstrap="Tests/bootstrap.php"
2 3 backupStaticAttributes="false"
3 4 cacheTokens="false"
4 5 colors="false"
@@ -15,14 +16,14 @@
15 16 stopOnSkipped="false"
16 17 strict="false"
17 18 verbose="false">
18   - <testsuites>
19   - <testsuite name="Test Suite">
20   - <directory suffix="Test.php">tests/</directory>
21   - </testsuite>
22   - </testsuites>
23   - <logging>
24   - <log type="coverage-html" target="tmp/report" charset="UTF-8"
25   - highlight="true"/>
26   - <log type="testdox-html" target="tmp/index.html"/>
27   - </logging>
  19 + <testsuites>
  20 + <testsuite name="Test Suite">
  21 + <directory suffix="Test.php">Tests/</directory>
  22 + </testsuite>
  23 + </testsuites>
  24 + <logging>
  25 + <log type="coverage-html" target="tmp/report" charset="UTF-8"
  26 + highlight="true"/>
  27 + <log type="testdox-html" target="tmp/index.html"/>
  28 + </logging>
28 29 </phpunit>
196 tests/BarnabyWalters/Helpers/HelpersTest.php
... ... @@ -0,0 +1,196 @@
  1 +<?php
  2 +
  3 +namespace Tests\BarnabyWalters\Helpers;
  4 +
  5 +use BarnabyWalters\Helpers\Helpers as H;
  6 +
  7 +/**
  8 + * A test suite for barnabywalters/helpers
  9 + *
  10 + * Contains tests for all my helper functions
  11 + *
  12 + * @author Barnaby Walters http://waterpigs.co.uk
  13 + * @autor app\helpers\tests
  14 + * @todo Move all traces of THE TRUNCENATOR out of here and into their own package
  15 + */
  16 +class HelpersTest extends \PHPUnit_Framework_TestCase {
  17 + // !Logic
  18 +
  19 + /**
  20 + * Test the truest() function
  21 + *
  22 + * @group unit
  23 + * @group logic
  24 + * @group helpers
  25 + */
  26 + public function testTruestReturnsTrue() {
  27 + // Should return the first true-ish value in args
  28 + $this->assertTrue(H::truest(false, '', true));
  29 + }
  30 +
  31 + /**
  32 + * @group unit
  33 + * @group logic
  34 + */
  35 + public function testTruestReturnFalseIfNoneTrue() {
  36 + $this->assertFalse(H::truest(false, '', ''));
  37 + }
  38 +
  39 + // !Text
  40 +
  41 + /**
  42 + * @group text
  43 + */
  44 + public function testExpandImgExpandsImages() {
  45 + $test = 'blah blah <img src="thevalue" />';
  46 + $expected = 'blah blah thevalue';
  47 + $this->assertEquals($expected, H::expandImg($test));
  48 + }
  49 +
  50 + /**
  51 + * @group text
  52 + */
  53 + public function testToAsciiSanitizes() {
  54 + $test = 'QQQ!’^*+MOREQ';
  55 + $expected = 'qqq-moreq';
  56 + $this->assertEquals($expected, H::toAscii($test));
  57 + }
  58 +
  59 + /**
  60 + * @group unit
  61 + * @group text
  62 + * @group helpers
  63 + */
  64 + public function testAuthorFromUriTwitter() {
  65 + $input = 'https://twitter.com/BarnabyWalters/status/254199790307524610';
  66 + $expected = '@barnabywalters';
  67 + $this->assertEquals($expected, H::authorFromUri($input));
  68 + }
  69 +
  70 + /**
  71 + * @group unit
  72 + * @group text
  73 + * @group helpers
  74 + */
  75 + public function testAuthorFromUriIndieweb() {
  76 + $input = 'http://waterpigs.co.uk/notes/254';
  77 + $expected = 'waterpigs.co.uk';
  78 + $this->assertEquals($expected, H::authorFromUri($input));
  79 + }
  80 +
  81 + /**
  82 + * @group unit
  83 + * @group text
  84 + * @group helpers
  85 + */
  86 + public function testTimeElement() {
  87 + $testTime = new \DateTime();
  88 + $result = '<time datetime="' . $testTime->format('Y-m-d') . '" title="' . $testTime->format('Y-z') . '">' . $testTime->format('Y-m-d') . '</time>';
  89 + $this->assertEquals($result, H::timeElement($testTime));
  90 + }
  91 +
  92 + /**
  93 + * @group unit
  94 + * @group text
  95 + * @group helpers
  96 + */
  97 + public function testTagstringToArray() {
  98 + $testTagstring = 'a tag, anothertag, <sometag>';
  99 + $testResult = array('a tag', 'anothertag', '&lt;sometag&gt;');
  100 + $this->assertEquals($testResult, H::tagstringToArray($testTagstring));
  101 + }
  102 +
  103 + /**
  104 + * @group unit
  105 + * @group text
  106 + * @group helpers
  107 + */
  108 + public function testTagstringClean() {
  109 + $testTagstring = 'a tag, anothertag, <sometag>';
  110 + $testResult = 'a tag,anothertag,&lt;sometag&gt;';
  111 + $this->assertEquals($testResult, H::tagstringClean($testTagstring));
  112 + }
  113 +
  114 + /**
  115 + * @group unit
  116 + * @group text
  117 + */
  118 + public function testAutolinkHashTags() {
  119 + $testText = 'Hey there, #this tag should be auto-linked, as should #this';
  120 + $expected = 'Hey there, <a rel="tag" href="/my/tags/this">#this</a> tag should be auto-linked, as should <a rel="tag" href="/my/tags/this">#this</a>';
  121 +
  122 + $this->assertEquals($expected, H::autolinkHashTags($testText, '/my/tags/'));
  123 + }
  124 +
  125 + /**
  126 + * @group unit
  127 + * @group text
  128 + * @group helpers
  129 + */
  130 + public function testGetPrivacyTagsIgnoresNonAuthTags() {
  131 + $tags = array('sometag', 'someothertag');
  132 + $expected = array();
  133 +
  134 + $this->assertEquals($expected, H::getAuthTags($tags));
  135 + }
  136 +
  137 + /**
  138 + * @group unit
  139 + * @group text
  140 + * @group helpers
  141 + */
  142 + public function testGetPrivacyTagsParsesMultiplePrivateTagsCorrectly() {
  143 + $tags = array('auth:private=domain.com', 'auth:private=someotherdomain.com');
  144 + $expected = array(
  145 + 'private' => array(
  146 + 'domain.com',
  147 + 'someotherdomain.com'
  148 + )
  149 + );
  150 +
  151 + $this->assertEquals($expected, H::getAuthTags($tags));
  152 + }
  153 +
  154 + /**
  155 + * @group unit
  156 + * @group text
  157 + * @group helpers
  158 + */
  159 + public function testGetPrivacyTagsParsesMultiplePrivateAndEditableTagsCorrectly() {
  160 + $tags = array('auth:private=domain.com', 'auth:private=someotherdomain.com', 'auth:editable=someotherdomain.com');
  161 + $expected = array(
  162 + 'private' => array(
  163 + 'domain.com',
  164 + 'someotherdomain.com'
  165 + ),
  166 + 'editable' => array(
  167 + 'someotherdomain.com'
  168 + )
  169 + );
  170 +
  171 + $this->assertEquals($expected, H::getAuthTags($tags));
  172 + }
  173 +
  174 + /**
  175 + * @group unit
  176 + * @group text
  177 + * @group helpers
  178 + */
  179 + public function testAtomDate() {
  180 + $this->assertEquals('2012-10-15T22:46:00+00:00', H::atomDate('15th October 2012 22:46'));
  181 + }
  182 +
  183 + /**
  184 + * @group unit
  185 + * @group text
  186 + * @group helpers
  187 + */
  188 + public function testFindURLs() {
  189 + $testString = 'Okay, so this string contains some URLs. http://waterpigs.co.uk, identi.ca, aaron.pk';
  190 + $testArray = array('http://waterpigs.co.uk', 'http://identi.ca', 'http://aaron.pk');
  191 + $this->assertEquals($testArray, H::findUrls($testString));
  192 + }
  193 +
  194 +}
  195 +
  196 +// EOF
14 tests/BarnabyWalters/Posse/HelpersTest.php
... ... @@ -0,0 +1,14 @@
  1 +<?php
  2 +
  3 +namespace Tests\BarnabyWalters\Posse;
  4 +
  5 +/**
  6 + * Description of HelpersTest
  7 + *
  8 + * @author barnabywalters
  9 + */
  10 +class HelpersTest {
  11 + //put your code here
  12 +}
  13 +
  14 +// EOF
144 tests/BarnabyWalters/Posse/TruncenatorTest.php
... ... @@ -0,0 +1,144 @@
  1 +<?php
  2 +
  3 +namespace Tests\BarnabyWalters\Posse;
  4 +
  5 +use BarnabyWalters\Posse\Truncenator;
  6 +
  7 +/**
  8 + * Description of TruncenatorTest
  9 + *
  10 + * @author Barnaby Walters
  11 + */
  12 +class TruncenatorTest extends \PHPUnit_Framework_TestCase {
  13 + /**
  14 + * @group unit
  15 + * @group text
  16 + * @group helpers
  17 + * @group truncenator
  18 + */
  19 + public function testTruncateRespectsLength() {
  20 + $input = 'Here is some text which is over 140 characters long Here is some text which is over 140 characters long Here is some text which is over 140 characters long';
  21 + $expected = 'Here is some text which is over 140 characters long Here is some text which is over 140 characters long Here is some text which is over 140…';
  22 + $this->assertEquals($expected, Truncenator::truncate($input, $length = 140));
  23 + }
  24 +
  25 + /**
  26 + * @group unit
  27 + * @group text
  28 + * @group helpers
  29 + * @group truncenator
  30 + */
  31 + public function testTruncatePreservesWords() {
  32 + $input = 'Here is some text which is over 140 characters long Here is some text which is over 140 characters long Here is some text which is over longlonglongword';
  33 + $expected = 'Here is some text which is over 140 characters long Here is some text which is over 140 characters long Here is some text which is over…';
  34 + $this->assertEquals($expected, Truncenator::truncate($input, $length = 140));
  35 + }
  36 +
  37 + /**
  38 + * @group unit
  39 + * @group text
  40 + * @group helpers
  41 + * @group truncenator
  42 + */
  43 + public function testTruncateAppendsURI() {
  44 + $input = 'Here is some text which is over 140 characters long Here is some text which is over 140 characters long Here is some text which is over 140 characters long';
  45 + $uri = 'http://example.org/notes/14';
  46 + $expected = 'Here is some text which is over 140 characters long Here is some text which is over 140 characters long Here is… http://example.org/notes/14';
  47 + $this->assertEquals($expected, Truncenator::truncate($input, $length = 140, $uri = $uri));
  48 + }
  49 +
  50 + /**
  51 + * @group unit
  52 + * @group text
  53 + * @group helpers
  54 + * @group truncenator
  55 + */
  56 + public function testTruncateAppendsURIPreservingWords() {
  57 + $input = 'Here is some text which is over 140 characters long Here is some text which is over 140 characters longwordlongwordlongwordlongword';
  58 + $uri = 'http://example.org/notes/15';
  59 + $expected = 'Here is some text which is over 140 characters long Here is some text which is over 140 characters… http://example.org/notes/15';
  60 + $this->assertEquals($expected, Truncenator::truncate($input, $length = 140, $uri = $uri));
  61 + }
  62 +
  63 + /**
  64 + * @group unit
  65 + * @group text
  66 + * @group helpers
  67 + * @group truncenator
  68 + */
  69 + public function testTruncateParenthesisesURIIfNotTruncated() {
  70 + $input = 'Here is some really short text';
  71 + $uri = 'http://example.org/notes/16';
  72 + $expected = 'Here is some really short text (http://example.org/notes/16)';
  73 + $this->assertEquals($expected, Truncenator::truncate($input, $length = 140, $uri = $uri));
  74 + }
  75 +
  76 + /**
  77 + * @group unit
  78 + * @group text
  79 + * @group helpers
  80 + * @group truncenator
  81 + */
  82 + public function testTruncateRespectsURILength() {
  83 + $input = 'Here is some text which is over 140 characters long Here is some text which is over 140 characters long Here is some text which is over 140 characters long';
  84 + $uri = 'http://example.org/notes/14';
  85 + $urilen = 20;
  86 + $expected = 'Here is some text which is over 140 characters long Here is some text which is over 140 characters long Here is some… http://example.org/notes/14';
  87 + }
  88 +
  89 + /**
  90 + * @group unit
  91 + * @group text
  92 + * @group helpers
  93 + * @group truncenator
  94 + */
  95 + public function testTruncateRespectsTextURILength() {
  96 + $input = 'Here is some text with a URI in: http://example.com and one without a protocol: example.org long Here is some text which is over 140 characters long Here is some text which is over 140 characters long';
  97 + $uri = 'http://example.org/notes/14';
  98 + $urilen = 20;
  99 + $expected = 'Here is some text with a URI in: http://example.com and one without a protocol: example.org long Here is… http://example.org/notes/14';
  100 + $this->assertEquals($expected, Truncenator::truncate($input, $length = 140, $uri = $uri, $urilen = $urilen));
  101 + }
  102 +
  103 + /**
  104 + * @group unit
  105 + * @group text
  106 + * @group helpers
  107 + * @group truncenator
  108 + */
  109 + public function testTruncateURILenOverloadsAppendURILength() {
  110 + $input = 'Here is some text over 140 characters long Here is some text over 140 characters long Here is some text over 140 characters long Here is some text over 140 characters long';
  111 + $uri = 'http://example.org/notes/14';
  112 + $urilen = 30; // over-engineering
  113 + $expected = 'Here is some text over 140 characters long Here is some text over 140 characters long Here is some text over… http://example.org/notes/14';
  114 + $this->assertEquals($expected, Truncenator::truncate($input, $length = 140, $uri = $uri, $urilen = $urilen));
  115 + }
  116 +
  117 + /**
  118 + * @group unit
  119 + * @group text
  120 + * @group helpers
  121 + * @group truncenator
  122 + */
  123 + public function testTruncatePreservesHashtags() {
  124 + $input = 'Here is some text with #hashtags in! Woo! And it’s really long really long really long really long really long really long really long with #morehashtags at the #end';
  125 + $uri = 'http://example.com/notes/34';
  126 + $expected = 'Here is some text with #hashtags in! Woo! And it’s really long really long really long really… #morehashtags http://example.com/notes/34';
  127 + $this->assertEquals($expected, Truncenator::truncate($input, $length = 140, $uri = $uri));
  128 + }
  129 +
  130 + /**
  131 + * @group unit
  132 + * @group text
  133 + * @group helpers
  134 + * @group truncenator
  135 + */
  136 + public function testTruncateNoParenthesisLengthOkay() {
  137 + $input = 'Here is a piece of text 109 characters long Here is a piece of text 109 characters long there is piece text';
  138 + $uri = 'http://example.com/articles/34'; // 30 long
  139 + $expected = 'Here is a piece of text 109 characters long Here is a piece of text 109 characters long there is piece text (http://example.com/articles/34)';
  140 + $this->assertEquals($expected, Truncenator::truncate($input, $length = 140, $uri = $uri));
  141 + }
  142 +}
  143 +
  144 +// EOF
337 tests/HelpersTest.php
... ... @@ -1,337 +0,0 @@
1   -<?php
2   -
3   -namespace BarnabyWalters\Helpers\Test;
4   -
5   -use BarnabyWalters\Helpers\Helpers as H;
6   -
7   -// Load files
8   -$vendorPath = realpath(dirname(__DIR__) . '/vendor/autoload.php');
9   -if (file_exists($vendorPath)) {
10   - // If the vendor dir exists (the user is in a testing environment) load the autoloader
11   - ob_start();
12   - require $vendorPath;
13   - ob_end_clean();
14   -} else {
15   - die('Cannot run tests as you haven’t installed the required dependencies');
16   -}
17   -
18   -/**
19   - * A test suite for barnabywalters/helpers
20   - *
21   - * Contains tests for all my helper functions
22   - *
23   - * @author Barnaby Walters http://waterpigs.co.uk
24   - * @autor app\helpers\tests
25   - * @todo Move all traces of THE TRUNCENATOR out of here and into their own package
26   - */
27   -class HelpersTest extends \PHPUnit_Framework_TestCase {
28   - // !Logic
29   -
30   - /**
31   - * Test the truest() function
32   - *
33   - * @group unit
34   - * @group logic
35   - * @group helpers
36   - */
37   - public function testTruestReturnsTrue() {
38   - // Should return the first true-ish value in args
39   - $this->assertTrue(H::truest(false, '', true));
40   - }
41   -
42   - /**
43   - * @group unit
44   - * @group logic
45   - */
46   - public function testTruestReturnFalseIfNoneTrue() {
47   - $this->assertFalse(H::truest(false, '', ''));
48   - }
49   -
50   - // !Text
51   -
52   - /**
53   - * @group text
54   - */
55   - public function testExpandImgExpandsImages() {
56   - $test = 'blah blah <img src="thevalue" />';
57   - $expected = 'blah blah thevalue';
58   - $this->assertEquals($expected, H::expandImg($test));
59   - }
60   -
61   - /**
62   - * @group text
63   - */
64   - public function testToAsciiSanitizes() {
65   - $test = 'QQQ!’^*+MOREQ';
66   - $expected = 'qqq-moreq';
67   - $this->assertEquals($expected, H::toAscii($test));
68   - }
69   -
70   - /**
71   - * @group unit
72   - * @group text
73   - * @group helpers
74   - */
75   - public function testAuthorFromUriTwitter() {
76   - $input = 'https://twitter.com/BarnabyWalters/status/254199790307524610';
77   - $expected = '@barnabywalters';
78   - $this->assertEquals($expected, H::authorFromUri($input));
79   - }
80   -
81   - /**
82   - * @group unit
83   - * @group text
84   - * @group helpers
85   - */
86   - public function testAuthorFromUriIndieweb() {
87   - $input = 'http://waterpigs.co.uk/notes/254';
88   - $expected = 'waterpigs.co.uk';
89   - $this->assertEquals($expected, H::authorFromUri($input));
90   - }
91   -
92   - /**
93   - * @group unit
94   - * @group text
95   - * @group helpers
96   - * @group truncenator
97   - */
98   - public function testTruncateRespectsLength() {
99   - $input = 'Here is some text which is over 140 characters long Here is some text which is over 140 characters long Here is some text which is over 140 characters long';
100   - $expected = 'Here is some text which is over 140 characters long Here is some text which is over 140 characters long Here is some text which is over 140…';
101   - $this->assertEquals($expected, H::truncate($input, $length = 140));
102   - }
103   -
104   - /**
105   - * @group unit
106   - * @group text
107   - * @group helpers
108   - * @group truncenator
109   - */
110   - public function testTruncatePreservesWords() {
111   - $input = 'Here is some text which is over 140 characters long Here is some text which is over 140 characters long Here is some text which is over longlonglongword';
112   - $expected = 'Here is some text which is over 140 characters long Here is some text which is over 140 characters long Here is some text which is over…';
113   - $this->assertEquals($expected, H::truncate($input, $length = 140));
114   - }
115   -
116   - /**
117   - * @group unit
118   - * @group text
119   - * @group helpers
120   - * @group truncenator
121   - */
122   - public function testTruncateAppendsURI() {
123   - $input = 'Here is some text which is over 140 characters long Here is some text which is over 140 characters long Here is some text which is over 140 characters long';
124   - $uri = 'http://example.org/notes/14';
125   - $expected = 'Here is some text which is over 140 characters long Here is some text which is over 140 characters long Here is… http://example.org/notes/14';
126   - $this->assertEquals($expected, H::truncate($input, $length = 140, $uri = $uri));
127   - }
128   -
129   - /**
130   - * @group unit
131   - * @group text
132   - * @group helpers
133   - * @group truncenator
134   - */
135   - public function testTruncateAppendsURIPreservingWords() {
136   - $input = 'Here is some text which is over 140 characters long Here is some text which is over 140 characters longwordlongwordlongwordlongword';
137   - $uri = 'http://example.org/notes/15';
138   - $expected = 'Here is some text which is over 140 characters long Here is some text which is over 140 characters… http://example.org/notes/15';
139   - $this->assertEquals($expected, H::truncate($input, $length = 140, $uri = $uri));
140   - }
141   -
142   - /**
143   - * @group unit
144   - * @group text
145   - * @group helpers
146   - * @group truncenator
147   - */
148   - public function testTruncateParenthesisesURIIfNotTruncated() {
149   - $input = 'Here is some really short text';
150   - $uri = 'http://example.org/notes/16';
151   - $expected = 'Here is some really short text (http://example.org/notes/16)';
152   - $this->assertEquals($expected, H::truncate($input, $length = 140, $uri = $uri));
153   - }
154   -
155   - /**
156   - * @group unit
157   - * @group text
158   - * @group helpers
159   - * @group truncenator
160   - */
161   - public function testTruncateRespectsURILength() {
162   - $input = 'Here is some text which is over 140 characters long Here is some text which is over 140 characters long Here is some text which is over 140 characters long';
163   - $uri = 'http://example.org/notes/14';
164   - $urilen = 20;
165   - $expected = 'Here is some text which is over 140 characters long Here is some text which is over 140 characters long Here is some… http://example.org/notes/14';
166   - }
167   -
168   - /**
169   - * @group unit
170   - * @group text
171   - * @group helpers
172   - * @group truncenator
173   - */
174   - public function testTruncateRespectsTextURILength() {
175   - $input = 'Here is some text with a URI in: http://example.com and one without a protocol: example.org long Here is some text which is over 140 characters long Here is some text which is over 140 characters long';
176   - $uri = 'http://example.org/notes/14';
177   - $urilen = 20;
178   - $expected = 'Here is some text with a URI in: http://example.com and one without a protocol: example.org long Here is… http://example.org/notes/14';
179   - $this->assertEquals($expected, H::truncate($input, $length = 140, $uri = $uri, $urilen = $urilen));
180   - }
181   -
182   - /**
183   - * @group unit
184   - * @group text
185   - * @group helpers
186   - * @group truncenator
187   - */
188   - public function testTruncateURILenOverloadsAppendURILength() {
189   - $input = 'Here is some text over 140 characters long Here is some text over 140 characters long Here is some text over 140 characters long Here is some text over 140 characters long';
190   - $uri = 'http://example.org/notes/14';
191   - $urilen = 30; // over-engineering
192   - $expected = 'Here is some text over 140 characters long Here is some text over 140 characters long Here is some text over… http://example.org/notes/14';
193   - $this->assertEquals($expected, H::truncate($input, $length = 140, $uri = $uri, $urilen = $urilen));
194   - }
195   -
196   - /**
197   - * @group unit
198   - * @group text
199   - * @group helpers
200   - * @group truncenator
201   - */
202   - public function testTruncatePreservesHashtags() {
203   - $input = 'Here is some text with #hashtags in! Woo! And it’s really long really long really long really long really long really long really long with #morehashtags at the #end';
204   - $uri = 'http://example.com/notes/34';
205   - $expected = 'Here is some text with #hashtags in! Woo! And it’s really long really long really long really… #morehashtags http://example.com/notes/34';
206   - $this->assertEquals($expected, H::truncate($input, $length = 140, $uri = $uri));
207   - }
208   -
209   - /**
210   - * @group unit
211   - * @group text
212   - * @group helpers
213   - * @group truncenator
214   - */
215   - public function testTruncateNoParenthesisLengthOkay() {
216   - $input = 'Here is a piece of text 109 characters long Here is a piece of text 109 characters long there is piece text';
217   - $uri = 'http://example.com/articles/34'; // 30 long
218   - $expected = 'Here is a piece of text 109 characters long Here is a piece of text 109 characters long there is piece text (http://example.com/articles/34)';
219   - $this->assertEquals($expected, H::truncate($input, $length = 140, $uri = $uri));
220   - }
221   -
222   - /**
223   - * @group unit
224   - * @group text
225   - * @group helpers
226   - */
227   - public function testTimeElement() {
228   - $testTime = new \DateTime();
229   - $result = '<time datetime="' . $testTime->format('Y-m-d') . '" title="' . $testTime->format('Y-z') . '">' . $testTime->format('Y-m-d') . '</time>';
230   - $this->assertEquals($result, H::timeElement($testTime));
231   - }
232   -
233   - /**
234   - * @group unit
235   - * @group text
236   - * @group helpers
237   - */
238   - public function testTagstringToArray() {
239   - $testTagstring = 'a tag, anothertag, <sometag>';
240   - $testResult = array('a tag', 'anothertag', '&lt;sometag&gt;');
241   - $this->assertEquals($testResult, H::tagstringToArray($testTagstring));
242   - }
243   -
244   - /**
245   - * @group unit
246   - * @group text
247   - * @group helpers
248   - */
249   - public function testTagstringClean() {
250   - $testTagstring = 'a tag, anothertag, <sometag>';
251   - $testResult = 'a tag,anothertag,&lt;sometag&gt;';
252   - $this->assertEquals($testResult, H::tagstringClean($testTagstring));
253   - }
254   -
255   - /**
256   - * @group unit
257   - * @group text
258   - */
259   - public function testAutolinkHashTags() {
260   - $testText = 'Hey there, #this tag should be auto-linked, as should #this';
261   - $expected = 'Hey there, <a rel="tag" href="/my/tags/this">#this</a> tag should be auto-linked, as should <a rel="tag" href="/my/tags/this">#this</a>';
262   -
263   - $this->assertEquals($expected, H::autolinkHashTags($testText, '/my/tags/'));
264   - }
265   -
266   - /**
267   - * @group unit
268   - * @group text
269   - * @group helpers
270   - */
271   - public function testGetPrivacyTagsIgnoresNonAuthTags() {
272   - $tags = array('sometag', 'someothertag');
273   - $expected = array();
274   -
275   - $this->assertEquals($expected, H::getAuthTags($tags));
276   - }
277   -
278   - /**
279   - * @group unit
280   - * @group text
281   - * @group helpers
282   - */
283   - public function testGetPrivacyTagsParsesMultiplePrivateTagsCorrectly() {
284   - $tags = array('auth:private=domain.com', 'auth:private=someotherdomain.com');
285   - $expected = array(
286   - 'private' => array(
287   - 'domain.com',
288   - 'someotherdomain.com'
289   - )
290   - );
291   -
292   - $this->assertEquals($expected, H::getAuthTags($tags));
293   - }
294   -
295   - /**
296   - * @group unit
297   - * @group text
298   - * @group helpers
299   - */
300   - public function testGetPrivacyTagsParsesMultiplePrivateAndEditableTagsCorrectly() {
301   - $tags = array('auth:private=domain.com', 'auth:private=someotherdomain.com', 'auth:editable=someotherdomain.com');
302   - $expected = array(
303   - 'private' => array(
304   - 'domain.com',
305   - 'someotherdomain.com'
306   - ),
307   - 'editable' => array(
308   - 'someotherdomain.com'
309   - )
310   - );
311   -
312   - $this->assertEquals($expected, H::getAuthTags($tags));
313   - }
314   -
315   - /**
316   - * @group unit
317   - * @group text
318   - * @group helpers
319   - */
320   - public function testAtomDate() {
321   - $this->assertEquals('2012-10-15T22:46:00+00:00', H::atomDate('15th October 2012 22:46'));
322   - }
323   -
324   - /**
325   - * @group unit
326   - * @group text
327   - * @group helpers
328   - */
329   - public function testFindURLs() {
330   - $testString = 'Okay, so this string contains some URLs. http://waterpigs.co.uk, identi.ca, aaron.pk';
331   - $testArray = array('http://waterpigs.co.uk', 'http://identi.ca', 'http://aaron.pk');
332   - $this->assertEquals($testArray, H::findUrls($testString));
333   - }
334   -
335   -}
336   -
337   -// EOF
5 tests/bootstrap.php
... ... @@ -0,0 +1,5 @@
  1 +<?php
  2 +
  3 +require realpath(__DIR__ . '/../vendor/autoload.php');
  4 +
  5 +// EOF

0 comments on commit ddae829

Please sign in to comment.
Something went wrong with that request. Please try again.