[ Поиск ] - [ Пользователи ] - [ Календарь ]
Полная Версия: Написание юнит теста
Эли4ка
Здравствуйте, дорогие форумчане. Вопрос.
Есть небольшой класс, в котором функция проверяет регулярным выражением ссылка есть профиль или что-то другое. Код ниже:

class Instagram {
public function isProfile( string $str ) {
if(preg_match("/^https\:\/\/(?:www\.|)instagram\.com\/([a-zA-Z\_0-9\.-]{4,32})(?:\/|\?|$)/is", $str) ) {
return true;
} else {
return false;
}
}
}


Делаю следующие тесты для проверки на правильной отработки:
1) https://instagram.com/dashka_milashka/
2) https://instagram.com/dashka_milashka
3) https://instagram.com/dashka_milashka/?igshid=1aetd1z8y1b0t
4) https://instagram.com/dashka_milashka/?igshid=1aetd1z8y1b0t/
5) https://instagram.com/dashka_milashka?igshid=1aetd1z8y1b0t
6) https://instagram.com/dashka_milashka?igshid=1aetd1z8y1b0t/
7) https://instagram.com/p/dashka_milashka
8) https://instagram.com/^dashka_milashka/
9) https://instagram.com/das/
10) https://instagram.com/dashka_milashka_ochar...ayashka_nyashka
1-6 это валидные ссылки для профиля. Они должны быть true;
7-8 это не валидная ссылка, несоответствие регулярному выражению. Должны быть false;
9-10 это не валидная ссылка, несоблюдение длины имени профиля. Должна быть false.
Соответственно код:

class InstagramTest extends TestCase
{
protected $instagram;

protected function setUp(): void
{
$this->instagram = new Instagram;
}

public function testFailureUrlProfile1()
{
$this->assertTrue( $this->instagram->isProfile('https://instagram.com/dashka_milashka/') );
}

public function testFailureUrlProfile2()
{
$this->assertTrue( $this->instagram->isProfile('https://instagram.com/dashka_milashka?igshid=1aetd1z8y1b0t') );
}

public function testFailureUrlProfile3()
{
$this->assertTrue( $this->instagram->isProfile('https://instagram.com/dashka_milashka?igshid=1aetd1z8y1b0t/') );
}

public function testFailureUrlProfile4()
{
$this->assertTrue( $this->instagram->isProfile('https://instagram.com/dashka_milashka/?igshid=1aetd1z8y1b0t') );
}

public function testFailureUrlProfile5()
{
$this->assertTrue( $this->instagram->isProfile('https://instagram.com/dashka_milashka/?igshid=1aetd1z8y1b0t/') );
}

public function testFailureUrlProfile6()
{
$this->assertTrue( $this->instagram->isProfile('https://instagram.com/dashka_milashka') );
}
public function testFailureUrlProfile7()
{
$this->assertFalse( $this->instagram->isProfile('https://instagram.com/^^dashka_milashka') );
}

public function testFailureUrlProfile8()
{
$this->assertFalse( $this->instagram->isProfile('https://instagram.com/p/dashka_milashka') );
}

public function testFailureUrlProfile9()
{
$this->assertFalse( $this->instagram->isProfile('https://instagram.com/dashka_milashka_ocharovashka_obayashka_nyashka') );
}

public function testFailureUrlProfile10()
{
$this->assertFalse( $this->instagram->isProfile('https://instagram.com/das') );
}
}


Правильно ли делаю? Не избыточно ли? и вообще надо ли такое делать или надо было вместо assertTrue|assertfalse применять assertRegExp?
Быстрый ответ:

 Графические смайлики |  Показывать подпись
Здесь расположена полная версия этой страницы.
Invision Power Board © 2001-2025 Invision Power Services, Inc.