CustomBehaviorFormTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 63
c 2
b 0
f 0
dl 0
loc 88
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testRules() 0 41 1
A setUp() 0 4 1
A testSave() 0 34 1
1
<?php
2
3
namespace site\tests\unit\models;
4
5
use Yii;
6
use \site\models\CustomBehaviorForm;
7
use \yii\di\Container;
8
use \common\models\Category;
9
10
class CustomBehaviorFormTest extends \Codeception\Test\Unit {
11
12
  private $user;
13
  private $category;
0 ignored issues
show
introduced by
The private property $category is not used, and could be removed.
Loading history...
14
15
  public function setUp() {
16
    $this->container = new Container;
0 ignored issues
show
Bug Best Practice introduced by
The property container does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
    $this->container->set(\common\interfaces\UserInterface::class, '\site\tests\_support\MockUser');
18
    $this->user = $this->container->get(\common\interfaces\UserInterface::class);
19
  }
20
21
  public function testRules() {
22
    $category = new Category();
23
    $form = new CustomBehaviorForm($this->user, $category);
24
25
    $form->attributes = [];
26
    expect('with no values, the form should not pass validation', $this->assertFalse($form->validate()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertFalse($form->validate()) targeting PHPUnit\Framework\Assert::assertFalse() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
27
28
    $form->attributes = [
29
      'name' => 'Clapping hands and smiling',
30
      'category_id' => 'definitely not a category id',
31
    ];
32
    expect('with an invalid category_id the form should not pass validation', $this->assertFalse($form->validate()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertFalse($form->validate()) targeting PHPUnit\Framework\Assert::assertFalse() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
33
34
    $form->attributes = [
35
      'category_id' => 'definitely not a category id',
36
    ];
37
    expect('without a name the form should not pass validation', $this->assertFalse($form->validate()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertFalse($form->validate()) targeting PHPUnit\Framework\Assert::assertFalse() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
38
    $form->attributes = [
39
      'name' => 'Clapping hands and smiling',
40
      'category_id' => 42,
41
    ];
42
    expect('with an invalid category_id the form should not pass validation', $this->assertFalse($form->validate()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertFalse($form->validate()) targeting PHPUnit\Framework\Assert::assertFalse() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
43
44
    $form->attributes = [
45
      'name' => 'Clapping hands and smiling',
46
      'category_id' => 6,
47
    ];
48
    expect('with a name and valid category_id, the form should validate', $this->assertTrue($form->validate()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertTrue($form->validate()) targeting PHPUnit\Framework\Assert::assertTrue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
49
50
    $form->attributes = [
51
      'name' => 'Clapping hands and smiling',
52
      'category_id' => 6,
53
    ];
54
    expect('with a name and valid category_id, the form should validate', $this->assertTrue($form->validate()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertTrue($form->validate()) targeting PHPUnit\Framework\Assert::assertTrue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
55
56
    $form->attributes = [
57
      'name' => '       Clapping hands and smiling .. -- aa       ',
58
      'category_id' => 6,
59
    ];
60
    $form->validate();
61
    expect('the form should trim the name when validating', $this->assertEquals('Clapping hands and smiling .. -- aa', $form->name));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals('Cla..... -- aa', $form->name) targeting PHPUnit\Framework\Assert::assertEquals() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
62
  }
63
64
  public function testSave() {
65
    $category = new Category();
66
    $user_id = 123;
67
    $this->user->id = $user_id;
68
69
    $custom_behavior = $this->getMockBuilder(\common\models\CustomBehavior::class)
70
      ->setmethods(['getisnewrecord', 'attributes', 'save'])
71
      ->getmock();
72
    $custom_behavior
73
      ->method('attributes')
74
      ->willReturn([
75
        'id',
76
        'user_id',
77
        'category_id',
78
        'name',
79
        'created_at',
80
        'updated_at',
81
      ]);
82
    $custom_behavior
83
      ->expects($this->once())
84
      ->method('save')
85
      ->willReturn($custom_behavior);
86
87
    Yii::$container->set(\common\interfaces\CustomBehaviorInterface::class, $custom_behavior);
88
    $form = new CustomBehaviorForm($this->user, $category);
89
    $name = 'a new custom behavior';
90
    $category_id = 4;
91
    $form->name = $name;
92
    $form->category_id = $category_id;
93
94
    $result = $form->save();
95
    expect('should have name correctly set', $this->assertEquals($result->name, $name));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($result->name, $name) targeting PHPUnit\Framework\Assert::assertEquals() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
96
    expect('should have category_id correctly set', $this->assertEquals($result->category_id, $category_id));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($res...egory_id, $category_id) targeting PHPUnit\Framework\Assert::assertEquals() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
97
    expect('should have name correctly set', $this->assertEquals($result->user_id, $user_id));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($result->user_id, $user_id) targeting PHPUnit\Framework\Assert::assertEquals() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
98
  }
99
}
100