Skip to content

Commit 14ee488

Browse files
author
Igor Chepurnoy
committed
add tests
1 parent af642fa commit 14ee488

File tree

8 files changed

+352
-0
lines changed

8 files changed

+352
-0
lines changed

.gitattributes

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Ignore all test and documentation for archive
2+
/.gitattributes export-ignore
3+
/.gitignore export-ignore
4+
/.scrutinizer.yml export-ignore
5+
/.travis.yml export-ignore
6+
/phpunit.xml.dist export-ignore
7+
/tests export-ignore
8+
/docs export-ignore

.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# phpstorm project files
2+
.idea
3+
4+
# netbeans project files
5+
nbproject
6+
7+
# zend studio for eclipse project files
8+
.buildpath
9+
.project
10+
.settings
11+
12+
# windows thumbnail cache
13+
Thumbs.db
14+
15+
# composer vendor dir
16+
/vendor
17+
18+
/composer.lock
19+
20+
# composer itself is not needed
21+
composer.phar
22+
23+
# Mac DS_Store Files
24+
.DS_Store
25+
26+
# phpunit itself is not needed
27+
phpunit.phar
28+
# local phpunit config
29+
/phpunit.xml
30+
31+
# local tests configuration
32+
/tests/data/config.local.php
33+
34+
# runtime cache
35+
/tests/runtime

.travis.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
language: php
2+
3+
php:
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
- 7.0
8+
- hhvm
9+
10+
# run build against hhvm but allow them to fail
11+
# http://docs.travis-ci.com/user/build-configuration/#Rows-That-are-Allowed-To-Fail
12+
matrix:
13+
fast_finish: true
14+
allow_failures:
15+
- php: hhvm
16+
17+
# faster builds on new travis setup not using sudo
18+
sudo: false
19+
20+
# cache vendor dirs
21+
cache:
22+
directories:
23+
- $HOME/.composer/cache
24+
25+
install:
26+
- travis_retry composer self-update && composer --version
27+
- travis_retry composer global require "fxp/composer-asset-plugin:~1.1.1"
28+
- export PATH="$HOME/.composer/vendor/bin:$PATH"
29+
- travis_retry composer install --prefer-dist --no-interaction
30+
31+
script:
32+
- phpunit --verbose $PHPUNIT_FLAGS

phpunit.xml.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<phpunit bootstrap="./tests/bootstrap.php"
3+
colors="true"
4+
convertErrorsToExceptions="true"
5+
convertNoticesToExceptions="true"
6+
convertWarningsToExceptions="true"
7+
stopOnFailure="false">
8+
<testsuites>
9+
<testsuite name="Yii2mod Test Suite">
10+
<directory>./tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

