From 2c03cde379c3f07ae7e5316ac689044fabbe9926 Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Wed, 4 Mar 2015 11:32:11 +0100 Subject: [PATCH] Adding unittest with phpunit #84, fix #13 --- .gitignore | 6 +- REVISION.md | 3 + phpunit.xml | 16 +++ test/CImage_RemoteDownloadTest.php | 165 +++++++++++++++++++++++++++++ test/CWhitelistTest.php | 128 ++++++++++++++++++++++ test/config.php | 6 ++ 6 files changed, 322 insertions(+), 2 deletions(-) create mode 100644 phpunit.xml create mode 100644 test/CImage_RemoteDownloadTest.php create mode 100644 test/CWhitelistTest.php create mode 100644 test/config.php diff --git a/.gitignore b/.gitignore index a0c83bd..e6bc346 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ -# Cache files # -###################### +# Cache files cache/* +# Test +coverage/ +coverage.clover diff --git a/REVISION.md b/REVISION.md index 4596f45..674027b 100644 --- a/REVISION.md +++ b/REVISION.md @@ -5,7 +5,10 @@ Revision history v0.7.0.x (latest) ------------------------------------- +* * Adding phpdoc, fix #48. +* Adding travis, fix #15. +* Adding scrutinizer, fix #57. v0.7.0 (2015-02-10) diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..e406494 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,16 @@ + + + + + + test + + + + + + + + + diff --git a/test/CImage_RemoteDownloadTest.php b/test/CImage_RemoteDownloadTest.php new file mode 100644 index 0000000..86ffee5 --- /dev/null +++ b/test/CImage_RemoteDownloadTest.php @@ -0,0 +1,165 @@ +setRemoteDownload(true); + + $res = $img->isRemoteSource($source); + $this->assertTrue($res, "Should be a valid remote source: '$source'."); + } + + + + /** + * Test + * + * @return void + * + * @dataProvider providerInvalidRemoteSource + */ + public function testAllowRemoteDownloadDefaultPatternInvalid($source) + { + $img = new CImage(); + $img->setRemoteDownload(true); + + $res = $img->isRemoteSource($source); + $this->assertFalse($res, "Should not be a valid remote source: '$source'."); + } + + + + /** + * Provider for hostname matching the whitelist. + * + * @return array + */ + public function providerHostnameMatch() + { + return [ + [ + "any.facebook.com", + "images.ak.instagram.com", + "google.com", + ], + ]; + } + + + + /** + * Test + * + * @param string $hostname matches the whitelist + * + * @return void + * + * @dataProvider providerHostnameMatch + * + */ + public function testRemoteHostWhitelistMatch($hostname) + { + $img = new CImage(); + $img->setRemoteHostWhitelist($this->remote_whitelist); + + $res = $img->isRemoteSourceOnWhitelist("http://$hostname/img.jpg"); + $this->assertTrue($res, "Should be a valid hostname on the whitelist: '$hostname'."); + } + + + + /** + * Provider for hostname not matching the whitelist. + * + * @return array + */ + public function providerHostnameNoMatch() + { + return [ + [ + "example.com", + ".com", + "img.jpg", + ], + ]; + } + + + + /** + * Test + * + * @param string $hostname not matching the whitelist + * + * @return void + * + * @dataProvider providerHostnameNoMatch + * + */ + public function testRemoteHostWhitelistNoMatch($hostname) + { + $img = new CImage(); + $img->setRemoteHostWhitelist($this->remote_whitelist); + + $res = $img->isRemoteSourceOnWhitelist("http://$hostname/img.jpg"); + $this->assertFalse($res, "Should not be a valid hostname on the whitelist: '$hostname'."); + } +} diff --git a/test/CWhitelistTest.php b/test/CWhitelistTest.php new file mode 100644 index 0000000..baaecc2 --- /dev/null +++ b/test/CWhitelistTest.php @@ -0,0 +1,128 @@ +set($this->remote_whitelist); + + $res = $whitelist->check($hostname); + $this->assertTrue($res, "Should be a valid hostname on the whitelist: '$hostname'."); + } + + + + /** + * Test + * + * @param string $hostname not matching the whitelist + * + * @return void + * + * @dataProvider providerHostnameNoMatch + * + */ + public function testRemoteHostWhitelistNoMatch($hostname) + { + $whitelist = new CWhitelist(); + $whitelist->set($this->remote_whitelist); + + $res = $whitelist->check($hostname); + $this->assertFalse($res, "Should not be a valid hostname on the whitelist: '$hostname'."); + } + + + + /** + * Test + * + * @expectedException Exception + * + * @return void + * + */ + public function testInvalidFormat() + { + $whitelist = new CWhitelist(); + $whitelist->set("should fail"); + } + + + + /** + * Test + * + * @return void + * + */ + public function testCheckEmpty() + { + $whitelist = new CWhitelist(); + $whitelist->set($this->remote_whitelist); + + $hostname = ""; + $res = $whitelist->check($hostname); + $this->assertFalse($res, "Should not be a valid hostname on the whitelist: '$hostname'."); + } +} diff --git a/test/config.php b/test/config.php new file mode 100644 index 0000000..e53e4b1 --- /dev/null +++ b/test/config.php @@ -0,0 +1,6 @@ +