2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-05-05 17:37:03 +01:00
|
|
|
namespace Symfony\Tests\Component\Validator\Constraints;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Validator\Constraints\Url;
|
|
|
|
use Symfony\Component\Validator\Constraints\UrlValidator;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
|
|
|
class UrlValidatorTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
protected $context;
|
2010-06-24 10:24:08 +01:00
|
|
|
protected $validator;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-12-06 14:45:37 +00:00
|
|
|
protected function setUp()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->validator = new UrlValidator();
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->validator->initialize($this->context);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-06-14 09:40:42 +01:00
|
|
|
protected function tearDown()
|
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context = null;
|
2011-06-14 09:40:42 +01:00
|
|
|
$this->validator = null;
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testNullIsValid()
|
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertTrue($this->validator->isValid(null, new Url()));
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-11-27 04:03:19 +00:00
|
|
|
public function testEmptyStringIsValid()
|
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2010-11-27 04:03:19 +00:00
|
|
|
$this->assertTrue($this->validator->isValid('', new Url()));
|
|
|
|
}
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException
|
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testExpectsStringCompatibleType()
|
|
|
|
{
|
|
|
|
$this->validator->isValid(new \stdClass(), new Url());
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider getValidUrls
|
|
|
|
*/
|
|
|
|
public function testValidUrls($url)
|
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertTrue($this->validator->isValid($url, new Url()));
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function getValidUrls()
|
|
|
|
{
|
|
|
|
return array(
|
2011-06-11 08:56:09 +01:00
|
|
|
array('http://a.pl'),
|
2010-06-24 10:24:08 +01:00
|
|
|
array('http://www.google.com'),
|
2011-06-11 08:56:09 +01:00
|
|
|
array('http://www.google.museum'),
|
2010-06-24 10:24:08 +01:00
|
|
|
array('https://google.com/'),
|
|
|
|
array('https://google.com:80/'),
|
2011-06-11 08:56:09 +01:00
|
|
|
array('http://www.example.coop/'),
|
|
|
|
array('http://www.test-example.com/'),
|
2011-03-06 11:40:06 +00:00
|
|
|
array('http://www.symfony.com/'),
|
2011-06-11 08:56:09 +01:00
|
|
|
array('http://symfony.fake/blog/'),
|
2012-02-23 05:06:46 +00:00
|
|
|
array('http://symfony.com/?'),
|
2011-06-11 08:56:09 +01:00
|
|
|
array('http://symfony.com/search?type=&q=url+validator'),
|
2012-02-23 05:06:46 +00:00
|
|
|
array('http://symfony.com/#'),
|
2012-02-23 05:12:02 +00:00
|
|
|
array('http://symfony.com/#?'),
|
2011-06-11 08:56:09 +01:00
|
|
|
array('http://www.symfony.com/doc/current/book/validation.html#supported-constraints'),
|
|
|
|
array('http://very.long.domain.name.com/'),
|
2010-06-24 10:24:08 +01:00
|
|
|
array('http://127.0.0.1/'),
|
|
|
|
array('http://127.0.0.1:80/'),
|
2011-04-22 15:28:55 +01:00
|
|
|
array('http://[::1]/'),
|
|
|
|
array('http://[::1]:80/'),
|
2011-06-11 08:56:09 +01:00
|
|
|
array('http://[1:2:3::4:5:6:7]/'),
|
2011-06-20 11:53:22 +01:00
|
|
|
array('http://sãopaulo.com/'),
|
|
|
|
array('http://sãopaulo.com.br/'),
|
|
|
|
array('http://пример.испытание/'),
|
|
|
|
array('http://مثال.إختبار/'),
|
|
|
|
array('http://例子.测试/'),
|
|
|
|
array('http://例子.測試/'),
|
|
|
|
array('http://例え.テスト/'),
|
|
|
|
array('http://مثال.آزمایشی/'),
|
|
|
|
array('http://실례.테스트/'),
|
|
|
|
array('http://العربية.idn.icann.org/'),
|
2011-06-20 12:52:36 +01:00
|
|
|
array('http://☎.com/'),
|
2010-06-24 10:24:08 +01:00
|
|
|
);
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider getInvalidUrls
|
|
|
|
*/
|
|
|
|
public function testInvalidUrls($url)
|
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$constraint = new Url(array(
|
|
|
|
'message' => 'myMessage'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('myMessage', array(
|
|
|
|
'{{ value }}' => $url,
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse($this->validator->isValid($url, $constraint));
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function getInvalidUrls()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('google.com'),
|
2011-06-11 08:56:09 +01:00
|
|
|
array('://google.com'),
|
|
|
|
array('http ://google.com'),
|
2010-06-24 10:24:08 +01:00
|
|
|
array('http:/google.com'),
|
2011-06-11 08:56:09 +01:00
|
|
|
array('http://goog_le.com'),
|
2010-06-24 10:24:08 +01:00
|
|
|
array('http://google.com::aa'),
|
2011-06-11 08:56:09 +01:00
|
|
|
array('http://google.com:aa'),
|
2012-02-23 05:06:46 +00:00
|
|
|
array('http://symfony.com?'),
|
|
|
|
array('http://symfony.com#'),
|
2011-05-25 10:00:43 +01:00
|
|
|
array('ftp://google.fr'),
|
2011-06-11 08:56:09 +01:00
|
|
|
array('faked://google.fr'),
|
|
|
|
array('http://127.0.0.1:aa/'),
|
|
|
|
array('ftp://[::1]/'),
|
|
|
|
array('http://[::1'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getValidCustomUrls
|
|
|
|
*/
|
|
|
|
public function testCustomProtocolIsValid($url)
|
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2011-06-11 08:56:09 +01:00
|
|
|
$constraint = new Url(array(
|
|
|
|
'protocols' => array('ftp', 'file', 'git')
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid($url, $constraint));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getValidCustomUrls()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('ftp://google.com'),
|
|
|
|
array('file://127.0.0.1'),
|
|
|
|
array('git://[::1]/'),
|
2010-06-24 10:24:08 +01:00
|
|
|
);
|
|
|
|
}
|
2011-06-23 12:39:36 +01:00
|
|
|
}
|