tests/ECCValidatorTest.php

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
namespace yii2mod\validators\tests;
4+
5+
use yii\base\DynamicModel;
6+
use yii2mod\validators\ECCValidator;
7+
8+
/**
9+
* Class ECCValidatorTest
10+
* @package yii2mod\validators\tests
11+
*/
12+
class ECCValidatorTest extends TestCase
13+
{
14+
public function testValidateVisa()
15+
{
16+
// invalid card number
17+
$creditCard = 402400716084777;
18+
$model = DynamicModel::validateData(compact('creditCard'), [
19+
['creditCard', ECCValidator::className(), 'format' => [ECCValidator::VISA]],
20+
]);
21+
$this->assertTrue($model->hasErrors());
22+
// valid card number
23+
$model->creditCard = 4024007160847777;
24+
$this->assertTrue($model->validate());
25+
}
26+
27+
public function testValidateMasterCard()
28+
{
29+
// invalid card number
30+
$creditCard = 528623577209728;
31+
$model = DynamicModel::validateData(compact('creditCard'), [
32+
['creditCard', ECCValidator::className(), 'format' => [ECCValidator::MASTERCARD]],
33+
]);
34+
$this->assertTrue($model->hasErrors());
35+
// valid card number
36+
$model->creditCard = 5286235772097289;
37+
$this->assertTrue($model->validate());
38+
}
39+
40+
public function testValidateAmericanExpress()
41+
{
42+
// invalid card number
43+
$creditCard = 34429546472054;
44+
$model = DynamicModel::validateData(compact('creditCard'), [
45+
['creditCard', ECCValidator::className(), 'format' => [ECCValidator::AMERICAN_EXPRESS]],
46+
]);
47+
$this->assertTrue($model->hasErrors());
48+
// valid card number
49+
$model->creditCard = 344295464720540;
50+
$this->assertTrue($model->validate());
51+
}
52+
53+
public function testValidateDiscover()
54+
{
55+
// invalid card number
56+
$creditCard = 601100966223229;
57+
$model = DynamicModel::validateData(compact('creditCard'), [
58+
['creditCard', ECCValidator::className(), 'format' => [ECCValidator::DISCOVER]],
59+
]);
60+
$this->assertTrue($model->hasErrors());
61+
// valid card number
62+
$model->creditCard = 6011009662232292;
63+
$this->assertTrue($model->validate());
64+
}
65+
66+
public function testDinersClub()
67+
{
68+
// invalid card number
69+
$creditCard = 3668047906690;
70+
$model = DynamicModel::validateData(compact('creditCard'), [
71+
['creditCard', ECCValidator::className(), 'format' => [ECCValidator::DINERS_CLUB]],
72+
]);
73+
$this->assertTrue($model->hasErrors());
74+
// valid card number
75+
$model->creditCard = 36680479066901;
76+
$this->assertTrue($model->validate());
77+
}
78+
79+
public function testJCB()
80+
{
81+
// invalid card number
82+
$creditCard = 311229671441931;
83+
$model = DynamicModel::validateData(compact('creditCard'), [
84+
['creditCard', ECCValidator::className(), 'format' => [ECCValidator::JCB]],
85+
]);
86+
$this->assertTrue($model->hasErrors());
87+
// valid card number
88+
$model->creditCard = 3112296714419317;
89+
$this->assertTrue($model->validate());
90+
}
91+
92+
public function testVoyager()
93+
{
94+
// invalid card number
95+
$creditCard = 86998535980759;
96+
$model = DynamicModel::validateData(compact('creditCard'), [
97+
['creditCard', ECCValidator::className(), 'format' => [ECCValidator::VOYAGER]],
98+
]);
99+
$this->assertTrue($model->hasErrors());
100+
// valid card number
101+
$model->creditCard = 869985359807593;
102+
$this->assertTrue($model->validate());
103+
}
104+
105+
public function testElectron()
106+
{
107+
// invalid card number
108+
$creditCard = 417500840273551;
109+
$model = DynamicModel::validateData(compact('creditCard'), [
110+
['creditCard', ECCValidator::className(), 'format' => [ECCValidator::ELECTRON]],
111+
]);
112+
$this->assertTrue($model->hasErrors());
113+
// valid card number
114+
$model->creditCard = 4175008402735512;
115+
$this->assertTrue($model->validate());
116+
}
117+
118+
public function testLaser()
119+
{
120+
// invalid card number
121+
$creditCard = 670913703978936;
122+
$model = DynamicModel::validateData(compact('creditCard'), [
123+
['creditCard', ECCValidator::className(), 'format' => [ECCValidator::LASER]],
124+
]);
125+
$this->assertTrue($model->hasErrors());
126+
// valid card number
127+
$model->creditCard = 6709137039789368;
128+
$this->assertTrue($model->validate());
129+
}
130+
131+
public function testSolo()
132+
{
133+
// invalid card number
134+
$creditCard = 633458050000000;
135+
$model = DynamicModel::validateData(compact('creditCard'), [
136+
['creditCard', ECCValidator::className(), 'format' => [ECCValidator::SOLO]],
137+
]);
138+
$this->assertTrue($model->hasErrors());
139+
// valid card number
140+
$model->creditCard = 6334580500000000;
141+
$this->assertTrue($model->validate());
142+
}
143+
144+
public function testMaestro()
145+
{
146+
// invalid card number
147+
$creditCard = 676283509877930;
148+
$model = DynamicModel::validateData(compact('creditCard'), [
149+
['creditCard', ECCValidator::className(), 'format' => [ECCValidator::MAESTRO]],
150+
]);
151+
$this->assertTrue($model->hasErrors());
152+
// valid card number
153+
$model->creditCard = 6762835098779303;
154+
$this->assertTrue($model->validate());
155+
}
156+
}

