From 431bda4b5c362a9bab0dc6f043b769ebd7a4625e Mon Sep 17 00:00:00 2001 From: Jelmer Prins Date: Wed, 9 Dec 2020 16:03:33 +0100 Subject: [PATCH 1/5] Clean up tests Some tests didn't have assertions and that caused issues --- .gitignore | 1 + spoon/tests/database/SpoonDatabaseTest.php | 125 ++++++++++++++++----- spoon/tests/datagrid/SpoonDataGridTest.php | 4 +- spoon/tests/feed/SpoonFeedRSSTest.php | 1 + spoon/tests/form/SpoonFormDropdownTest.php | 2 +- spoon/tests/form/SpoonFormTest.php | 13 +++ spoon/tests/template/SpoonTemplateTest.php | 7 +- 7 files changed, 116 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index 61ead86..9cad150 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /vendor +spoon/tests/tmp/spoon.zip diff --git a/spoon/tests/database/SpoonDatabaseTest.php b/spoon/tests/database/SpoonDatabaseTest.php index 443fa33..d04a5a5 100644 --- a/spoon/tests/database/SpoonDatabaseTest.php +++ b/spoon/tests/database/SpoonDatabaseTest.php @@ -23,43 +23,53 @@ public function setup() public function testExecute() { // create database - try { $this->db->execute('CREATE DATABASE IF NOT EXISTS spoon_tests'); } - catch (SpoondatabaseException $e) - { + try { + $this->db->execute('CREATE DATABASE IF NOT EXISTS spoon_tests'); + } catch (SpoondatabaseException $e) { $this->fail('You should manually create a database "spoon_tests"'); } // clear all tables - if(count($this->db->getTables()) != 0) $this->db->drop($this->db->getTables()); + if (count($this->db->getTables()) != 0) { + $this->db->drop($this->db->getTables()); + } // create table users - $this->db->execute(" + $this->db->execute( + " CREATE TABLE `users` ( `id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY , `username` VARCHAR( 255 ) NOT NULL , `email` VARCHAR( 255 ) NOT NULL , `developer` ENUM( 'Y', 'N' ) NOT NULL - ) ENGINE = MYISAM;"); + ) ENGINE = MYISAM;" + ); // create dummy table - $this->db->execute(" + $this->db->execute( + " CREATE TABLE `test` ( `id` int(11) NOT NULL auto_increment, `value` varchar(255) NOT NULL, PRIMARY KEY (`id`) - ) ENGINE=MyISAM;"); + ) ENGINE=MyISAM;" + ); // create table with datetime - $this->db->execute(" + $this->db->execute( + " CREATE TABLE `date_test` ( `id` int(11) NOT NULL auto_increment, `date` DATETIME NOT NULL, PRIMARY KEY (`id`) - ) ENGINE=MyISAM;"); + ) ENGINE=MyISAM;" + ); // do nothing $this->db->execute('SELECT * FROM users LIMIT ?', 10); $this->db->execute('SELECT * FROM users limit :limit', array(':limit' => 10)); + $this->expectException(PDOException::class); + $this->db->execute('SELECT * FROM non_existing limit :limit', array(':limit' => 10)); } /** @@ -113,8 +123,13 @@ public function testInsert() $this->db->insert('users', $aData); // insert 1000 records - for($i = 0; $i < 1000; $i++) $array[$i] = $aData; + for ($i = 0; $i < 1000; $i++) { + $array[$i] = $aData; + } $this->db->insert('users', $array); + $aData['id'] = 1; + $this->expectException(PDOException::class); + $this->db->insert('users', $aData); } /** @@ -127,14 +142,20 @@ public function testInsertDate() $this->db->insert('date_test', $aData); // multiple rows data - $aData = array( - array( + $aData = [ + [ 'date' => new DateTime(), - ), - array( + ], + [ 'date' => new DateTime(), - ), - ); + ], + ]; + $this->db->insert('date_test', $aData); + $this->expectException(PDOException::class); + $aData = [ + 'id' => 1, + 'date' => new DateTime(), + ]; $this->db->insert('date_test', $aData); } @@ -165,11 +186,17 @@ public function testGetVar() { $this->assertEquals('1001', $this->db->getVar('SELECT COUNT(id) FROM users')); $this->assertEquals('1001', $this->db->getVar('SELECT COUNT(id) FROM users WHERE id != ?', 1337)); - $this->assertEquals('1001', $this->db->getVar('SELECT COUNT(id) FROM users WHERE id != :id', array(':id' => 1337))); + $this->assertEquals( + '1001', + $this->db->getVar('SELECT COUNT(id) FROM users WHERE id != :id', array(':id' => 1337)) + ); $this->assertEquals('1', $this->db->getVar('SELECT id FROM users ORDER BY id ASC LIMIT 1')); $this->assertEquals('1', $this->db->getVar('SELECT id FROM users ORDER BY id ASC LIMIT ?', 1)); $this->assertEquals('1', $this->db->getVar('SELECT id FROM users ORDER BY id ASC LIMIT ?', array(1))); - $this->assertEquals('1', $this->db->getVar('SELECT id FROM users ORDER BY id ASC LIMIT :limit', array(':limit' => 1))); + $this->assertEquals( + '1', + $this->db->getVar('SELECT id FROM users ORDER BY id ASC LIMIT :limit', array(':limit' => 1)) + ); } /** @@ -178,10 +205,27 @@ public function testGetVar() public function testGetPairs() { $this->assertEquals(10, count($this->db->getPairs('SELECT id, username FROM users LIMIT 10;'))); - $this->assertEquals(10, count($this->db->getPairs('SELECT id, username FROM users WHERE id != ? LIMIT 10', 1337))); - $this->assertEquals(10, count($this->db->getPairs('SELECT id, username FROM users WHERE id != ? LIMIT ?', array(1337, 10)))); - $this->assertEquals(10, count($this->db->getPairs('SELECT id, username FROM users WHERE id != :id LIMIT 10', array(':id' => 1337)))); - $this->assertEquals(10, count($this->db->getPairs('SELECT id, username FROM users WHERE id != :id LIMIT :limit', array(':id' => 1337, ':limit' => 10)))); + $this->assertEquals( + 10, + count($this->db->getPairs('SELECT id, username FROM users WHERE id != ? LIMIT 10', 1337)) + ); + $this->assertEquals( + 10, + count($this->db->getPairs('SELECT id, username FROM users WHERE id != ? LIMIT ?', array(1337, 10))) + ); + $this->assertEquals( + 10, + count($this->db->getPairs('SELECT id, username FROM users WHERE id != :id LIMIT 10', array(':id' => 1337))) + ); + $this->assertEquals( + 10, + count( + $this->db->getPairs( + 'SELECT id, username FROM users WHERE id != :id LIMIT :limit', + array(':id' => 1337, ':limit' => 10) + ) + ) + ); } /** @@ -206,13 +250,23 @@ public function testUpdate() $this->assertEquals(0, $this->db->getNumRows('SELECT id FROM users WHERE id = ?', 1337)); // update record - $this->db->update('users', array('id' => 1337, 'username' => 'Bauffman', 'email' => 'erik@bauffman.be', 'developer' => 'Y'), 'id = ?', 2); + $this->db->update( + 'users', + array('id' => 1337, 'username' => 'Bauffman', 'email' => 'erik@bauffman.be', 'developer' => 'Y'), + 'id = ?', + 2 + ); // 1 record with id 1337 $this->assertEquals(1, $this->db->getNumRows('SELECT id FROM users WHERE id = ?', 1337)); // update record - $this->db->update('users', array('id' => 1337), 'id = :leet AND id != :bauffman', array(':leet' => 1337, ':bauffman' => 291)); + $this->db->update( + 'users', + array('id' => 1337), + 'id = :leet AND id != :bauffman', + array(':leet' => 1337, ':bauffman' => 291) + ); } /** @@ -222,7 +276,7 @@ public function testUpdateDate() { // data $aData['date'] = new DateTime(); - $this->db->update('date_test', $aData); + self::assertTrue(is_int($this->db->update('date_test', $aData))); } /** @@ -230,8 +284,8 @@ public function testUpdateDate() */ public function testOptimize() { - $this->db->optimize('users'); - $this->db->optimize(array('users')); + self::assertArraySubset([], $this->db->optimize('users')); + self::assertArraySubset([], $this->db->optimize(array('users'))); } /** @@ -261,8 +315,14 @@ public function testGetRecord() $data['email'] = 'erik@bauffman.be'; $data['developer'] = 'Y'; - $this->assertEquals($data, $this->db->getRecord('SELECT username, email, developer FROM users WHERE id = ?', 1337)); - $this->assertEquals($data, $this->db->getRecord('SELECT username, email, developer FROM users WHERE id = :id', array(':id' => 1337))); + $this->assertEquals( + $data, + $this->db->getRecord('SELECT username, email, developer FROM users WHERE id = ?', 1337) + ); + $this->assertEquals( + $data, + $this->db->getRecord('SELECT username, email, developer FROM users WHERE id = :id', array(':id' => 1337)) + ); } /** @@ -271,7 +331,10 @@ public function testGetRecord() public function testGetRecords() { $this->assertEquals(100, count($this->db->getRecords('SELECT * FROM users WHERE id != ? LIMIT 100', 1337))); - $this->assertEquals(100, count($this->db->getRecords('SELECT * FROM users WHERE id != :id LIMIT 100', array(':id' => 1337)))); + $this->assertEquals( + 100, + count($this->db->getRecords('SELECT * FROM users WHERE id != :id LIMIT 100', array(':id' => 1337))) + ); } /** diff --git a/spoon/tests/datagrid/SpoonDataGridTest.php b/spoon/tests/datagrid/SpoonDataGridTest.php index 73fbcde..c95c436 100644 --- a/spoon/tests/datagrid/SpoonDataGridTest.php +++ b/spoon/tests/datagrid/SpoonDataGridTest.php @@ -21,6 +21,7 @@ public function testMain() // create datagrid $dg = new SpoonDatagrid($source); + self::assertInstanceOf(SpoonDataGrid::class, $dg); } public function testGetTemplate() @@ -37,7 +38,6 @@ public function testGetTemplate() $dg = new SpoonDatagrid($source); // fetch instance - if($dg->getTemplate() instanceof SpoonTemplate) { /* do nothing */ } - else throw new SpoonException('getTemplate should return an object of SpoonTemplate.'); + self::assertInstanceOf(SpoonTemplate::class, $dg->getTemplate(), 'getTemplate should return an object of SpoonTemplate.'); } } diff --git a/spoon/tests/feed/SpoonFeedRSSTest.php b/spoon/tests/feed/SpoonFeedRSSTest.php index d9e39c5..19cd09c 100644 --- a/spoon/tests/feed/SpoonFeedRSSTest.php +++ b/spoon/tests/feed/SpoonFeedRSSTest.php @@ -12,5 +12,6 @@ class SpoonFeedRSSTest extends TestCase public function testMain() { $rss = new SpoonFeedRSS('Spoon Library', 'http://feeds2.feedburner.com/spoonlibrary', 'Spoon Library - RSS feed.'); + self::assertInstanceOf(SpoonFeedRSS::class, $rss); } } diff --git a/spoon/tests/form/SpoonFormDropdownTest.php b/spoon/tests/form/SpoonFormDropdownTest.php index 61b3691..94641d4 100644 --- a/spoon/tests/form/SpoonFormDropdownTest.php +++ b/spoon/tests/form/SpoonFormDropdownTest.php @@ -163,7 +163,7 @@ public function testDefaultElement() */ public function testParse() { - $this->ddmDefaultElement->parse(); + self::assertTrue(is_string($this->ddmDefaultElement->parse())); } public function testRequired() diff --git a/spoon/tests/form/SpoonFormTest.php b/spoon/tests/form/SpoonFormTest.php index 44f001e..a8926a9 100644 --- a/spoon/tests/form/SpoonFormTest.php +++ b/spoon/tests/form/SpoonFormTest.php @@ -15,18 +15,31 @@ public function testMain() { $frm = new SpoonForm('name', 'action'); $frm->addButton('submit', 'submit'); + self::assertInstanceOf(SpoonFormButton::class, $frm->getField('submit')); $frm->addCheckbox('agree', true); + self::assertInstanceOf(SpoonFormCheckbox::class, $frm->getField('agree')); $frm->addDate('date', time(), 'd/m/Y'); + self::assertInstanceOf(SpoonFormDate::class, $frm->getField('date')); $frm->addDropdown('author', array(1 => 'Davy', 'Tijs', 'Dave'), 1); + self::assertInstanceOf(SpoonFormDropdown::class, $frm->getField('author')); $frm->addFile('pdf'); + self::assertInstanceOf(SpoonFormFile::class, $frm->getField('pdf')); $frm->addImage('image'); + self::assertInstanceOf(SpoonFormImage::class, $frm->getField('image')); $frm->addHidden('cant_see_me', 'whoop-tie-doo'); + self::assertInstanceOf(SpoonFormHidden::class, $frm->getField('cant_see_me')); $frm->addMultiCheckbox('hobbies', array(array('label' => 'Swimming', 'value' => 'swimming'))); + self::assertInstanceOf(SpoonFormMultiCheckbox::class, $frm->getField('hobbies')); $frm->addPassword('top_sekret', 'stars-and-stripes'); + self::assertInstanceOf(SpoonFormPassword::class, $frm->getField('top_sekret')); $frm->addRadiobutton('gender', array(array('label' => 'Male', 'value' => 'male'))); + self::assertInstanceOf(SpoonFormRadiobutton::class, $frm->getField('gender')); $frm->addTextarea('message', 'big piece of text'); + self::assertInstanceOf(SpoonFormTextarea::class, $frm->getField('message')); $frm->addText('email', 'something@example.org'); + self::assertInstanceOf(SpoonFormText::class, $frm->getField('email')); $frm->addText('now', date('H:i')); + self::assertInstanceOf(SpoonFormText::class, $frm->getField('now')); } public function testExistsField() diff --git a/spoon/tests/template/SpoonTemplateTest.php b/spoon/tests/template/SpoonTemplateTest.php index adeaa3c..f7fb9b7 100644 --- a/spoon/tests/template/SpoonTemplateTest.php +++ b/spoon/tests/template/SpoonTemplateTest.php @@ -2,19 +2,20 @@ use PHPUnit\Framework\TestCase; -$includePath = dirname(dirname(dirname(dirname(__FILE__)))); +$includePath = dirname(__DIR__, 3); set_include_path(get_include_path() . PATH_SEPARATOR . $includePath); require_once 'spoon/spoon.php'; class SpoonTemplateTest extends TestCase { - public function testMain() + public function testMain(): void { $tpl = new SpoonTemplate(); + self::assertInstanceOf(SpoonTemplate::class, $tpl); } - public function testGetAssignedValue() + public function testGetAssignedValue(): void { $tpl = new SpoonTemplate(); $tpl->assign('name', 'value'); From 0d4ed3af2a62133d5bd93519b87b9f1fbf342ad2 Mon Sep 17 00:00:00 2001 From: Jelmer Prins Date: Wed, 9 Dec 2020 16:15:14 +0100 Subject: [PATCH 2/5] Switch to github actions --- .github/workflows/run-tests.yml | 52 +++++++++++++++++++ .travis.yml | 21 -------- .../database/SpoonDatabaseLargeDataSet.php | 2 +- spoon/tests/database/SpoonDatabaseTest.php | 2 +- 4 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 .github/workflows/run-tests.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml new file mode 100644 index 0000000..386d928 --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -0,0 +1,52 @@ +name: run-tests + +on: [push, pull_request] + +jobs: + phpunit: + runs-on: ubuntu-latest + strategy: + fail-fast: true + matrix: + php: [7.1, 7.2, 7.3, 7.4] + testsuite: ["Spoon library Test Suite"] + name: PHPUnit - ${{ matrix.testsuite }} (PHP ${{ matrix.php }}) + services: + mysql: + image: mysql:5.7 + env: + MYSQL_DATABASE: spoon_tests + MYSQL_ROOT_PASSWORD: "spoon" + ports: + - 3306:3306 + options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Cache dependencies + uses: actions/cache@v2 + with: + path: ~/.composer/cache/files + key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, bcmath, intl, gd, exif, iconv, imagick + coverage: xdebug # Switch to PCOV for a speed gain when we can use PHP 7.2 & PHPUnit 8.0. + + - name: Install dependencies + env: + FORK_ENV: test + run: composer install -o + + - name: Execute tests + run: vendor/bin/simple-phpunit --testsuite=${{ matrix.testsuite}} --coverage-clover=${{ matrix.testsuite}}.clover + + - name: Upload Coverage report + uses: codecov/codecov-action@v1 + with: + file: ${{ matrix.testsuite}}.clover + flags: ${{ matrix.testsuite}} diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 08982d7..0000000 --- a/.travis.yml +++ /dev/null @@ -1,21 +0,0 @@ -language: php - -matrix: - include: - - php: 7.1 - -script: - - vendor/bin/simple-phpunit --coverage-clover=coverage.clover - -before_script: - - composer install - - mysql -uroot -e 'CREATE DATABASE spoon_tests' - - mysql -uroot -e "CREATE USER 'spoon'@'localhost' IDENTIFIED BY 'spoon'" - - mysql -uroot -e "GRANT ALL PRIVILEGES ON spoon_tests.* TO 'spoon'@'localhost'" - - mysql -uroot -e 'FLUSH PRIVILEGES' - -after_success: - - wget https://scrutinizer-ci.com/ocular.phar; - - php ocular.phar code-coverage:upload --format=php-clover coverage.clover; - -sudo: false diff --git a/spoon/tests/database/SpoonDatabaseLargeDataSet.php b/spoon/tests/database/SpoonDatabaseLargeDataSet.php index db0ad93..b77265b 100644 --- a/spoon/tests/database/SpoonDatabaseLargeDataSet.php +++ b/spoon/tests/database/SpoonDatabaseLargeDataSet.php @@ -35,7 +35,7 @@ class SpoonDatabaseLargeDataSet extends TestCase */ public function setup() { - $this->db = new SpoonDatabase('mysql', 'localhost', 'spoon', 'spoon', 'spoon_tests'); + $this->db = new SpoonDatabase('mysql', 'localhost', 'root', 'spoon', 'spoon_tests'); } /** diff --git a/spoon/tests/database/SpoonDatabaseTest.php b/spoon/tests/database/SpoonDatabaseTest.php index d04a5a5..49b9203 100644 --- a/spoon/tests/database/SpoonDatabaseTest.php +++ b/spoon/tests/database/SpoonDatabaseTest.php @@ -17,7 +17,7 @@ class SpoonDatabaseTest extends TestCase public function setup() { // create database object - $this->db = new SpoonDatabase('mysql', 'localhost', 'spoon', 'spoon', 'spoon_tests'); + $this->db = new SpoonDatabase('mysql', 'localhost', 'root', 'spoon', 'spoon_tests'); } public function testExecute() From c1f27046114b6d567f205a0acb4b9e448380e94d Mon Sep 17 00:00:00 2001 From: Jelmer Prins Date: Wed, 9 Dec 2020 16:29:53 +0100 Subject: [PATCH 3/5] Fix autoload in tests --- phpunit.xml.dist | 1 + 1 file changed, 1 insertion(+) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b4ed7be..f20258f 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -2,6 +2,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd" colors="true" + bootstrap="vendor/autoload.php" > From bc0bdcce1abdb5f23e73d8467c0b87c5da7a4fae Mon Sep 17 00:00:00 2001 From: Jelmer Prins Date: Wed, 9 Dec 2020 17:08:27 +0100 Subject: [PATCH 4/5] Try fixing library.php not found --- .github/workflows/run-tests.yml | 2 +- phpunit.xml.dist | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 386d928..2002857 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -9,7 +9,7 @@ jobs: fail-fast: true matrix: php: [7.1, 7.2, 7.3, 7.4] - testsuite: ["Spoon library Test Suite"] + testsuite: ["unit"] name: PHPUnit - ${{ matrix.testsuite }} (PHP ${{ matrix.php }}) services: mysql: diff --git a/phpunit.xml.dist b/phpunit.xml.dist index f20258f..a2dc2c1 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -5,7 +5,7 @@ bootstrap="vendor/autoload.php" > - + ./spoon/tests/ From b46b1781821fb10e1d32e7df5aa379e9750e2092 Mon Sep 17 00:00:00 2001 From: Jelmer Prins Date: Wed, 9 Dec 2020 17:17:05 +0100 Subject: [PATCH 5/5] Switch hostname to 127.0.0.1 for tests --- spoon/tests/database/SpoonDatabaseLargeDataSet.php | 2 +- spoon/tests/database/SpoonDatabaseTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spoon/tests/database/SpoonDatabaseLargeDataSet.php b/spoon/tests/database/SpoonDatabaseLargeDataSet.php index b77265b..f1ed3c4 100644 --- a/spoon/tests/database/SpoonDatabaseLargeDataSet.php +++ b/spoon/tests/database/SpoonDatabaseLargeDataSet.php @@ -35,7 +35,7 @@ class SpoonDatabaseLargeDataSet extends TestCase */ public function setup() { - $this->db = new SpoonDatabase('mysql', 'localhost', 'root', 'spoon', 'spoon_tests'); + $this->db = new SpoonDatabase('mysql', '127.0.0.1', 'root', 'spoon', 'spoon_tests'); } /** diff --git a/spoon/tests/database/SpoonDatabaseTest.php b/spoon/tests/database/SpoonDatabaseTest.php index 49b9203..da53a0c 100644 --- a/spoon/tests/database/SpoonDatabaseTest.php +++ b/spoon/tests/database/SpoonDatabaseTest.php @@ -17,7 +17,7 @@ class SpoonDatabaseTest extends TestCase public function setup() { // create database object - $this->db = new SpoonDatabase('mysql', 'localhost', 'root', 'spoon', 'spoon_tests'); + $this->db = new SpoonDatabase('mysql', '127.0.0.1', 'root', 'spoon', 'spoon_tests'); } public function testExecute()