Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Few tests JDocument package #1843

Merged
merged 5 commits into from

3 participants

@piotr-cz

JDocumentImage
JDocumentXml
JDocumentHtml (8 methods)

This isn't much, but these are my first unit tests.

@dongilbert
Collaborator

Looks good. All the tests are passing on my end.

@piotr-cz

Pleas take a look at the code quality, as these are the first test I've ever written.

I was going to write one more for JDocumentHtml::mergeHeadData() but I'm confused, as this method is using internal methods (setMetaData, addStyleDeclaration) whilte setMetadata is setting values directly.

Is this how it's supposed to work?

@dongilbert
Collaborator

OK - I'll take another look when I have time to give it a proper review of quality as well as functionality.

Thanks for doing this.

...uites/unit/joomla/document/html/JDocumentHTMLTest.php
((15 lines not shown))
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
+ }
+
+ /**
+ * Terst construct
@elinw
elinw added a note

Test :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@elinw elinw commented on the diff
...tes/unit/joomla/document/image/JDocumentImageTest.php
((85 lines not shown))
+ $this->equalTo('image'),
+ 'JDocumentImage::__construct: Default Type does not match'
+ );
+ }
+
+ /**
+ * Test...
+ *
+ * @covers JDocumentImage::render
+ *
+ * @return void
+ */
+ public function testRender()
+ {
+ JResponse::clearHeaders();
+
@elinw
elinw added a note

What about doing this as a data provider. That way if one fails the rest of the tests still run. Also I usually put a message so it's easier to figure out which test is the one that failed. It isn't super important but it makes php unit easier to use.

I'll need your guidance on data provider.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dongilbert dongilbert merged commit 72cecd0 into joomla:staging
@piotr-cz piotr-cz deleted the unknown repository branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 5, 2013
  1. @piotr-cz

    tests for JDocumentImage

    piotr-cz authored
  2. @piotr-cz

    Tests for JDocumentXml

    piotr-cz authored
  3. @piotr-cz

    few tests for JDocumentHtml

    piotr-cz authored
Commits on Mar 15, 2013
  1. @piotr-cz

    Fix typo

    piotr-cz authored
  2. @piotr-cz
This page is out of date. Refresh to see the latest.
View
227 tests/suites/unit/joomla/document/html/JDocumentHTMLTest.php
@@ -38,33 +38,136 @@ protected function setUp()
}
/**
- * Test getHeadData
+ * Tears down the fixture, for example, closes a network connection.
+ * This method is called after a test is executed.
*
- * @todo Implement testGetHeadData().
+ * @access protected
*
* @return void
*/
- public function testGetHeadData()
+ protected function tearDown()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
+ }
+
+ /**
+ * Test construct
+ *
+ * @covers JDocumentHtml::__construct
+ *
+ * @return void
+ */
+ public function test__construct($options = array())
+ {
+ $documentHtml = new JDocumentHtml;
+
+ $this->assertThat(
+ $documentHtml->_mime,
+ $this->equalTo('text/html'),
+ 'JDocumentHtml::__construct: Default Mime does not match'
+ );
+
+ $this->assertThat(
+ $documentHtml->_type,
+ $this->equalTo('html'),
+ 'JDocumentHtml::__construct: Default Type does not match'
);
}
/**
- * Test...
+ * Test getHeadData
*
- * @todo Implement testSetHeadData().
+ * @covers JDocumentHtml::setHeadData
+ * @covers JDocumentHtml::getHeadData
*
* @return void
*/
- public function testSetHeadData()
+ public function testSetAndGetHeadData()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
+
+ // Get default values
+ $default = $this->object->getHeadData();
+
+ // Test invalid data
+ $return = $this->object->setHeadData('invalid');
+
+ $this->assertThat(
+ $this->object->getHeadData(),
+ $this->equalTo($default),
+ 'JDocumentHtml::setHeadData invalid data allowed to be set'
);
+
+ // Test return value
+ $this->assertThat(
+ $return,
+ $this->isNull(),
+ 'JDocumentHtml::setHeadData did not return null'
+ );
+
+ // Test setting/ getting values
+ $test_data = array(
+ 'title' => 'My Custom Title',
+ 'description' => 'My Description',
+ 'link' => 'http://joomla.org',
+ 'metaTags' => array(
+ 'myMetaTag' => 'myMetaContent'
+ ),
+ 'links' => array(
+ 'index.php' => array(
+ 'relation' => 'Start',
+ 'relType' => 'rel',
+ 'attribs' => array()
+ )
+ ),
+ 'styleSheets' => array(
+ 'test.css' => array(
+ 'mime' => 'text/css',
+ 'media' => null,
+ 'attribs' => array()
+ )
+ ),
+ 'style' => array(
+ 'text/css' => 'body { background: white; }'
+ ),
+ 'scripts' => array(
+ 'test.js' => array(
+ 'mime' => 'text/javascript',
+ 'defer' => false,
+ 'async' => false
+ )
+ ),
+ 'script' => array(
+ 'text/javascript' => "window.addEvent('load', function() { new JCaption('img.caption'); });"
+ ),
+ 'custom' => array(
+ "<script>var html5 = true;</script>"
+ )
+ );
+
+ foreach ($test_data as $dataKey => $dataValue)
+ {
+ // Set
+ $return = $this->object->setHeadData(array($dataKey => $dataValue));
+
+ // Get
+ $compareTo = $this->object->getHeadData();
+
+ // Assert
+ $this->assertThat(
+ $compareTo[$dataKey],
+ $this->equalTo($dataValue),
+ 'JDocumentHtml::setHeadData did not return ' . $dataKey . ' properly or setHeadData with ' . $dataKey . ' did not work'
+ );
+
+ // Test return value
+ $this->assertThat(
+ $return,
+ $this->equalTo($this->object),
+ 'JDocumentHtml::setHeadData did not return JDocumentHtml instance'
+ );
+ }
+
+
+ // Could use native methods (JDocument::addStyleSheet, etc) like $this->mergeHeadData
}
/**
@@ -85,45 +188,115 @@ public function testMergeHeadData()
/**
* Test...
*
- * @todo Implement testAddHeadLink().
+ * @covers JDocumentHtml::addHeadLink
*
- * @return void
+ * @return void
+ *
+ * @note MDC: <link> https://developer.mozilla.org/en-US/docs/HTML/Element/link
*/
public function testAddHeadLink()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
+ // Simple
+ $this->object->addHeadLink('index.php', 'Start');
+
+ $this->assertThat(
+ $this->object->_links['index.php'],
+ $this->equalTo(array('relation' => 'Start', 'relType' => 'rel', 'attribs' => array())),
+ 'addHeadLink did not work'
+ );
+
+ // RSS
+ $link = '&format=feed&limitstart=';
+ $attribs = array('type' => 'application/rss+xml', 'title' => 'RSS 2.0');
+
+ $this->object->addHeadLink($link, 'alternate', 'rel', $attribs);
+
+ $this->assertThat(
+ $this->object->_links[$link],
+ $this->equalTo(array('relation' => 'alternate', 'relType' => 'rel', 'attribs' => $attribs)),
+ 'JDocumentHtml::addHeadLink did not work for RSS'
);
}
/**
* Test...
*
- * @todo Implement testAddFavicon().
+ * @covers JDocumentHtml::addFavicon
*
- * @return void
+ * @return void
*/
public function testAddFavicon()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
+ $this->object->addFavicon('templates\protostar\favicon.ico');
+
+ $this->assertThat(
+ $this->object->_links['templates/protostar/favicon.ico'],
+ $this->equalTo(array('relation' => 'shortcut icon', 'relType' => 'rel', 'attribs' => array('type' => 'image/vnd.microsoft.icon'))),
+ 'JDocumentHtml::addFavicon did not work'
+ );
+
+ $this->object->addFavicon('favicon.gif', null);
+
+ $this->assertThat(
+ $this->object->_links['favicon.gif'],
+ $this->equalTo(array('relation' => 'shortcut icon', 'relType' => 'rel', 'attribs' => array('type' => null))),
+ 'JDocumentHtml::addFavicon did not work'
);
}
/**
* Test...
*
- * @todo Implement testAddCustomTag().
+ * @covers JDocumentHtml::addCustomTag
*
- * @return void
+ * @return void
*/
public function testAddCustomTag()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
+ $this->object->addCustomTag("\t <script>var html5 = true;</script>\r\n");
+
+ $this->assertThat(
+ in_array('<script>var html5 = true;</script>', $this->object->_custom),
+ $this->isTrue(),
+ 'JDocumentHtml::addCustomTag did not work'
+ );
+ }
+
+ /**
+ * We test both at once
+ *
+ * @covers JDocumentHtml::isHtml5
+ * @covers JDocumentHtml::setHtml5
+ *
+ * @return void
+ */
+ public function testIsAndSetHtml5()
+ {
+ // Check true
+ $this->object->setHtml5(true);
+
+ $this->assertThat(
+ $this->object->isHtml5(),
+ $this->isTrue(),
+ 'JDocumentHtml::setHtml5(true) did not work'
+ );
+
+ // Check false
+ $this->object->setHtml5(false);
+
+ $this->assertThat(
+ $this->object->isHtml5(),
+ $this->isFalse(),
+ 'JDocumentHtml::setHtml5(false) did not work'
+ );
+
+ // Check non-boolean
+ $this->object->setHtml5('non boolean');
+
+ $this->assertThat(
+ $this->object->isHtml5(),
+ $this->logicalNot($this->equalTo('non boolean')),
+ "JDocumentHtml::setHtml5('non boolean') did not work"
);
}
View
157 tests/suites/unit/joomla/document/image/JDocumentImageTest.php
@@ -0,0 +1,157 @@
+<?php
+/**
+ * @package Joomla.UnitTest
+ * @subpackage Document
+ *
+ * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+require_once JPATH_PLATFORM . '/joomla/document/document.php';
+require_once JPATH_PLATFORM . '/joomla/document/image/image.php';
+
+/**
+ * Test class for JDocumentImage.
+ *
+ * @package Joomla.UnitTest
+ * @subpackage Document
+ * @since 13.1
+ */
+class JDocumentImageTest extends TestCase
+{
+ /**
+ * @var JDocumentImage
+ * @access protected
+ */
+ protected $object;
+
+ /**
+ * Sets up the fixture, for example, opens a network connection.
+ * This method is called before a test is executed.
+ *
+ * @access protected
+ *
+ * @return void
+ */
+ protected function setUp()
+ {
+ parent::setUp();
+
+ $this->saveFactoryState();
+
+ require_once JPATH_PLATFORM . '/joomla/factory.php';
+
+ $app = $this->getMockApplication();
+
+ JFactory::$application = $app;
+
+ $this->object = new JDocumentImage;
+ }
+
+ /**
+ * Tears down the fixture, for example, closes a network connection.
+ * This method is called after a test is executed.
+ *
+ * @access protected
+ *
+ * @return void
+ */
+ protected function tearDown()
+ {
+ $this->restoreFactoryState();
+
+ parent::teardown();
+ }
+
+ /**
+ * Tests the JDocumentImage::__construct method.
+ *
+ * @return void
+ *
+ * @covers JDocumentImage::__construct
+ */
+ public function test__construct($options = array())
+ {
+ $documentImage = new JDocumentImage;
+
+ $this->assertThat(
+ $documentImage->_mime,
+ $this->equalTo('image/png'),
+ 'JDocumentImage::__construct: Default Mime does not match'
+ );
+
+ $this->assertThat(
+ $documentImage->_type,
+ $this->equalTo('image'),
+ 'JDocumentImage::__construct: Default Type does not match'
+ );
+ }
+
+ /**
+ * Tests the JDocumentImage::render method.
+ *
+ * @covers JDocumentImage::render
+ *
+ * @return void
+ */
+ public function testRender()
+ {
+ JResponse::clearHeaders();
+
@elinw
elinw added a note

What about doing this as a data provider. That way if one fails the rest of the tests still run. Also I usually put a message so it's easier to figure out which test is the one that failed. It isn't super important but it makes php unit easier to use.

I'll need your guidance on data provider.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $testFiles = array(
+ 'jpg' => array(
+ 'file' => 'logo.jpg',
+ 'mime' => 'image/jpeg'
+ ),
+ 'jpeg' => array(
+ 'file' => 'logo.jpeg',
+ 'mime' => 'image/jpeg'
+ ),
+ 'gif' => array(
+ 'file' => 'logo.gif',
+ 'mime' => 'image/gif'
+ ),
+ 'png' => array(
+ 'file' => 'logo.png',
+ 'mime' => 'image/png',
+ ),
+ 'bmp' => array(
+ 'file' => 'logo.png',
+ 'mime' => 'image/png'
+ )
+ );
+
+ foreach ($testFiles as $type => $info)
+ {
+ // Set type
+ JFactory::$application->input->set('type', $type);
+
+ $buffer = file_get_contents(__DIR__ . '/' . $info['file']);
+
+ // Render
+ $this->object->setBuffer($buffer);
+ $returnBuffer = $this->object->render();
+
+ // Check buffer return
+ $this->assertThat(
+ $returnBuffer,
+ $this->equalTo($buffer),
+ 'JDocumentImage::render: Buffer does not match for type `' . $type . '`'
+ );
+
+ // Check Mime
+ $this->assertThat(
+ $this->object->_mime,
+ $this->equalTo($info['mime']),
+ 'JDocumentImage::render: Mime does not match for type `' . $type . '`'
+ );
+ }
+
+ // Chek Charset
+ $this->assertThat(
+ $this->object->_charset,
+ $this->isNull(),
+ 'JDocumentImage::render Charset is not null'
+ );
+ }
+}
View
BIN  tests/suites/unit/joomla/document/image/logo.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  tests/suites/unit/joomla/document/image/logo.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  tests/suites/unit/joomla/document/image/logo.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  tests/suites/unit/joomla/document/image/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
82 tests/suites/unit/joomla/document/xml/JDocumentXMLTest.php
@@ -50,59 +50,53 @@ protected function setUp()
*/
public function testRender()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete('This test has not been implemented yet.');
- }
+ JResponse::clearHeaders();
- /**
- * Test...
- *
- * @todo Implement testGetHeadData().
- *
- * @return void
- */
- public function testGetHeadData()
- {
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete('This test has not been implemented yet.');
- }
+ $this->object->setBuffer('Unit Test Buffer');
- /**
- * Test...
- *
- * @todo Implement testSetHeadData().
- *
- * @return void
- */
- public function testSetHeadData()
- {
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete('This test has not been implemented yet.');
- }
+ $this->assertThat(
+ $this->object->render(),
+ $this->equalTo('Unit Test Buffer'),
+ 'We did not get the buffer back properly'
+ );
- /**
- * Test...
- *
- * @todo Implement testGetName().
- *
- * @return void
- */
- public function testGetName()
- {
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete('This test has not been implemented yet.');
+ $headers = JResponse::getHeaders();
+
+ $disposition = false;
+
+ foreach ($headers as $head)
+ {
+ if ($head['name'] == 'Content-disposition')
+ {
+ $this->assertThat(
+ $head['value'],
+ $this->stringContains('.xml'),
+ 'The content disposition did not include json extension'
+ );
+ $disposition = true;
+ }
+ }
+
+ $this->assertThat(
+ $disposition,
+ $this->equalTo(true),
+ 'No Content-disposition headers'
+ );
}
/**
- * Test...
- *
- * @todo Implement testSetName().
+ * We test both at once
*
* @return void
*/
- public function testSetName()
+ public function testGetAndSetName()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete('This test has not been implemented yet.');
+ $this->object->setName('unittestfilename');
+
+ $this->assertThat(
+ $this->object->getName(),
+ $this->equalTo('unittestfilename'),
+ 'setName or getName did not work'
+ );
}
}
Something went wrong with that request. Please try again.