tests/PhoneValidatorTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace yii2mod\validators\tests;
4+
5+
use yii\base\DynamicModel;
6+
7+
/**
8+
* Class PhoneValidatorTest
9+
* @package yii2mod\validators\tests
10+
*/
11+
class PhoneValidatorTest extends TestCase
12+
{
13+
public function testInvalidPhoneNumber()
14+
{
15+
$phone = 123123123;
16+
$model = DynamicModel::validateData(compact('phone'), [
17+
['phone', \yii2mod\validators\PhoneValidator::className(), 'country' => 'US'],
18+
]);
19+
$this->assertTrue($model->hasErrors());
20+
}
21+
22+
public function testValidPhoneNumber()
23+
{
24+
$phone = '718-494-0022';
25+
$model = DynamicModel::validateData(compact('phone'), [
26+
['phone', \yii2mod\validators\PhoneValidator::className(), 'country' => 'US'],
27+
]);
28+
$this->assertEmpty($model->getErrors());
29+
}
30+
}

tests/TestCase.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace yii2mod\validators\tests;
4+
5+
use yii\helpers\ArrayHelper;
6+
use Yii;
7+
8+
/**
9+
* This is the base class for all yii framework unit tests.
10+
*/
11+
class TestCase extends \PHPUnit_Framework_TestCase
12+
{
13+
protected function setUp()
14+
{
15+
parent::setUp();
16+
$this->mockApplication();
17+
}
18+
19+
protected function tearDown()
20+
{
21+
$this->destroyApplication();
22+
}
23+
24+
/**
25+
* Populates Yii::$app with a new application
26+
* The application will be destroyed on tearDown() automatically.
27+
* @param array $config The application configuration, if needed
28+
* @param string $appClass name of the application class to create
29+
*/
30+
protected function mockApplication($config = [], $appClass = '\yii\console\Application')
31+
{
32+
new $appClass(ArrayHelper::merge([
33+
'id' => 'testapp',
34+
'basePath' => __DIR__,
35+
'vendorPath' => $this->getVendorPath(),
36+
'components' => [
37+
'i18n' => [
38+
'translations' => [
39+
'*' => [
40+
'class' => 'yii\i18n\PhpMessageSource',
41+
'basePath' => '@app/messages', // if advanced application, set @frontend/messages
42+
'sourceLanguage' => 'en'
43+
],
44+
],
45+
],
46+
]
47+
], $config));
48+
}
49+
50+
/**
51+
* @return string vendor path
52+
*/
53+
protected function getVendorPath()
54+
{
55+
return dirname(__DIR__) . '/vendor';
56+
}
57+
58+
/**
59+
* Destroys application in Yii::$app by setting it to null.
60+
*/
61+
protected function destroyApplication()
62+
{
63+
Yii::$app = null;
64+
}
65+
}

tests/bootstrap.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
// ensure we get report on all possible php errors
4+
error_reporting(-1);
5+
6+
define('YII_ENABLE_ERROR_HANDLER', false);
7+
define('YII_DEBUG', true);
8+
9+
$_SERVER['SCRIPT_NAME'] = '/' . __DIR__;
10+
$_SERVER['SCRIPT_FILENAME'] = __FILE__;
11+
12+
require_once(__DIR__ . '/../vendor/autoload.php');
13+
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

0 commit comments

Comments
 (0)