Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitchenkoSergey committed Jan 7, 2018
1 parent 1497438 commit a490345
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ phpunit.phar

composer.phar

/vendor
/vendor

composer.lock
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"require": {
"php": ">=5.5.0"
},
"require-dev": {
"phpunit/phpunit": "*"
},
"autoload": {
"psr-4": {
"Idg\\": "src/"
Expand Down
2 changes: 1 addition & 1 deletion examples/columns.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

$idg = new \Idg\Idg(1000, 3000, null, new ImagickPixel('#fff'));
$idg->beginDocument(40, 30, 40, 30);
$idg->beginRow()->setPaddingBottom(30);
$idg->beginRow();
$idg->beginColumn(300);
$idg->image('test_image.jpg');
$idg->beginBlock()->setLeft(20);
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_element.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function afterRender()
$customBlock = new GreenBlock();
$idg = new \Idg\Idg(1000, 3000);

$idg->beginDocument(20, 30, 60, 30);
$idg->beginDocument(20, 30, 30, 30);
$idg->text('Custom element',
$fontRobotoRegular, 26, '#000', Imagick::ALIGN_CENTER);
$idg->beginElement($customBlock)->setTop(20)->setPaddingBottom(25);
Expand Down
2 changes: 1 addition & 1 deletion examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

$idg = new \Idg\Idg(1000, 3000, null, new ImagickPixel('#fff'));

$idg->beginDocument(20, 30, 40, 30);
$idg->beginDocument(20, 30, 20, 30);

$idg->text('Lorem ipsum dolor sit amet', $fontRobotoBold, 26, '#000', Imagick::ALIGN_LEFT);

Expand Down
10 changes: 9 additions & 1 deletion src/Elements/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,13 @@
*/
class Document extends Element
{

/**
* @return int
*/
public function getHeight()
{
$height = parent::getHeight();
$height += $this->getTop();
return $height;
}
}
85 changes: 85 additions & 0 deletions tests/BlocksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
use Idg\Exceptions\StructureException;
/**
* Blocks test
* Class BlocksTest
*/
class BlocksTest extends \PHPUnit\Framework\TestCase
{
/**
* @return \Idg\Idg
*/
protected function getIdg()
{
return new \Idg\Idg(1000, 3000, null, new ImagickPixel('#fff'));
}

/**
* Test block
*/
public function testBlock()
{
$idg = $this->getIdg();
$idg->beginDocument();
$idg->beginBlock();
$idg->endBlock();
$idg->endDocument();
$idg->compose();

//empty document
$document = $idg->getDocument();
$this->assertEquals(0, $document->getHeight());
$this->assertEquals(1000, $document->getWidth());

$idg = $this->getIdg();
$idg->beginDocument();
$idg->beginBlock()->setTop(20)->setLeft(20);
$idg->beginBlock()->setTop(30);

$idg->endBlock();
$idg->beginBlock()->setTop(10);

$idg->endBlock();
$idg->endBlock();

$idg->beginBlock()->setTop(40);

$idg->endBlock();
$idg->endDocument();
$idg->compose();

$document = $idg->getDocument();
$this->assertEquals(20 + 30 + 10 + 40, $document->getHeight());
$this->assertEquals(1000, $document->getWidth());
}

/**
* Test absolute block
*/
public function testAbsoluteBlock()
{
$idg = $this->getIdg();
$idg->beginDocument(10);
$idg->beginBlock()->setTop(20)->setLeft(20);
$idg->beginBlock()->setTop(30);

$idg->endBlock();
$idg->beginBlock()->setTop(10);
$idg->beginAbsoluteBlock(40, 50)->setWidth(100);

$idg->endBlock();
$idg->endBlock();
$idg->endBlock();

// don't increase document height
$idg->beginAbsoluteBlock(40, 50)->setWidth(100);

$idg->endBlock();
$idg->endDocument();
$idg->compose();

$document = $idg->getDocument();
$this->assertEquals(10 + 20 + 30 + 10, $document->getHeight());
$this->assertEquals(1000, $document->getWidth());
}
}
51 changes: 51 additions & 0 deletions tests/StructureValidationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
use Idg\Exceptions\StructureException;
/**
* Structure validation test
* Class StructureValidationTest
*/
class StructureValidationTest extends \PHPUnit\Framework\TestCase
{
/**
* @return \Idg\Idg
*/
protected function getIdg()
{
return new \Idg\Idg(1000, 3000, null, new ImagickPixel('#fff'));
}

/**
* Test document is first
*/
public function testDocumentIsFirst()
{
$idg = $this->getIdg();
$this->expectException(StructureException::class);
$idg->beginBlock();
}

/**
* Test close element
*/
public function testCloseElement()
{
$idg = $this->getIdg();
$this->expectException(StructureException::class);
$idg->beginDocument();
$idg->compose();
}

/**
* Test column in row
*/
public function testColumn()
{
$idg = $this->getIdg();
$this->expectException(StructureException::class);
$idg->beginDocument();
$idg->beginColumn(200);
$idg->endColumn();
$idg->endDocument();
$idg->compose();
}
}
53 changes: 53 additions & 0 deletions tests/TablesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
use Idg\Exceptions\StructureException;
/**
* Tables test
* Class TablesTest
*/
class TablesTest extends \PHPUnit\Framework\TestCase
{
/**
* @return \Idg\Idg
*/
protected function getIdg()
{
return new \Idg\Idg(1000, 3000, null, new ImagickPixel('#fff'));
}

/**
* Test absolute block
*/
public function testTableWithBlocks()
{
$idg = $this->getIdg();
$idg->beginDocument(10);
$idg->beginBlock()->setTop(20)->setLeft(20);
$idg->beginBlock()->setTop(30);

$idg->endBlock();
$idg->beginBlock()->setTop(10);
$idg->beginAbsoluteBlock(40, 50)->setWidth(100);

$idg->endBlock();
$idg->endBlock();

$idg->beginRow()->setTop(15);
$idg->beginColumn(100)->setStaticHeight(150);
$idg->endColumn();
$idg->beginColumn(100)->setStaticHeight(170);
$idg->endColumn();
$idg->endRow();
$idg->endBlock();

// don't increase document height
$idg->beginAbsoluteBlock(40, 50)->setWidth(100);

$idg->endBlock();
$idg->endDocument();
$idg->compose();

$document = $idg->getDocument();
$this->assertEquals(10 + 20 + 30 + 10 + 15 + 170, $document->getHeight());
$this->assertEquals(1000, $document->getWidth());
}
}
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php

0 comments on commit a490345

Please sign in to comment.