1515use UserFrosting \Sprinkle \Core \Tests \RefreshDatabase ;
1616use UserFrosting \Sprinkle \Account \Account \Registration ;
1717use UserFrosting \Sprinkle \Account \Database \Models \Interfaces \UserInterface ;
18- use UserFrosting \Support \ Exception \ HttpException ;
18+ use UserFrosting \Sprinkle \ Account \ Database \ Models \ User ;
1919
2020/**
2121 * RegistrationTest Class
@@ -26,6 +26,17 @@ class RegistrationTest extends TestCase
2626 use TestDatabase;
2727 use RefreshDatabase;
2828
29+ /**
30+ * @var array Test user data
31+ */
32+ protected $ fakeUserData = [
33+ 'user_name ' => 'FooBar ' ,
34+ 'first_name ' => 'Foo ' ,
35+ 'last_name ' => 'Bar ' ,
36+ 'email ' => 'Foo@Bar.com ' ,
37+ 'password ' => 'FooBarFooBar123 '
38+ ];
39+
2940 public function tearDown ()
3041 {
3142 parent ::tearDown ();
@@ -45,7 +56,43 @@ public function setUp()
4556 }
4657
4758 /**
48- * testNormalRegistration
59+ * Test validation works
60+ */
61+ public function testValidation ()
62+ {
63+ $ registration = new Registration ($ this ->ci , [
64+ 'user_name ' => 'OwlFancy ' ,
65+ 'first_name ' => 'Owl ' ,
66+ 'last_name ' => 'Fancy ' ,
67+ 'email ' => 'owl@fancy.com ' ,
68+ 'password ' => 'owlFancy1234 '
69+ ]);
70+
71+ $ validation = $ registration ->validate ();
72+ $ this ->assertTrue ($ validation );
73+ }
74+
75+ /**
76+ * Test the $requiredProperties property
77+ * @depends testValidation
78+ * @expectedException UserFrosting\Support\Exception\HttpException
79+ * @expectedExceptionMessage Account can't be registrated as 'first_name' is required to create a new user.
80+ */
81+ public function testMissingFields ()
82+ {
83+ $ registration = new Registration ($ this ->ci , [
84+ 'user_name ' => 'OwlFancy ' ,
85+ //'first_name' => 'Owl',
86+ 'last_name ' => 'Fancy ' ,
87+ 'email ' => 'owl@fancy.com ' ,
88+ 'password ' => 'owlFancy1234 '
89+ ]);
90+
91+ $ validation = $ registration ->validate ();
92+ }
93+
94+ /**
95+ * @depends testValidation
4996 */
5097 public function testNormalRegistration ()
5198 {
@@ -56,77 +103,53 @@ public function testNormalRegistration()
56103 // Tests can't mail properly
57104 $ this ->ci ->config ['site.registration.require_email_verification ' ] = false ;
58105
59- // Genereate user data
60- $ fakeUserData = [
61- 'user_name ' => 'FooBar ' ,
62- 'first_name ' => 'Foo ' ,
63- 'last_name ' => 'Bar ' ,
64- 'email ' => 'Foo@Bar.com ' ,
65- 'password ' => 'FooBarFooBar123 '
66- ];
67-
68106 // Get class
69- $ registration = new Registration ($ this ->ci , $ fakeUserData );
107+ $ registration = new Registration ($ this ->ci , $ this -> fakeUserData );
70108 $ this ->assertInstanceOf (Registration::class, $ registration );
71109
72110 // Register user
73111 $ user = $ registration ->register ();
74112
75- // Registration should return a valid user
113+ // Registration should return a valid user, with a new ID
76114 $ this ->assertInstanceOf (UserInterface::class, $ user );
77115 $ this ->assertEquals ('FooBar ' , $ user ->user_name );
116+ $ this ->assertInternalType ('int ' , $ user ->id );
78117
79- // We try to register the same user again. Should throw an error
80- $ registration = new Registration ($ this ->ci , $ fakeUserData );
81- $ this ->expectException (HttpException::class);
82- $ validation = $ registration ->validate ();
83-
84- // Should throw email error if we change the username
85- $ fakeUserData ['user_name ' ] = 'BarFoo ' ;
86- $ registration = new Registration ($ this ->ci , $ fakeUserData );
87- $ this ->expectException (HttpException::class);
88- $ validation = $ registration ->validate ();
118+ // Make sure the user is added to the db by querying it
119+ $ users = User::where ('email ' , 'Foo@Bar.com ' )->get ();
120+ $ this ->assertCount (1 , $ users );
121+ $ this ->assertSame ('FooBar ' , $ users ->first ()['user_name ' ]);
89122 }
90123
91124 /**
92- * Test validation works
125+ * @depends testNormalRegistration
126+ * @expectedException UserFrosting\Support\Exception\HttpException
127+ * @expectedExceptionMessage Username is already in use.
93128 */
94- public function testValidation ()
129+ public function testValidationWithDuplicateUsername ()
95130 {
96- // Reset database
97- $ this ->refreshDatabase ();
98-
99- $ registration = new Registration ($ this ->ci , [
100- 'user_name ' => 'FooBar ' ,
101- 'first_name ' => 'Foo ' ,
102- 'last_name ' => 'Bar ' ,
103- 'email ' => 'Foo@Bar.com ' ,
104- 'password ' => 'FooBarFooBar123 '
105- ]);
131+ // Create the first user to test against
132+ $ this ->testNormalRegistration ();
106133
107- // Validate user. Shouldn't tell us the username is already in use since we reset the database
134+ // We try to register the same user again. Should throw an error
135+ $ registration = new Registration ($ this ->ci , $ this ->fakeUserData );
108136 $ validation = $ registration ->validate ();
109- $ this ->assertTrue ($ validation );
110137 }
111138
112139 /**
113- * Test the $requiredProperties property
140+ * @depends testNormalRegistration
141+ * @expectedException UserFrosting\Support\Exception\HttpException
142+ * @expectedExceptionMessage Email is already in use.
114143 */
115- public function testMissingFields ()
144+ public function testValidationWithDuplicateEmail ()
116145 {
117- // Reset database
118- $ this ->refreshDatabase ();
146+ // Create the first user to test against
147+ $ this ->testNormalRegistration ();
119148
120- $ registration = new Registration ($ this ->ci , [
121- 'user_name ' => 'FooBar ' ,
122- //'first_name' => 'Foo',
123- 'last_name ' => 'Bar ' ,
124- 'email ' => 'Foo@Bar.com ' ,
125- 'password ' => 'FooBarFooBar123 '
126- ]);
127-
128- // Validate user. Shouldn't tell us the username is already in use since we reset the database
129- $ this ->expectException (HttpException::class);
149+ // Should throw email error if we change the username
150+ $ fakeUserData = $ this ->fakeUserData ;
151+ $ fakeUserData ['user_name ' ] = 'BarFoo ' ;
152+ $ registration = new Registration ($ this ->ci , $ fakeUserData );
130153 $ validation = $ registration ->validate ();
131154 }
132155}
0 commit comments