diff --git a/tests/resources/app/models/Comment.cfc b/tests/resources/app/models/Comment.cfc index 69c75ed..5150e64 100644 --- a/tests/resources/app/models/Comment.cfc +++ b/tests/resources/app/models/Comment.cfc @@ -14,7 +14,8 @@ component property name="sentimentAnalysis" casts="JsonCast@quick"; variables._discriminators = [ - "InternalComment" + "InternalComment", + "PictureComment" ]; diff --git a/tests/resources/app/models/PictureComment.cfc b/tests/resources/app/models/PictureComment.cfc new file mode 100644 index 0000000..1f8c338 --- /dev/null +++ b/tests/resources/app/models/PictureComment.cfc @@ -0,0 +1,9 @@ +component + accessors="true" + extends="Comment" + table="pictureComments" + joincolumn="FK_comment" + discriminatorValue="picture" +{ + property name="filename"; +} \ No newline at end of file diff --git a/tests/resources/database/migrations/2024_06_13_134500_create_picture_comments_table.cfc b/tests/resources/database/migrations/2024_06_13_134500_create_picture_comments_table.cfc new file mode 100644 index 0000000..6220a8c --- /dev/null +++ b/tests/resources/database/migrations/2024_06_13_134500_create_picture_comments_table.cfc @@ -0,0 +1,41 @@ +component { + + function up( schema, qb ) { + schema.create( "pictureComments", function( t ) { + t.unsignedInteger( "FK_comment" ) + .references( "id" ) + .onTable( "comments" ) + .onUpdate( "CASCADE" ) + .onDelete( "CASCADE" ); + t.text( "filename" ); + t.primaryKey( "FK_comment" ); + } ); + + qb.newQuery().table( "comments" ).insert( [ + { + "id": 5, + "body": "This is an picture comment. It is very, very picturesque.", + "commentable_id": 1246, + "commentable_type": "Post", + "designation": "picture", + "user_id": 1, + "created_date": "2024-06-13 13:14:22", + "modified_date": "2024-06-13 13:14:22", + "sentimentAnalysis" : '{ "analyzed": true, "magnitude": 0.8, "score": 0.6 }' + } + ] ); + + qb.newQuery().table( "pictureComments" ).insert( [ + { + "FK_comment": 5, + "filename": "bliss.jpg" + } + ] ); + } + + function down( schema, qb ) { + schema.drop( "pictureComments" ); + qb.table( "comments" ).where( "id", 5 ).delete(); + } + +} diff --git a/tests/specs/integration/BaseEntity/ChildClassSpec.cfc b/tests/specs/integration/BaseEntity/ChildClassSpec.cfc index 9edd3fc..d132f75 100644 --- a/tests/specs/integration/BaseEntity/ChildClassSpec.cfc +++ b/tests/specs/integration/BaseEntity/ChildClassSpec.cfc @@ -331,6 +331,63 @@ component extends="tests.resources.ModuleIntegrationSpec" { } ); } ); + + it( "Will return an mix of child classes when retrieving all discriminated comments", function() { + var comments = getInstance( "Comment" ) + .where('designation', '!=', 'public') + .orderBy('id') + .get(); + + expect( comments[1] ).toBeInstanceOf( "InternalComment" ); + expect( comments[2] ).toBeInstanceOf( "PictureComment" ); + } ); + + it( "Will load foreign key when retrieving different child classes through parent", function() { + // comment id 4 = internal comment + var internalMemento = getInstance( "Comment" ) + .findOrFail(4) + .getMemento(); + + expect( internalMemento ).toHaveKey( "FK_comment" ); + expect( internalMemento['FK_comment'] ).toBe( 4 ) + + // comment id 5 = picture comment + var pictureMemento = getInstance( "Comment" ) + .findOrFail(5) + .getMemento(); + + expect( pictureMemento ).toHaveKey( "FK_comment" ); + expect( pictureMemento['FK_comment'] ).toBe( 5 ) + } ); + + it( "Can update each child class when fetching from parent class", function() { + // comment id 4 = internal comment + var internalComment = getInstance( "Comment" ) + .findOrFail(4) + .update({ + reason: 'Super private, ya know?' + }); + + var uInternalComment = getInstance( "Comment" ) + .findOrFail(4); + + expect( uInternalComment).toBeInstanceOf( "internalComment" ); + expect( uInternalComment.getReason() ).toBe('Super private, ya know?'); + + // comment id 5 = picture comment + var pictureComment = getInstance( "Comment" ) + .findOrFail(5) + .update({ + filename: 'Lenna.jpeg' + }); + + var uPictureComment = getInstance( "Comment" ) + .findOrFail(5); + + expect( uPictureComment).toBeInstanceOf( "pictureComment" ); + expect( uPictureComment.getFileName() ).toBe('Lenna.jpeg'); + } ); + it( "returns an array of discriminated entities when loading through a relationship", () => { var post = getInstance( "Post" ).findOrFail( 1245 ); var comments = post.getComments();